Java Database programming

Source: Internet
Author: User
Tags odbc

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.