<%@page import="com.jaovo.msg.dao.UserDaoImpl"%> <%@page import="com.jaovo.msg.model.User"%> <%@ 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 name = request.getParameter("课程名称"); String teacher = request.getParameter("任课教师"); String place = request.getParameter("上课地点"); User user = new User(); user.setUsername(name); user.setPassword(teacher); user.setNickname(place); UserDaoImpl userDao = new UserDaoImpl(); userDao.add(user); %> <%="注册成功!" %><br> <a href = "list.jsp">查看所有信息</a> <a href = "loginwindow.jsp">登录</a><br> </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> <title>用户添加页面</title> </head> <body> <div align = "center"> <form action="add.jsp" method="get"> <table> <tr> <td>课程名称:</td> <td><input type="text" name="课程名称" /></td> </tr> <tr> <td>任课教师:</td> <td><input type="password" name="任课教师" /></td> </tr> <tr> <td>上课地点:</td> <td><input type="text" name="上课地点" /></td> </tr> </table> <input type="submit" value="保存" /> <input type="reset" value="重置" /> </form> </div> </body> </html> package com.jaovo.msg.Util; 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/jaovo_msg"; 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(); } } } public class UserDaoImpl implements IUserDao { @Override public void add(User user) { //获得链接对象 Connection connection = DBUtil.getConnection(); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { String sql = "insert into class(课程名称,任课教师,上课地点) value (?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); preparedStatement.setString(2, user.getPassword()); preparedStatement.setString(3, user.getNickname()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //关闭资源 DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } } } package com.jaovo.msg.model; public class User { private String name; private String place; private String teacher; public String getUsername() { return name; } public void setUsername(String name) { this.name = name; } public String getNickname() { return place; } public void setNickname(String place) { this.place = place; } public String getPassword() { return teacher; } public void setPassword(String teacher) { this.teacher = teacher; } }
截图