一、测试题目
二、完成过程
1.设计思想
①连接mysql数据库
②设计user类,增加参数
③设计add类,向数据库内增加内容
④设计addInput页面,完成录入操作
⑤设计add页面,接收录入的参数,并调用add类函数
2.源代码
user.java
package xihuan; public class User {
private String kcmc;
private String rkjs;
private String skdd;
public String getKcmc() {
return kcmc;
}
public void setKcmc(String kcmc) {
this.kcmc = kcmc;
}
public String getRkjs() {
return rkjs;
}
public void setRkjs(String rkjs) {
this.rkjs = rkjs;
}
public String getSkdd() {
return skdd;
}
public void setSkdd(String skdd) {
this.skdd = skdd;
} }
add.java
package xihuan;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class add{ public void add1(User user) {
//获得链接对象
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select count(*) from t_user where 'kcmc' = ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getKcmc());
//接收结果集
resultSet = preparedStatement.executeQuery();
//遍历结果集
sql = "insert into t_user(kcmc,rkjs,skdd) value (?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getKcmc());
preparedStatement.setString(2, user.getRkjs());
preparedStatement.setString(3, user.getSkdd());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
} }
}
DBUtil.java
package xihuan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class DBUtil { public static Connection getConnection() {
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/web1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
Connection connection = null;
try {
//2 创建链接对象connection
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
} //关闭资源的方法
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
UserException
package xihuan;
public class UserException extends RuntimeException{ public UserException() {
super();
// TODO Auto-generated constructor stub
} public UserException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
} public UserException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
} public UserException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
} public UserException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
} }
addInput1.jsp
<%@ 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>
<title>用户添加页面</title>
</head>
<body>
<form action="add1.jsp" method="get">
<table align="center" border="1">
<tr >
<td>课程名称:</td>
<td>
<input type="text" name="kcmc"/>
</td>
</tr>
<tr>
<td>任课教师:</td>
<td> <select name="rkjs">
<option value="王建民">王建民</option>
<option value="刘丹">刘丹</option>
<option value="刘立嘉">刘立嘉</option>
<option value="王辉">王辉</option>
<option value="杨子光">杨子光</option>
</select>
</td>
</tr>
<tr>
<td>上课地点:</td>
<td>
<select name="skdd">
<option value="基教">基教</option>
<option value="一教">一教</option>
<option value="二教">二教</option>
<option value="三教">三教</option>
</td>
<td>
<input type="text" name="jt"/>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="保存" />
</td>
</tr>
</table>
</form>
</body>
</html>
add1.jsp
<%@page import="xihuan.*"%>
<%@ 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>
<%
//接受客户端传递过来的参数
String kcmc= request.getParameter("kcmc");
String rkjs= request.getParameter("rkjs");
String skdd= request.getParameter("skdd");
String jt= request.getParameter("jt");
User f=new User();
f.setKcmc(kcmc);
f.setSkdd(skdd+jt);
f.setRkjs(rkjs);
add a = new add();
a.add1(f);
%> <body>
保存成功<br>
</body>
</html>
三、结果显示
三、psp0级
1.项目计划总结
时间计划:上课时间10点-10点40,下午2点-3点,晚上6点-7点
内容规划:上午完成最基本的连接录入,下午完成中间的异常情况,晚上撰写博客园总结收获
2.时间记录日志
实际时间:10点开始-10点43,下午1点42-2点24,晚上6点28-7点10,共计约2小时
内容基本完成
3.缺陷记录日志
①界面不美观
②有一些异常处理没有写到,比如录入为空时
四、实验总结
这次实验完成的很顺,因为前一天晚上看了四个多小时关于这方面的操作,主要是中间那部分选择老师和地点的,我没有按照老师的要求,而是做了一个下拉菜单,因为我看课程表都是可以选择的,这样更加方便一些。