java程式通過jdbc串連oracle資料庫方法

來源:互聯網
上載者:User

 1.  賦予scott使用者串連許可權:Grant connect to scott;

2.   在Myeclipse中建立java項目匯入jdbc包(classes12.jar):

       右鍵項目 bulid path -〉add external archives 選擇classes12.jar

3.   建立java檔案, lianxi01.java

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement; 

importoracle.jdbc.*;

importoracle.jdbc.driver.*;

publicclasslianxi01 {

    publicstaticvoid 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. 點擊運行,即可進行資料庫查詢

5. 註: String url一行,@100.100.100.20:1521:dbsid2需要根據實際IP和服務名進行修改。

 

相關文章

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.