JDBC Connection Database
• Create a program that connects to the database in JDBC with 7 steps:
1. Load the JDBC driver:
Before connecting to the database, first load the driver of the database you want to connect to the JVM (Java Virtual machine),
This is achieved through the static method forname (String className) of the Java.lang.Class class.
For example:
Try {
Load the MySQL driver class
Class.forName ("Com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e) {
System.out.println ("Driver class not found, load driver failed!") ");
E.printstacktrace ();
}
After a successful load, an instance of the driver class is registered in the DriverManager class.
2. Provide the URL of the JDBC connection
• The connection URL defines the protocol, sub-protocol, and data source identity when the database is connected.
• Written form: protocol: Sub-Protocol: Data source identification
Protocol: Always start with JDBC in JDBC
Sub-Protocol: A bridge-connected driver or database management system name.
Data source identification: The tag locates the address of the database source and the connection port.
For example: (MySQL connection URL)
Jdbc:mysql:
LOCALHOST:3306/TEST?USEUNICODE=TRUE&CHARACTERENCODING=GBK;
Useunicode=true: Indicates the use of the Unicode character set. If Characterencoding is set to
gb2312 or GBK, this parameter must be set to true . CHARACTERENCODING=GBK: The character encoding method.
3. Create a connection to the database
• To connect to a database, you need to request and obtain a connection object from Java.sql.DriverManager.
The object represents a connection to a database.
• Use DriverManager's getconnectin (string URL, string username,
String password) method to pass in the path to the specified database to be connected, the user name of the database, and
Password to get it.
For example:
Connect to MySQL database, user name and password are root
String url = "Jdbc:mysql://localhost:3306/test";
String username = "root";
String Password = "root";
Try {
Connection con =
Drivermanager.getconnection (URL, username, password);
}catch(SQLException se) {
SYSTEM.OUT.PRINTLN ("Database connection failed! ");
Se.printstacktrace ();
}
4. Create a statement
• To execute an SQL statement, you must obtain an java.sql.Statement instance that is divided into the following 3 statement instances
Type of:
1. Execute static SQL statements. Typically implemented through statement instances.
2. Execute dynamic SQL statements. Typically implemented through PreparedStatement instances.
3. Execute the database stored procedure. Typically implemented through CallableStatement instances.
The specific implementation method:
Statement stmt = Con.createstatement ();
PreparedStatement pstmt = con.preparestatement (sql);
CallableStatement cstmt =
Con.preparecall ("{Call Demosp (?,?)}");
5. Execute SQL statements
The statement interface provides three ways to execute SQL statements: ExecuteQuery, executeupdate
and execute
1, ResultSet executeQuery (String sqlString): Execute SQL statement that queries the database
A result set (ResultSet) object is returned.
2,int executeupdate (String sqlString): Used to perform INSERT, UPDATE, or
Delete statements and SQL DDL statements, such as CREATE table and drop table
3. Execute (sqlString): Used to perform a return of multiple result sets, multiple update counts, or a combination of the two
Statement.
Specific implementation code:
ResultSet rs = stmt.executequery ("SELECT * from ...");
int rows = stmt.executeupdate ("INSERT into ...");
boolean flag = Stmt.execute (String sql);
6. Processing results
Two cases:
1. The number of records affected by this operation is returned by performing the update.
2. The result of executing the query returned is a ResultSet object.
resultset contains all rows that conform to the conditions in the SQL statement, and it provides a set of get methods for these
Access to the data in the row.
• Get data using the access method of the result set (ResultSet) object:
while (Rs.next ()) {
String name = rs.getstring ("name");
String pass = rs.getstring (1); //This method is more efficient
}
(columns are numbered from left to right and start with column 1)
7. Close the JDBC Object
All JDBC objects used are closed after the operation is complete to release the JDBC resource, turn off order harmony
The opposite of the Ming Order:
1. Close record set
2. Closing the statement
3. Close the Connection object
if (rs! = null) { //close recordset
Try {
Rs.close ();
}catch(SQLException e) {
E.printstacktrace ();
}
}
if (stmt! = null) { //close declaration
Try {
Stmt.close ();
}catch(SQLException e) {
E.printstacktrace ();
}
}
if (conn! = null) { //Close Connection object
Try {
Conn.close ();
}catch(SQLException e) {
E.printstacktrace ();
}
}
JDBC Connection database code and procedures in full Java development JDBC Connection database