UserDemo2. Java:
Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Public class UserDemo2 {
//define database driver link path
Private static final String url="JDBC: mysql://localhost: 3306/test".
Private static final String username="root";
Private static final String password="sjH13351292490";
The Connection conn=null;
//links database method
Public void getConnection () {//static
Try {
//1. The load driver
Class.forName("com.mysql.jdbc.Driver");
//2. Create a database link object
Conn=DriverManager.
GetConnection (url, username, password);
System. The out. Println (" database link success ");
} the catch (a ClassNotFoundException e) {
e.printStackTrace();
System. The out. Println (" database load failure ");
} the catch (SQLException e) {
e.printStackTrace();
System. The out. Println (" database link failure "+ e. oString ());
}
}
//user name and password is correct
Public void findNamePassored (String name, String password) {
//define the SQL statement
String SQL="select * from the where name=? And password=?" ;
Try {
//create a statement object
//executing SQL statements
PreparedStatement ps=conn. PrepareStatement (SQL);//the precompiled
ps.setString(1, name);
Ps. SetString (2, password);
The ResultSet rs=ps. ExecuteQuery ();
{if (rs. Next ())
System. The out. Println (" the user ");
} else {
System. The out. Println (" the user does not exist ");
}
} the catch (SQLException e) {
e.printStackTrace();
}
}
}
D.j ava:
Public class d {
Public static void main (String [] args) {
UserDemo2 user=new UserDemo2 ();
The user. The getConnection ();
User. FindNamePassored (" zmz1 ", "123");
}
}
CodePudding user response:
You are here to make a breakpoint, look for to the object itConn=DriverManager.
GetConnection (url, username, password);