public class UserDao {
Public static Boolean login (User loginUser) throws SQLException {
//1. Write SQL
String SQL="select * from the user where the username=? And password=?" ;
//(2) for connection to connect to the database
The Connection conn=JDBCUtils. GetConnection ();
PreparedStatement PPS=conn. PrepareStatement (SQL);
String usernmae=loginUser. GetUsernmae ();
String password=loginUser. GetPassword ();
PPS. SetString (1, usernmae);
PPS. SetString (2, password);
The ResultSet ResultSet=PPS. ExecuteQuery ();
//if the back view is empty, then there is no this account password
JDBCUtils. Close (conn, PPS (Statement), the resultSet);
If (resultSet==null) {
return false;
} else {
return true;
}
}
}
CodePudding user response:
this is my test can connect to the database, but shows can connect
Public class UserDaoTest {
@ Test
Public void loginTest () {
User User=new User (" 123 ", "123");
Try {
Boolean login=UserDao. The login (user);
System. The out. Println (login);
} the catch (SQLException e) {
e.printStackTrace();
}
}
}
CodePudding user response:
When I use another problem when the server side, I put the encapsulation of the User object the User in the UserDao. The login (User) when an error@ WebServlet ("/loginServlet ")
Public class LoginServlet extends the HttpServlet {
@ Override
Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
//conversion code format
Request. SetCharacterEncoding (" utf-8 ");
//get request information
Map//will be encapsulated in the User object
User User=new User ();
SetThe set=PM. KeySet ();
If (the set!=null) {
For (String s: set) {
String [] value=https://bbs.csdn.net/topics/pm.get (s);
//according to the key value transfer, account password
If (s.e quals (" username ")) {
User. SetUsernmae (value [0]);
} the if (s.e quals (" password ")) {
User. SetPassword (value [0]);
}
}
}
//determine whether there is this account
Boolean login=false;
Try {
The login=UserDao. The login (user);
} the catch (SQLException e) {
e.printStackTrace();
}
If (login==true) {
The response. GetWriter (). The write (" login success ");
} else {
Response. SendRedirect ("/xgfirst/login. JSP ");
}
}
@ Override
Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
Enclosing the doPost (request, response);
}
}
CodePudding user response:
Java. Lang. ClassNotFoundException: com. Mysql.. JDBC Driver
The at org. Apache. Catalina. Loader. WebappClassLoaderBase. LoadClass (WebappClassLoaderBase. Java: 1358)
The at org. Apache. Catalina. Loader. WebappClassLoaderBase. LoadClass (WebappClassLoaderBase. Java: 1180)
The at Java. The base/Java. Lang. Class. ForName0 (Native Method)
The at Java. The base/Java. Lang. Class.forname (292) Class. Java:
At com. Utils. JDBCUtils. & lt; clinit> (JDBCUtils. Java: 25)
At com. UserDao. UserDao. Login (userDao. Java: 20)
At com. Servlet. LoginServlet. DoPost (LoginServlet. Java: 45)
The at javax.mail. Servlet. HTTP. HttpServlet. Service (HttpServlet. Java: 660)
The at javax.mail. Servlet. HTTP. HttpServlet. Service (HttpServlet. Java: 741)
The at org. Apache. Catalina. Core. ApplicationFilterChain. InternalDoFilter (ApplicationFilterChain. Java: 231)
The at org. Apache. Catalina. Core. ApplicationFilterChain. DoFilter (ApplicationFilterChain. Java: 166)
At org, apache tomcat, websocket server WsFilter. DoFilter (WsFilter. Java: 52)
The at org. Apache. Catalina. Core. ApplicationFilterChain. InternalDoFilter (ApplicationFilterChain. Java: 193)
The at org. Apache. Catalina. Core. ApplicationFilterChain. DoFilter (ApplicationFilterChain. Java: 166)
The at org. Apache. Catalina. Core. StandardWrapperValve. Invoke (StandardWrapperValve. Java: 199)
The at org. Apache. Catalina. Core. StandardContextValve. Invoke (StandardContextValve. Java: 96)
The at org. Apache. Catalina. The authenticator. AuthenticatorBase. Invoke (AuthenticatorBase. Java: 543)
The at org. Apache. Catalina. Core. StandardHostValve. Invoke (StandardHostValve. Java: 139)
The at org. Apache. Catalina. Valves. ErrorReportValve. Invoke (ErrorReportValve. Java: 81)
The at org. Apache. Catalina. Valves. AbstractAccessLogValve. Invoke (AbstractAccessLogValve. Java: 688)
The at org. Apache. Catalina. Core. StandardEngineValve. Invoke (StandardEngineValve. Java: 87)
At org. Apache. Catalina. Connector. CoyoteAdapter. Service (343) CoyoteAdapter. Java:
At org. Apache. Coyote. Http11. Http11Processor. Service (609) Http11Processor. Java:
The at org. Apache. Coyote. AbstractProcessorLight. Process (AbstractProcessorLight. Java: 65)
The at org. Apache. Coyote. AbstractProtocol $ConnectionHandler. Process (AbstractProtocol. Java: 818)
At org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun NioEndpoint. Java: (1623)
The at org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase. Java: 49)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull