JDBC Learning notes (i)

Source: Internet
Author: User

     Public Static voidMain (string[] args) {ResultSet rs=NULL; Statement stmt=NULL; Connection Conn=NULL; Try {                           /*** 1. Load the JDBC driver: * Load target database driver to JVM * After a successful load, an instance of the driver class is registered to the DriverManager class * Oracle-driver:oracle.jdbc.driver.oracledriver * mysql-driver:com.mysql.jdbc.driver * SQL Serve              R-driver:com.microsoft.sqlserver.jdbc.sqlserverdriver * Db2-driver:com.ibm.db2.jdbc.app.db2driver */Class.forName ("Oracle.jdbc.driver.OracleDriver"); /**            * 2. URL to provide JDBC connection * The URL defines the protocol, sub-protocol, data source identity * format when connecting to the database: Protocol: Sub-Protocol: Data Source Identity * Protocol: in JDBC             Always start with 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. * oracle-url:jdbc:oracle:thin:@< database is located on the server IP address ><: Port ><: Database name > Default port: 1521 * Jdbc:oracle:thi N:@127.0.0.1:1521:ORCL * mysql-url:jdbc:mysql://< Database is located on the server IP address ><: Port >/<: Database name >[? parameter name 1][= argument value 1][ & parameter name 2][= argument value 2] default port: 3306 * jdbc:mysql://localhost/mydb?user=soft&password=soft1234&useunicode=t Rue&characterencoding=8859_1 * SQL server-url:jdbc:microsoft:sqlserver://< database is located on the server IP address ><: Port;>            <database= database name > Default port: 1433 * jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=test * db2-url:jdbc:db2://< database is located on the server IP address ><: Port >/< database name > Default port: * jdbc:db2://127.0.0.1:5000/s Ample * Reference:http://www.open-open.com/lib/view/open1404997846888.html            */String URL= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"; /*** 3. Create a connection to a database * request to Java.sql.DriverManager and get the connection object that represents the connection to a database * using Driverma The Nager getconnectin (string URL, string username, string password) method passes in the path of the specified database to be connected, the user name of the database, and the password to obtain*/Conn= Drivermanager.getconnection (URL, "Scott", "Tiger"); /*** 4. Get database Operations Object * Get Java.sql.Statement instances, statement instances into 3 classes * One, execute static SQL statements.             Usually implemented by Statement instances, Statement stmt = Con.createstatement (); * Second, execute the dynamic SQL statement. Typically implemented through PreparedStatement instances.             PreparedStatement pstmt = con.preparestatement (sql); * Third, execute the database stored procedure.            Typically implemented through CallableStatement instances. */stmt=conn.createstatement (); /**A . Define the SQL *statement interface to execute provides three ways to execute SQL statements: ExecuteQuery, executeupdate, and execute * one, Re             Sultset executeQuery (String sqlString): Executes the SQL statement that queries the database and returns a result set (ResultSet) object.             * ResultSet rs = stmt.executequery ("SELECT * from ..."); * Two, int executeupdate (String sqlString): Used to execute INSERT, UPDATE, or DELETE statements and SQL DDL statements, such as CREATE table and drop table, etc. * I             NT rows = stmt.executeupdate ("INSERT into ...");             * Third, execute (sqlString): Used to perform statements that return multiple result sets, multiple update counts, or combinations of both * Boolean flag = Stmt.execute (String sql); */String SQL= "SELECT * FROM dept"; RS=stmt.executequery (SQL); /*** 6, processing results * There are two processing results: * One, the implementation of the update returned is the number of records affected by this operation. * Second, the execution of the query returns the result is a ResultSet object.             ResultSet contains all rows that conform to the conditions in the SQL statement, and it provides access to the data in those rows through a set of Get methods. */            while(Rs.next ()) {//System.out.println (rs.getstring ("Deptno"));System.out.println (rs.getstring (1)); }            } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } finally {                         /*** 7, close the JDBC object, release resources * Close order and declaration order opposite: * 1, close the Recordset, 2, close the Declaration, 3, close the Connection object*/           Try {              if(rs! =NULL) {rs.close (); RS=NULL; }              if(stmt! =NULL) {stmt.close (); stmt=NULL; }              if(Conn! =NULL) {conn.close (); Conn=NULL; }             } Catch(SQLException e) {e.printstacktrace (); }            }      }

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.