Step1: Constructing connections
Class.forName ("Com.mysql.jdbc.Driver");
Connection con = drivermanager.getconnection ("jdbc:mysql://127.0.0.1:3306/db", "root", "123");
Step2: Constructing statements
String sql = "Select Username,password from T_user where username=?";
PreparedStatement stmt = con.preparestatement (sql);
Stmt.setstring (1, "Zhangshan"); "?" to SQL. (question mark) to pass the value.
Step3: Execute Query
ResultSet rs = Stmt.executequery (); RS is the result set returned by the SQL query statement
STEP4: Processing results (Java gets results returned by SQL query statements)
List List = new ArrayList ();
Iterate through the result set so that it can be a loop, how many records there are, how many times it will loop,
Once is a record, imagine a record like a table
Here can do query out of the data processing, can be encapsulated into objects and the like (that is, give it the value)
if (Rs.next ()) {//Iterate through the result set so that it can be written
User U = new user ();
String username = rs.getstring ("username");
String address = rs.getstring ("password");
U.setusername (username);
U.setaddress (address);
List.add (U);
}
The above example means that each record is encapsulated into a user object, and then these user objects are stored in a list
STEP4: Close connection, etc.
Java Get SQL query statement result