"Go" example code for a very standard Java connection to an Oracle database

Source: Internet
Author: User

Original address: http://www.cnblogs.com/liuxianan/archive/2012/08/05/2624300.html

The most basic Oracle database connection code (for oracle11g only):

1, right-click Project, build path, configuration build path, select the third item "Library", then click "Add External Jar" and select "D:\Oracle\app\oracle\product\11.2.0\server \jdbc\lib\ Ojdbc6_g.jar "(Note: D:\Oracle is the installation path for the database).

2. The following code is a very standard example of Oracle database connection code:

1 /**2 * A very standard example code for connecting to an Oracle database3  */4  Public voidtestoracle ()5 {6Connection con =NULL;//Create a connection to a database7PreparedStatement pre =NULL;//Create precompiled statement objects, usually with this instead of statement8ResultSet result =NULL;//Create a result set object9     TryTen     { OneClass.forName ("Oracle.jdbc.driver.OracleDriver");//load the Oracle driver ASystem.out.println ("Start trying to connect to the database!") "); -String url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:xe";//127.0.0.1 is a native address, XE is the default database name for Lite Oracle -String user = "System";//user name, system default account name theString Password = "147";//The password you selected when you installed the settings -con = drivermanager.getconnection (url, user, password);//Get Connections -SYSTEM.OUT.PRINTLN ("Connection Successful! "); -String sql = "SELECT * from student where name=?";//precompiled statement, "? The delegate parameter +Pre = con.preparestatement (SQL);//Instantiating precompiled Statements -Pre.setstring (1, "Liu Xianhan");//The parameter is set, and the previous 1 indicates the index of the parameter, not the index of the column name in the table +result = Pre.executequery ();//Execute the query, note that no additional arguments are required in parentheses A          while(Result.next ()) at             //When the result set is not empty -System.out.println ("School Number:" + result.getint ("id") + "name:" -+ result.getstring ("name")); -     } -     Catch(Exception e) -     { in e.printstacktrace (); -     } to     finally +     { -         Try the         { *             //close each of the above objects individually, because if you do not close it, it will affect performance and consume resources $             //Note the order of the closing, the last used first closePanax Notoginseng             if(Result! =NULL) - result.close (); the             if(Pre! =NULL) + pre.close (); A             if(Con! =NULL) the con.close (); +SYSTEM.OUT.PRINTLN ("The database connection is closed! "); -         } $         Catch(Exception e) $         { - e.printstacktrace (); -         } the     } -}

This is my copy of a piece of code, feeling written more organized than I, so specifically turn around

"Go" example code for a very standard Java connection to an Oracle database

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.