How to connect a java program to an oracle database through jdbc

Source: Internet
Author: User

 1. Grant scott user connection permissions: Grant connect to scott;

2. Create a java project in Myeclipse to import the jdbc package (classes12.jar ):

Right-click the project bulid path-> add external archives and select classes12.jar.

3. Create a java file, lianxi01.java

ImportJava. SQL. Connection;

ImportJava. SQL. ResultSet;

ImportJava. SQL. SQLException;

ImportJava. SQL. Statement;

ImportOracle. jdbc.*;

ImportOracle. jdbc. driver.*;

PublicclassLianxi01 {

PublicstaticvoidMain (String [] args)ThrowsSQLException, ClassNotFoundException {

//TODOAuto-generated method stub

Oracle. jdbc. OracleDriver.IsDebug();

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

Java. SQL. DriverManager.RegisterDriver(NewOracle. jdbc. driver. OracleDriver ());

//

String url = "jdbc: oracle: thin: @ 100.100.100.20: 1521: dbsid2 ";

String user = "scott ";

String password = "tiger ";

StringSQL= "Select table_name from user_tables ";

String sql2 = "select * from dept ";

//

Connection con = java. SQL. DriverManager.GetConnection(Url, user, password );

Statement stmt = con. createStatement ();

ResultSet rs = (java. SQL. Statement) stmt1_.exe cuteQuery (sql2 );

While(Rs. next ()){

// String s = rs. getString (2 );

System.Out. Println (rs. getString (1) + "" + rs. getString (2) + "" + rs. getString (3 ));

}

}

}

4. Click Run to query the database.

5. Note: A String url line, @ 100.100.100.20: 1521: dbsid2Modify the IP address and service name.

 

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.