Java programs Connect Oracle database methods through JDBC

Source: Internet
Author: User

1. give Scott users access to the connection:Grant Connect to Scott;

2. Create a new Java project in MyEclipse to import the JDBC Package (Classes12.jar):

Right-click Project bulid path-〉add External Archives Select Classes12.jar

3. Create a new Java file, Lianxi01.java

Import java.sql.Connection;

Import Java.sql.ResultSet;

Import java.sql.SQLException;

Import java.sql.Statement;

Import oracle.jdbc. *;

Import oracle.jdbc.driver. *;

Public class lianxi01 {

public staticvoid main (string[] args)throws SQLException, classnotfoundexception{

//TODO auto-generated method Stub

Oracle.jdbc.OracleDriver. Isdebug ();

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

Java.sql.DriverManager. Registerdriver (new oracle.jdbc.driver.OracleDriver ());

        //

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

String user="Scott";

String password="Tiger";

String sql="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) stmt). ExecuteQuery (SQL2);

while(Rs.next ()) {

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

System. out . println (rs.getstring (1) + "" + rs.getstring (2) +" "+ rs.getstring (3)) ;

        }

    }

}

4. Click to run, you can query the database

5. Note: a String URL line,@100.100.100.20:1521:dbsid2 needs to be based on the actual IP And the service name is modified.

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.