1. 程式人生 > >軟件概論課堂測試

軟件概論課堂測試

編寫 tcl n! val url tle each war oct

課題:

編寫教師開設課程的網頁

代碼:

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>

軟件概論課堂測試