Mysql 12: The DriverManager method for connecting to the database through JDBC
- Connect to the database through JDBC
- Create a program to connect to the database using JDBC, which contains seven steps:
- 1. load the JDBC driver:
- Before connecting to the database, load the driver of the database to be connected to the JVM (Java virtual machine ),
- This is achieved through the static method forName (String className) of the java. lang. Class.
- For example:
- Try {
- // Load the MySql driver class
- Class. forName ("com. mysql. jdbc. Driver ");
- } Catch (ClassNotFoundException e ){
- System. out. println ("The driver class cannot be found. failed to load the driver! ");
- E. printStackTrace ();
- }
- After the Driver class is loaded, the Driver class instance is registered to the DriverManager class.
- 2. provide the URL of the JDBC connection
- The connection URL defines the protocol, sub-protocol, and data source identification used to connect to the database.
- Writing format: Protocol: Sub-protocol: Data Source ID
- Protocol: always starts with JDBC in jdbc.
- Sub-protocol: the name of the bridge connection driver or database management system.
- Data Source ID: Mark the address and connection port of the database source.
- Example: (MySql connection URL)
- Jdbc: mysql:
- // Localhost: 3306/test? UseUnicode = true & characterEncoding = gbk;
- UseUnicode = true: indicates that the Unicode character set is used. If characterEncoding is set
- Gb2312 or GBK. this parameter must be set to true. CharacterEncoding = gbk: Character encoding method.
- 3. create a database connection
- To connect to the database, you must request to java. SQL. DriverManager and obtain the Connection object,
- This object represents a database connection.
- Use getConnectin (String url, String username,
- String password ).
- Password.
- For example:
- // Connect to the MySql database. both the 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 the java. SQL. Statement instance. the Statement instance is divided into three types:
- Type:
- 1. execute static SQL statements. It is usually implemented through a Statement instance.
- 2. execute dynamic SQL statements. It is usually implemented through the PreparedStatement instance.
- 3. execute the database stored procedure. It is usually implemented through the CallableStatement instance.
- Specific Implementation methods:
- Statement stmt = con. createStatement ();
- PreparedStatement pstmt = con. prepareStatement (SQL );
- CallableStatement cstmt =
- Con. prepareCall ("{CALL demoSp (? ,?)} ");
- 5. execute SQL statements
- The Statement interface provides three methods for executing SQL statements: executeQuery and executeUpdate.
- And execute
- 1. ResultSet executeQuery (String sqlString): Run the SQL statement used to query the database.
- Returns a result set object.
- 2. int executeUpdate (String sqlString): used to execute INSERT, UPDATE, or
- DELETE statements and SQL DDL statements, such as CREATE TABLE and DROP TABLE
- 3. execute (sqlString): used for executing and returning multiple result sets, multiple update counts, or a combination
- Statement.
- Specific implementation code:
- ResultSet rs = stmt.exe cuteQuery ("SELECT * FROM ...");
- Int rows = stmt.exe cuteUpdate ("insert ...");
- Boolean flag = stmt.exe cute (String SQL );
- 6. processing result
- Two cases:
- 1. the number of records affected by this operation is returned when an update is executed.
- 2. the result returned by executing the query is a ResultSet object.
- The ResultSet contains all rows that meet the conditions in the SQL statement, and provides
- The data in the row.
- Use the access method of the result set object to obtain data:
- 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 from column 1)
- 7. disable JDBC objects
- After the operation is completed, all the used JDBC objects should be closed to release the JDBC resources, close the sequence and
- Reverse order:
- 1. disable record set
- 2. close the statement
- 3. close the connection object
- If (rs! = Null) {// close the record set
- Try {
- Rs. close ();
- } Catch (SQLException e ){
- E. printStackTrace ();
- }
- }
- If (stmt! = Null) {// Close the declaration
- Try {
- Stmt. close ();
- } Catch (SQLException e ){
- E. printStackTrace ();
- }
- }
- If (conn! = Null) {// close the connection object
- Try {
- Conn. close ();
- } Catch (SQLException e ){
- E. printStackTrace ();
- }
- }