Oracle JDBC Demo

Source: Internet
Author: User
Tags rowcount stmt

Two different ways:

Thin is a thin client connection that does not require an Oracle client to be installed, and only requires a JDBC-driven jar package in the classpath. Thin is a purely Java-written Oracle database provider.
OCI is a way for a rich client to connect using this connection to install an Oracle client. OCI is the acronym for Oracle Call Interface, which provides access for Oracle, which is the use of Java to invoke the native Oracle client and then access the database, with the advantage of being fast, but requiring the installation and configuration of the database.

Switching from the thin driver to the OCI driver is simple in configuration, simply java:oracle:thin the connection string: @hostip:p ort: the instance name is changed to Java:oracle:[email protected] Local service name.

such as: From Jdbc:oracle:thin:@10.1.1.2:1521:xe
Change into
Jdbc:oracle:oci8: @xe

Call StoredProcedure using:

CallableStatement cstmt = Con.preparecall ("Call Spname (?,?)");

Get the metadata information for the database:

DatabaseMetaData Dbmd = Con.getmetadata ();
Gets the metadata information for the result set:
ResultSetMetaData RSMD = Rs.getmetadata ();

A statement constructor can contain 3 parameters:

    • resultSetType, its value includes: ResultSet.TYPE_FORWARD_ONLY , ResultSet.TYPE_SCROLL_INSENSITIVE orResultSet.TYPE_SCROLL_SENSITIVE,默认情况下,该参数的值是ResultSet.TYPE_FORWARD_ONLY
    • resultSetConcurrency, its value includes: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE , by default, the value of the parameter is ResultSet.CONCUR_READ_ONLY .
    • resultSetHoldability, its value includes: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT .

When you get the data set, you can set Resultset.setfetchsize to avoid outofmemory errors.

About JDBC database connection, the same database connection, can not do multiple things at the same time, must be serialized? Or is this determined by the isolation level of the database?

Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.sql.SQLException; Public classJdbcdemo {//CREATE TABLE MyTest (key varchar2 (+), value varchar2 ());         Public Static FinalString Oracle_driver_class = "Oracle.jdbc.driver.OracleDriver";  Public    Static voidMain (string[] args) {System.out.println ("Hello jdbc"); FinalConnection conn = Getconn (false); //Selectdemo ("SELECT * from MyTest"); //Insertdemo (conn,10000,1000); //Selectdemo ("SELECT * from MyTest");            /*New Thread () {public void run () {try {Insertdemo                    (conn,100,10);                    } catch (SQLException e) {e.printstacktrace ();                            }}}.start ();                    New Thread () {public void run () {try {Insertdemo (conn,100,10);                    } catch (SQLException e) {e.printstacktrace ();                }}}.start (); */            NewThread () { Public voidrun () {Try{Selectdemo (conn,"SELECT * FROM MyTest"); } Catch(SQLException e) {e.printstacktrace ();                }}}.start (); Try{Thread.Sleep (1000);                Conn.commit (); } Catch(Exception e) {e.printstacktrace (); }             }        StaticConnection Getconn (Booleanautocommit) {Connection conn=NULL; Try{class.forname (oracle_driver_class); //Both method, use thin (pure Java not need Oracle client installed)//or OCI (need Oracle client installed)conn = Drivermanager.getconnection ("Jdbc:oracle:thin:@192.168.99.105:1521:xe", "System", "student");        Conn.setautocommit (autocommit); } Catch(ClassNotFoundException e) {e.printstacktrace (); }        Catch(SQLException sqle) {sqle.printstacktrace (); }        returnConn; }        Static voidSelectdemo (Connection conn,string sql)throwsSQLException {preparedstatement stmt=conn.preparestatement (SQL); ResultSet RS=Stmt.executequery (); Rs.setfetchsize (1);//It is useful to set this value when the resultset is very largeResultSetMetaData RSMD =Rs.getmetadata ();  for(intIndex=1;index<=rsmd.getcolumncount (); index++) System.out.print (rsmd.getcolumnname (index)+ "\ T");        System.out.println ();  while(Rs.next ()) { for(intIndex=1;index<=rsmd.getcolumncount (); index++) System.out.print (rs.getstring (index)+ "\ T");            System.out.println (); }     }        Static voidInsertdemo (Connection Conn,intRowCountintBatchcount)throwsSQLException {String SQL= "INSERT into mytest (Key,value) VALUES (?,?)"; PreparedStatement stmt=conn.preparestatement (SQL);  for(inti=1;i<=rowcount;i++) {stmt.setstring (1, "Key" +i); Stmt.setstring (2, "Value" +i+ "" +Thread.CurrentThread (). GetName ());            Stmt.addbatch (); if(i% Batchcount = = 0) Stmt.executebatch ();    } stmt.executebatch (); } }

Oracle JDBC Demo

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.