Java-connected code for Oracle

Source: Internet
Author: User

The latest to do a connection to Oracle code, to match the environment variable: classpath=.; E:j2sdk14lib Ools.jar; E:oracleora90jdbclibclasses12.zip

The tested computer is already installed with Oracle clients and can be connected with sqlplus.

/*

* This sample shows the "All" names from the EMP table

*

* It uses the JDBC THIN driver. The same program in the

* Oci8 samples Directory to "to" and "use" the other drivers.

*///You need to import the java.sql package to use JDBC

Import java.sql.*; Class Test

{

public static void Main (String args [])

Throws SQLException

{

Load the Oracle JDBC driver

Drivermanager.registerdriver (New Oracle.jdbc.OracleDriver ());

/* try{

Class.forName ("Oracle.jdbc.driver.OracleDriver");

}catch (Exception e) {

System.out.println ("No driver!");

}

*/

Connect to the database

You are must put a database name after the @ sign in the connection URL.

Can use either the fully specified sql*net syntax or a short cut

Syntax as::. The example uses the short cut syntax.

String url = "Jdbc:oracle:thin:@172.28.31.85:1521:yikatong";

String userName = "Scott";

String PassWord = "Tiger"; if (args.length > 0) url = args[0];

if (Args.length > 1) userName = args[1];

if (Args.length > 2) password = args[2];

System.out.println (URL);

System.out.println (UserName);

SYSTEM.OUT.PRINTLN (password);

Connection conn =

Drivermanager.getconnection (URL, userName, password); Create a Statement

Statement stmt = Conn.createstatement (); Select the ename column from the EMP table

ResultSet RSet = Stmt.executequery ("SELECT * from Test"); Iterate through the result and print the employee names

while (Rset.next ())

System.out.println (rset.getstring (1));

}

}

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.