JDBC (Java database Connectivity): A set of database programming APIs available in Java that defines a standard Java class library (in java.sql and javax.sql packages) to access the database. Using JDBC, you can write programs in Java, implement a connection to a specific database, send SQL statements to a database, implement specific operations on the database, and process the results of a database return. 。
1.JDBC Programming Six steps: (1). Select the JDBC driver type according to the database used by the application.
(2). Connect to the database and get the connection object.
(3). Create a statement object from connection.
(4). Use the Statement object to submit the SQL statement.
(5). Operation result set.
(6). Recycle database resources.
2. Select the JDBC driver type (1). LDBC-ODBC Bridge plus ODBC driver
(2). Local API and some Java-based drivers
(3). Pure Java Driver for database middleware
(4). Native protocol Pure Java driver (best performance)
Example of 3.JDBC Connection code:
Packagecom;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties;/*** JDBC Helper class * *@author * */ Public classJdbcutil {/*it is best to save with a configuration file*/ Private StaticString driver;//Drive Private StaticString URL;//Connection String Private StaticString user;//User name Private StaticString password;//Password Private StaticConnection Conn;//Connection Object Private StaticPreparedStatement pstmt;//PreparedStatement Object Private StaticResultSet rs;//ResultSet Object /*Load Database driver*/ Static{loadProperties (); Try{class.forname (driver); } Catch(ClassNotFoundException e) {Throw NewRuntimeException (e); } } PrivateJdbcutil () {}/*** Read configuration file*/ Private Static voidloadProperties () {Properties Properties=NewProperties (); InputStream in=Thread.CurrentThread (). GetClass (). getResourceAsStream ("/com/lovo/day2/jdbc.properties"); Try{properties.load (in); } Catch(IOException e) {Throw NewRuntimeException (e); } Driver= Properties.getproperty ("Driver"); URL= Properties.getproperty ("url"); User= Properties.getproperty ("User"); Password= Properties.getproperty ("Password"); } /*** Get Database connection * *@returnConnection Object*/ Public StaticConnection getconnection () {Try { returnconn =drivermanager.getconnection (URL, user, password); } Catch(SQLException e) {Throw NewRuntimeException (e); } } /*** Release ResultSet resources * *@paramRS*/ Public Static voidClose (ResultSet rs) {Try { if(NULL!=rs) rs.close (); } Catch(SQLException e) {Throw NewRuntimeException (e); } } /*** Release ResultSet resources * *@paramRS*/ Public Static voidClose (Statement stmt) {Try { if(NULL!=stmt) stmt.close (); } Catch(SQLException e) {Throw NewRuntimeException (e); } } /*** Release ResultSet resources * *@paramRS*/ Public Static voidClose (Connection conn) {Try { if(NULL!=conn) conn.close (); } Catch(SQLException e) {Throw NewRuntimeException (e); } } /*** FREE All resources*/ Public Static voidClose () {Try { if(NULL! = RS &&!rs.isclosed ()) {Rs.close (); } RS=NULL; } Catch(SQLException e) {Throw NewRuntimeException (e); } finally { Try { if(NULL! = pstmt &&!pstmt.isclosed ()) {Pstmt.close (); } pstmt=NULL; } Catch(SQLException e) {Throw NewRuntimeException (e); } finally { Try{SYSTEM.OUT.PRINTLN (conn); if(NULL! = Conn &&!conn.isclosed ()) {Conn.close (); } Conn=NULL; } Catch(SQLException e) {Throw NewRuntimeException (e); } } } } /*** Perform additions and deletions * *@paramSQL * SQL statement to be executed *@paramparams * The parameters of a placeholder in an SQL statement *@returnnumber of rows affected*/ Public Static intexecuteupdate (String sql, Object ... params) {Try { if(NULL==conn) getconnection (); Pstmt=conn.preparestatement (SQL); if(NULL! = params) {//set parameters for the SQL statement placeholder for(inti = 0; i < params.length; i++) {Pstmt.setobject (i+ 1, Params[i]); } } returnpstmt.executeupdate (); } Catch(SQLException e) {Throw NewRuntimeException (e); } } /*** Implementation Query * *@paramSQL * Query SQL statement to execute *@paramparams * The parameters of a placeholder in an SQL statement *@returnquery result set ResultSet object*/ Public StaticResultSet executeQuery (String sql, Object ... params) {Try { if(NULL==conn) getconnection (); Pstmt=conn.preparestatement (SQL); if(NULL! = params) {//set parameters for the SQL statement placeholder for(inti = 0; i < params.length; i++) {Pstmt.setobject (i+ 1, Params[i]); } } returnrs =Pstmt.executequery (); } Catch(SQLException e) {Throw NewRuntimeException (e); } }}
Java Database programming