JAVAEE: JSP passes the value to the database and logs in the test information
PersonDAO.java
package dao;
import java.sql.*;
import domain.Person;
import util.JDBCUtils;
/ **
* A program in the data layer
* Objects are returned in the DAO layer, and these objects can be added, deleted, modified and checked in the business layer
* * /
public class PersonDAO {
* A program in the data layer
* Objects are returned in the DAO layer, and these objects can be added, deleted, modified and checked in the business layer
* * /
public class PersonDAO {
public Person login(Person user) { //登录
Person existUser = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
Person existUser = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {// JDBC query
conn = JDBCUtils.getConnection ();
String sql = "select * from person where name =? and password =?"; //
Stmt = conn.prepareStatement (sql); // will sql when the database is compiled Send to database for compilation
conn = JDBCUtils.getConnection ();
String sql = "select * from person where name =? and password =?"; //
Stmt = conn.prepareStatement (sql); // will sql when the database is compiled Send to database for compilation
// Set the sql parameter
stmt.setString (1, user.getName ()); // Incoming data value, will not be used as a keyword-to prevent injection
stmt.setString (2, user.getPassword ());
rs = stmt .executeQuery (); // Execute sql
stmt.setString (1, user.getName ()); // Incoming data value, will not be used as a keyword-to prevent injection
stmt.setString (2, user.getPassword ());
rs = stmt .executeQuery (); // Execute sql
// If the login is successful, there is only one record
if (rs.next ()) {
existUser = new Person (); // The logged-in user
existsUser.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");
if (rs.next ()) {
existUser = new Person (); // The logged-in user
existsUser.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;
}
}
}
}
This is a test file Test
package dao;
import util.JDBCUtils;
import java.sql.*;
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;
// TODO Auto-generated method stub
Person existUser = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {// JDBC query
conn = JDBCUtils.getConnection ();
String sql = "select * from person where name =? and password =?"; //
Stmt = conn.prepareStatement (sql); // SQL Send to database for compilation
conn = JDBCUtils.getConnection ();
String sql = "select * from person where name =? and password =?"; //
Stmt = conn.prepareStatement (sql); // SQL Send to database for compilation
// Set the sql parameter
stmt.setString (1, "Tom"); // Incoming data value, will not be used as a keyword-to prevent injection
stmt.setString (2, "12345");
rs = stmt.executeQuery () ; // Execute sql
stmt.setString (1, "Tom"); // Incoming data value, will not be used as a keyword-to prevent injection
stmt.setString (2, "12345");
rs = stmt.executeQuery () ; // Execute sql
// If the login is successful, there is only one record
if (rs.next ()) {
existUser = new Person (); // The logged-in user
existsUser.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());
}
if (rs.next ()) {
existUser = new Person (); // The logged-in user
existsUser.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());
}
}