JAVAEE:JSP传值给数据库,测试信息进行登录
PersonDAO.java
package dao; import java.sql.*; import domain.Person; import util.JDBCUtils; /** * 数据层一个程序 * DAO层返回对象,在业务层通过操作这些对象来实现增删改查 * */ public class PersonDAO { public Person login(Person user) { //登录 Person existUser = null; Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try {//JDBC查询 conn = JDBCUtils.getConnection(); String sql = "select * from person where name=? and password=?"; //数据库编译时 stmt = conn.prepareStatement(sql); //将sql发送给数据库进行编译 //设置sql参数 stmt.setString(1,user.getName()); //传入数据值,不会作为关键字 --防止注入 stmt.setString(2,user.getPassword()); rs = stmt.executeQuery(); //执行sql //如果登陆成功,只有一条记录 if (rs.next()) { existUser=new Person(); //表示已登录用户 existUser.setID(rs.getString("ID")); existUser.setName(rs.getString("name")); existUser.setPassword(rs.getString("password")); existUser.setBirthDate(rs.getDate("birthday")); } } catch (Exception e) { e.printStackTrace(); } System.out.println(user.getName()); System.out.println(user.getPassword()); try { System.out.println(rs.getString("name")); } catch (Exception e) { // TODO: handle exception System.out.println("2"); } return existUser; } }
这个是个测试文件Test
package dao; import util.JDBCUtils; import java.sql.*; import domain.Person; public class test { public static void main(String[] args) { // TODO Auto-generated method stub Person existUser = null; Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try {//JDBC查询 conn = JDBCUtils.getConnection(); String sql = "select * from person where name=? and password=?"; //数据库编译时 stmt = conn.prepareStatement(sql); //将sql发送给数据库进行编译 //设置sql参数 stmt.setString(1, "Tom"); //传入数据值,不会作为关键字 --防止注入 stmt.setString(2, "12345"); rs = stmt.executeQuery(); //执行sql //如果登陆成功,只有一条记录 if (rs.next()) { existUser=new Person(); //表示已登录用户 existUser.setID(rs.getString("ID")); existUser.setName(rs.getString("name")); existUser.setPassword(rs.getString("password")); existUser.setBirthDate(rs.getDate("birthday")); } } catch (Exception e) { e.printStackTrace(); } try { System.out.println(rs.getString("name")); } catch (Exception e) { // TODO: handle exception } System.out.println(existUser.getID()); System.out.println(existUser.getName()); System.out.println(existUser.getPassword()); System.out.println(existUser.getBirthDate()); } }
这是我做的一个测试 如果直接输入Tom和12345 数据库就会读到 包括结果集rs和新建的person类existuser都有数值
之后我再DAO类里面做测试 user的值能传过来 但是查询过后就没有 rs没有数值 try语句会走向catch
JDBCUtils
package util; import java.sql.*; /* * Person类的JDBC 工具类,抽取公共方法 */ public class JDBCUtils { //设置数据库名称和登录用户信息 private static String DRIVERCLASS="com.mysql.cj.jdbc.Driver"; private static String URL="jdbc:mysql://localhost:3306/factory?serverTimezone=UTC"; private static final String USER="root"; //数据库用户 private static final String PASSWORD="12345"; //用户root的连接数据库的密码 private static Connection conn=null; //与数据库的连接; // 装载驱动 private static void loadDriver() { try { //1.加载MYSQL JDBC驱动程序 Class.forName(DRIVERCLASS); } catch(ClassNotFoundException e) { e.printStackTrace(); } } //建立连接 public static Connection getConnection() throws Exception { loadDriver(); return (Connection)DriverManager.getConnection(URL, USER, PASSWORD); } //释放资源 public static void release(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } release(stmt, conn); } public static void release(Statement stmt, Connection conn) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } } }
Person.java
package domain; import java.util.Date; //传输对象,保存User相关数据 public class Person { private String ID; //类属性需要与数据库中的字段一一对应 private String name; private String password; private Date birthday; public Person() { super(); } public Person(String iD, String name, String password, Date birthDate) { super(); ID = iD; this.name = name; this.password = password; this.birthday = birthDate; } public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthDate() { return birthday; } public void setBirthDate(Date birthDate) { this.birthday = birthDate; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
personservice.java
package service; import domain.Person; import dao.PersonDAO; /** * 业务层一个程序 * */ public class PersonService { /** * 根据用户名 和 密码查询其它信息 * @param user * @return 如果返回null证明 用户名或者密码错误 */ public Person login(Person user) { PersonDAO userDAO = new PersonDAO(); //查询, 将 user对象传递 DAO return userDAO.login(user); } }
我最后解决了 原来是下面lib没有jar包 jsp访问不了mysql