软件工程概论 - 课堂测试

课题:

编写教师开设课程的网页

 

代码:

package pers.sun.DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class Data {   
 
 public static Connection getConnection() {
  //加载驱动
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  
  String root="root";
  String password="sunyu";
  String url="jdbc:mysql://localhost:3306/user_message";
  //链接对象
  Connection con=null;
  try {
   con=DriverManager.getConnection(url,root,password);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  return con;
  
 }
 
 //关闭资源
 public static void close(Connection con) {
   try {
    if(con!=null)
     con.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
 }
 
 public static void close(PreparedStatement pre) {
  try {
   if(pre!=null)
    pre.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 } 
 
 public static void close(ResultSet result) {
  try {
   if(result!=null)
    result.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}

package pers.sun.user;

public class Teacher {
 private String classname;
 private String tename;
 private String teplace;
 
 public String getClassname() {
  return classname;
 }
 public void setClassname(String classname) {
  this.classname = classname;
 }
 public String getTename() {
  return tename;
 }
 public void setTename(String tename) {
  this.tename = tename;
 }
 public String getTeplace() {
  return teplace;
 }
 public void setTeplace(String teplace) {
  this.teplace = teplace;
 }
 
}

package pers.sun.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import pers.sun.DataBase.Data;

public class UserTool {
//1添加用户
 public static void add(User tuser) {
  //获得链接对象
  Connection con=Data.getConnection();
  //插入
  String sql="insert into user_infor(username,password) value(?,?)";
  //语句传输对象
  PreparedStatement pre=null;
  try {
   pre=con.prepareStatement(sql);
   //写进表
   pre.setString(1, tuser.getUsername());
   pre.setString(2, tuser.getPassword());
   //????刷新???
   pre.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Data.close(pre);
   Data.close(con);
  }
 }
 public static void add(Teacher teacher) {
  //获得链接对象
  Connection con=Data.getConnection();
  //插入
  String sql="insert into teacher_infor(classname,teachername,teachplace) value(?,?,?)";
  //语句传输对象
  PreparedStatement pre=null;
  try {
   pre=con.prepareStatement(sql);
   //写进表
   pre.setString(1, teacher.getClassname());
   pre.setString(2, teacher.getTename());
   pre.setString(3, teacher.getTeplace());
   //????刷新???
   pre.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Data.close(pre);
   Data.close(con);
  }
  
 }
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="pers.sun.user.*" %>
<%@ page import="pers.sun.judge.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 
 String classnamex=request.getParameter("classname");
 String tenamex=request.getParameter("tename");
 String placex=request.getParameter("place");
 
 boolean valuex=ValueData.valueNull(request, new String[]{"classname","tename","place"});
 if(!valuex)
 {
%> 
 <jsp:forward page="loginshow.jsp"></jsp:forward>
<%
 }
 Teacher tea=new Teacher();
 tea.setClassname(classnamex);
 tea.setTename(tenamex);
 tea.setTeplace(placex);
 
 UserTool.add(tea);
 
%>
<jsp:forward page="xinxi.jsp"></jsp:forward>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="pers.sun.judge.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登陆</title>
</head>
<body>
<form action="loginhandl.jsp" method="post">
 <table align="center" border="1" width="500">
  <tr>
   <td>课程名称</td>
   <td><input type="text" name="classname">
   <%=ValueData.showError(request,"classname") %>
   </td>
  </tr>
  <tr>
   <td>任课教师</td>
   <td><input type="text" name="tename">
   <%=ValueData.showError(request,"tename") %>
   </td>
  </tr>
  <tr>
   <td>上课地点</td>
   <td><input type="text" name="place">
   <%=ValueData.showError(request,"place") %>
   </td>
  </tr>
  <tr align="center">
   <td colspan="2">
   <input type="submit" value="保存">
   <input type="reset" value="重置">
   </td>
  </tr>
 </table>
</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <h1 align="center">添加成功!</h1>
</body>
</html>

 

posted @ 2017-11-28 22:35  丨Kouch  阅读(175)  评论(0编辑  收藏  举报