Java Operations Oracle Database sample _java

Source: Internet
Author: User

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

1, right key project-> build path-> Configuration build path, select the third "library", then click "Add External Jar", 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 Oracle database connection code example:

Copy Code code as follows:

/**
* A very standard sample code to connect to an Oracle database
*/
public void Testoracle ()
{
Connection con = null;//Create a database connection
PreparedStatement pre = null;//Create Precompiled statement objects, usually with this instead of statement
ResultSet result = null;//Create a results set object
Try
{
Class.forName ("Oracle.jdbc.driver.OracleDriver");//Load Oracle Driver
System.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 native address, XE is the default database name for Lite Oracle
String user = "System";//user name, system default account name
String Password = "147";//the password you choose to install
con = drivermanager.getconnection (url, user, password);//Get connection
SYSTEM.OUT.PRINTLN ("Connection successful!") ");
String sql = "SELECT * from student where name=?"; /precompiled statement, "? "represents the parameter
Pre = con.preparestatement (SQL);//Instantiating precompiled statements
Pre.setstring (1, "Liu Xianhan");/set parameter, the preceding 1 represents the index of the parameter, not the index of the column name in the table
result = Pre.executequery ()//Execute query, note that no additional parameters are required in parentheses
while (Result.next ())
When the result set is not empty
System.out.println ("School No.:" + result.getint ("id") + "name:"
+ result.getstring ("name"));
}
catch (Exception e)
{
E.printstacktrace ();
}
Finally
{
Try
{
Close several of the above objects individually, because it affects performance and consumes resources because it is not closed
Note the order of closure, the last to use the first shutdown
if (Result!= null)
Result.close ();
if (pre!= null)
Pre.close ();
if (Con!= null)
Con.close ();
SYSTEM.OUT.PRINTLN (The database connection is closed!) ");
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}

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.