课堂作业:课程增加
一.设计思路
通过html制作出简单的输入框,然后通过request将其传入已经建立好的数据库的表中。
二.源代码
package duo; 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 'name1' = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getName()); //接收结果集 resultSet = preparedStatement.executeQuery(); //遍历结果集 sql = "insert into t_user(name,teacher,place) value (?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getName()); preparedStatement.setString(2, user.getTeacher()); preparedStatement.setString(3, user.getPlace()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //关闭资源 DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } } }
package duo; 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(); } } }
package duo; public class User { private String name1; private String teacher; private String place; public String getName() { return name1; } public void setName(String name) { this.name1 = name; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } }
package duo; 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 } }
<%@ 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" width="500"> <tr> <td>课程名称:</td> <td> <input type="text" name="name1"/> </td> </tr> <tr> <td>任课教师:</td> <td> <input type="text" name="teacher"/> </td> </tr> <tr> <td>上课地点:</td> <td> <input type="text" name="place"/> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="提交" /> </td> </tr> </table> </form> </body> </html>
<%@page import="duo.*"%> <%@ 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 name1= request.getParameter("name1"); String teacher= request.getParameter("teacher"); String place= request.getParameter("place"); User f=new User(); f.setName(name1); f.setTeacher(teacher); f.setPlace(place); add a = new add(); a.add1(f); %> <body> 添加成功<br> </body>
三.备注
此程序仍然没有能够加入输入课程名称时的限制条件,关于在制作页面时采用下拉菜单的形式是问的蔡金阳,嘻嘻