# JDBC Analysis
# One, JDBC concept
The so-called JDBC, actually comes from the Java database conectivity abbreviation, as the name implies to implement in the Java program to connect the database, so that the database can be manipulated. In fact, JDBC is a specification for a Java program to communicate with a database. This specification (interface) is defined under the java.sql package. Its four core objects:
-DriverManager: For loading Database drivers
-Connection: Used to connect to the database
-Statement: Used to execute SQL statements
-ResultSet: Used to encapsulate the result set returned from the database
# Two, JDBC first experience
"' Java
1. Registration Driver
Class.forName ("Com.mysql.jdbc.Driver");
2. Get a Connection object
String url= "jdbc:mysql:///db_day07?user=root&password=123";
Connection Connection = drivermanager.getconnection (URL);
3. Get the statement object that executes the SQL statement
Statement stmt = Connection.createstatement ();
4. Execute SQL statements
String sql = "SELECT * from user";
5. Get the result set object resultset, which encapsulates all the data from the database
ResultSet rs = stmt.executequery (SQL);
6. Parse the result set
while (Rs.next ()) {
System.out.println (Rs.getint (1) + ";" +rs.getstring (2) + ";" +rs.getint (3) + ";" +rs.getstring (4));
}
7. Releasing Resources
Rs.close ();
Stmt.close ();
Connection.close ();
```
# # Database-driven role:
A database driver is a set of classes that have implemented the JDBC specification. It encapsulates all classes that implement the JDBC specification.
For the MySQL driver, the Connection object, the object can connect to the MySQL database, in the same vein, the Oracle driver obtains the connection object, can connect the Oracle database.
# Three or four major core objects in detail
# 1.DriverManager: Load Driver
There are two ways to register a drive:
1) drivermanager.registerdriver (new Com.mysql.jdbc.Driver ());
This is inappropriate because the driver is registered with the Com.mysql.jdbc.Driver class loading, which is equivalent to registering twice
2) Class.forName ("Com.mysql.jdbc.Driver"); In this way, the Com.mysql.jdbc.Driver class is loaded directly, and the static code blocks of the driver class are registered as drivers when the class is loaded. Therefore, the registration drive is used in this way
# 2.Connection: Connect to the database
What kind of database driver, the connection object is connected to what kind of database, such as the MySQL database driver, the connection object is used to connect to the MySQL database.
How to get Connection objects:
-Way One:
"' Java
String url = "Jdbc:mysql://localhost:3306/db_day07";
String user = "root";
String Password = "123";
Connection conn = drivermanager.getconnection (URL, user, password);
```
-Method Two: Deposit user name and password into Properties property set object (map)
"' Java
Properties Pro = new properties ();
Pro.setproperty ("User", "root");
Pro.setproperty ("password", "123");
Connection conn = drivermanager.getconnection (URL, pro);
```
-Method Three: Use the user name and password as the URL parameter
"' Java
String url= "jdbc:mysql:///db_day07?user=root&password=123";
Connection conn = drivermanager.getconnection (URL);
```
# # 3.Statement Object: The object used to execute the SQL statement
Get the way:
The createstatement () of the Connection object;
Methods for executing SQL statements:
-Method One: ExecuteQuery (String sql): The return value is a ResultSet object that encapsulates the results of the query.
Use this method for queries of SQL statements. Because the query's SQL (SELECT) produces a result set.
-Method Two: Executeupdate (String sql): The return value is an int that represents the number of rows affected.
For a SQL statement (INSERT, UPDATE, delete) that does not produce a result, the return value of the method is the number of rows affected.
# # 4.ResultSet object: Used to encapsulate the result set
If the statement object executes the ExecuteQuery method produces the result, the result set is encapsulated into the ResultSet object.
There are several ways to master:
-Next (): Before the first call, the cursor points to the first line before the first call, referring to the first row, and the second call in the second row. The return value of the next method returns True if the row has data, otherwise false.
-GetInt (int columnindex): Finds the column's data through the index of the column, noting that the index starts at 1
-GetInt (String columnname): The column's data is found by the name of the column.
-GetString (), GetObject () ...
# Iv. Perfecting the JDBC code
# # encapsulates the Jdbcutil tool class to get connection objects through a tool class
"' Java
private static String driver = null;
private static String URL = null;
private static String user = null;
private static String password = NULL;
static{
Static code blocks are loaded as the class loads and are loaded only once.
Properties P = new properties ();
try {
P.load (Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("dbinfo.properties"));
Driver = P.getproperty ("Driver");
url = p.getproperty ("url");
user = P.getproperty ("user");
Password = p.getproperty ("password");
Class.forName (driver);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static Connection getconnection () throws SQLException, classnotfoundexception{
Return drivermanager.getconnection (Url,user,password);
}
```
# v. Case: Complete Login Verification
Implementation of login Verification
"' Java
Public User Finduser (string Username, string password) {
User user = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Get a Connection object from the tool class
conn = Jdbcutil.getconnection ();
stmt = Conn.createstatement ();
SELECT * from user where username= ' Zhangsan ' and password= ' 123456 '
String sql = "SELECT * from user where username= '" +username
+ "' and password= '" +password+ "'";
rs = stmt.executequery (SQL);
if (Rs.next ()) {
If the user name and password are correct, then a record is obtained and the record is parsed and encapsulated into the user object
user = new User ();
User.setuid (rs.getstring (1));
User.setusername (rs.getstring (2));
User.setpassword (Rs.getstring (3));
}
} catch (ClassNotFoundException | SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally{
Freeing resources
Jdbcutil.releaseresource (conn, stmt, RS);
}
return user;
}
```
Java technology sharing: A brief analysis of JDBC