Query all
// Query all users public list <user> finduser () {list <user> List = new arraylist <user> (); try {datacon Dc = new datacon (); // database object connection con = dc. getconnction (); // connect to the database string SQL = "select * from user"; // SQL statement preparedstatement PS = con. preparestatement (SQL); // process the SQL statement resultset rs = ps.exe cutequery (); // execute the query while (RS. next () {user US = new user (); // create user object us. setusername (RS. getstring ("username"); // get the username passed to the user object us in the database column. setpassword (RS. getstring ("password"); // you can obtain the name of the column in the database and input the password to the user object list. add (US);} DC. closeall (RS, PS, con); // closes the database} catch (exception e) {e. printstacktrace ();} return list ;}
// Query all users in the test class. Test userdao = new userdao (); User user = new user (); @ test public void testuser () {list <user> List = userdao. finduser (); // call the method for (User U: List) system. out. println (U. getUserName ());}
Determine the user name and password
// Query a single user public int selectuser (User user) {try {datacon Dc = new datacon (); // database object connection con = dc. getconnction (); // connect to the database string SQL = "select * from user where username =? And Password =? "; // The SQL statement preparedstatement PS = con. preparestatement (SQL); PS. setstring (1, user. getUserName (); // get the user name ps. setstring (2, user. getPassword (); // obtain the password resultset rs = ps.exe cutequery (); // execute the query if (RS. next () {Return Rs. getint ("ID") ;}} catch (exception e) {e. printstacktrace ();} return-1 ;}
// Determine the user's code in the test class
@ Test public void testselect () {user. setusername ("Ga"); // input the user value. setpassword ("123"); // input value userdao. selectuser (User); // call method}
Data insertion
// Insert the public void insertuser (User user) {try {datacon Dc = new datacon (); // database object connection con = dc. getconnction (); // connect to the database string SQL = "insert into user (username, password) values (?,?) "; // Insert statement preparedstatement PS = con. preparestatement (SQL); // Execute SQL ps. setstring (1, user. getUserName (); // input the user name PS to be passed in. setstring (2, user. getPassword (); // pass in the password ps.exe cuteupdate (); // execute insert} catch (exception e) {e. printstacktrace ();}}
// Insert information to the test class for testing
@ Test public void testinsert () {user. setusername ("Haha"); // input user. setpassword ("123"); // input value userdao. insertuser (User); // call method}
// Update the User Password
Public void updateuser (User user) {try {datacon Dc = new datacon (); // database object connection con = dc. getconnction (); // connect to the database string SQL = "update user SET Password =? Where id =? "; // Update statement preparedstatement PS = con. preparestatement (SQL); // Execute SQL ps. setstring (1, user. getPassword (); // input the password PS to be passed in. setint (2, user. GETID (); // pass in the password ps.exe cuteupdate (); // execute insert} catch (exception e) {e. printstacktrace ();}}
// Test user updates
public void testUpdate(){ User user = new User(); user.setPassWord("123"); user.setId(5); userDao.updateUser(user); }
// Delete
Public void deleteuser (int id) {try {datacon Dc = new datacon (); // database object connection con = dc. getconnction (); // connect to the database string SQL = "delete from user where id =? "; // Delete statement preparedstatement PS = con. preparestatement (SQL); PS. setint (1, ID); // The deleted ID ps.exe cuteupdate ();} catch (exception e) {e. printstacktrace ();}}
// Test Deletion
public void testDelete(){ userDao.deleteUser(11); userDao.deleteUser(12); }