JDBC: Java Database Connectivity, short for JDBC

To access the database, the need for each database commodity to provide the database access drivers (database vendors must provide), Oracle database — Oracle driver, MySQL database –MySQL driver, SQL Server database — SqlServer driver

Driver: a set of standard interfaces (apis) to access this database

In order to unify the database access specification, SUN customized a set of all database factories need to respect the access standard (JDBC API), each database vendor according to this standard to develop the driver of the database; For programmers, different databases operate on the same API;

JDBC API: a set of database access interfaces; The custom apis for this specification are organized in a java.sql package;

Create a JDBC driver to connect to a database, including 7 steps: Before you connect to a database, you first load the driver of the database you want to connect to into the JVM (Java Virtual Machine) 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 successful loading, instances of the Driver class are registered with the DriverManager class. 2. URL providing JDBC connection The URL defines the protocol, sub-protocol, and data source identification for connecting to the database. Written form: Protocol: Subprotocol: Data source identification Protocol: always starts with JDBC in JDBC subprotocol: is the name of the bridge driver or database management system. Data source identification: Identifies the address and connection port from which the database is located. For example: (MySql connection URL) JDBC: MySql: //localhost:3306/test? useUnicode=true&characterEncoding=gbk ; UseUnicode =true: The Unicode character set is used. If characterEncoding is set to GB2312 or GBK, this parameter must be set to true. CharacterEncoding = GBK: characterEncoding mode. To connect to a database, request java.sql.DriverManager and get the Connection object, which represents a Connection to a database. Use DriverManager’s getConnectin(String url, String username, String password) method to pass in the specified database path, database username and password to connect. For example: / / connect the MySql database, the user name and password is root url String = “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() ; SQL > create a Statement to execute a SQL Statement, you must obtain a java.sql.Statement instance. This is usually implemented through the Statement instance. 2. Execute dynamic SQL statements. It is usually implemented through a PreparedStatement instance, which I usually use. 3. Execute the database stored procedure. Usually implemented through the CallableStatement instance, never used. Statement STMT = con.createstatement (); PreparedStatement pstmt = con.prepareStatement(sql) ; CallableStatement cstmt = con.prepareCall(“{CALL demoSp(? ,?) } “); The Statement interface provides three methods for executing SQL statements: executeQuery, executeUpdate, and execute 1. ResultSet executeQuery(String sqlString) Execute the SQL statement to query the database and return a ResultSet object. 2, int executeUpdate(String sqlString) : used to execute INSERT, UPDATE or DELETE statements and SQL DDL statements, such as: Execute (sqlString): used to execute a statement that returns multiple result sets, multiple update counts, or a combination of the two. ResultSet rs = stmt.executeQuery(“SELECT * FROM…”) ); int rows = stmt.executeUpdate(“INSERT INTO …” ); boolean flag = stmt.execute(String sql) ; There are two processing results: 1. The number of records affected by the operation is returned after the update. 2. The result of executing the query is a ResultSet object. A ResultSet contains all rows that match the criteria in the SQL statement, and it provides access to the data in those rows through a set of GET methods. While (rs.next()){String name = rs.getString(“name”); String pass = rs.getString(1) ; } (columns are numbered from left to right, starting with column 1) 1, close recordset 2, close declaration 3, close 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() ; }}