如何編寫一個JDBC程式來從多個數據庫中提取資料?


要連線到資料庫,您需要

註冊驅動程式

選擇所需的資料庫,使用 **DriverManager** 類的 **registerDriver()** 方法或名為 Class 的類的 **forName()** 方法註冊特定資料庫的驅動程式類。

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

獲取連線

透過將資料庫的 URL、資料庫中使用者的使用者名稱和密碼(以字串格式)作為引數傳遞給 **DriverManager** 類的 **getConnection()** 方法來建立連線物件。

Connection mysqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");

並且,要提取資料,您需要執行 select 查詢,如下所示:

ResultSet rs = stmt.executeQuery("Select * from Employee");

要列印 Result 物件的內容,您需要使用 ResultSet 介面的以下方法

next()

此方法返回一個布林值,當結果集物件包含更多行時為真,不包含時為假。

getXXX()

這些方法用於檢索每一行中的列值。如果特定列為整數型別,則需要使用 getInt() 方法,如果為字串型別,則需要使用 getString() 方法。

//Executing the query
ResultSet rs = stmt.executeQuery("Select *from Employee");

while(rs.next()) {
   System.out.print("Name: "+rs.getString("Name")+", ");
   System.out.print("Salary: "+rs.getInt("Salary")+", ");
   System.out.print("City: "+rs.getString("Location"));
   System.out.println();
}

示例

假設我們在 Oracle 資料庫中有一個名為 Student 的表,如下所示

NAME    AGE  PERCENTAGE
--------------------------
Raju     19    85
Raja     17    67
Mukthar  18    79
David    19    90

以及一個在 MySQL 資料庫中名為 Employee 的表,如下所示

+---------+--------+----------------+
| Name    | Salary | Location       |
+---------+--------+----------------+
| Amit    | 30000  | Hyderabad      |
| Kalyan  | 40000  | Vishakhapatnam |
| Renuka  | 50000  | Delhi          |
| Archana | 15000  | Mumbai         |
+---------+--------+----------------+

以下 JDBC 程式提取這兩個表的內容。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Multiple_DBs {
   public Connection connectToOracle() throws SQLException{
      //Registering the Driver
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

      //Getting the connection
      String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe";
      Connection oracleCon = DriverManager.getConnection(oracleUrl, "system", "password");
      System.out.println("Connected to Oracle database.....");
      return oracleCon;
   }

   public Connection connectToMySQL() throws SQLException{
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///testdb";
      Connection mySqlCon = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connected to MySQL database......");
      return mySqlCon;
   }

   public void ExtractDataFromMySQL(Connection con) throws SQLException {
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Executing the query
      ResultSet rs = stmt.executeQuery("Select *from Employee");
      System.out.println("Contents of Employee table in MySQL database: ");
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Salary: "+rs.getInt("Salary")+", ");
         System.out.print("City: "+rs.getString("Location"));
         System.out.println();
      }
      System.out.println();
   }
   public void ExtractDataFromOracle(Connection con) throws SQLException {
      //Creating the Statement
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("Select *from Student");
      System.out.println("Contents of student table in Oracle database: ");
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Age: "+rs.getInt("Age")+", ");
         System.out.print("Percentage: "+rs.getString("Percentage"));
         System.out.println();
      }
      System.out.println();
   }

   public static void main(String[] args) throws Exception {
      Multiple_DBs obj = new Multiple_DBs();
      //Connecting to Oracle
      Connection oracleCon = obj.connectToOracle();
      //Extracting data from Oracle
      obj.ExtractDataFromOracle(oracleCon);
      //Connecting to MySQL
      Connection msqlCon = obj.connectToMySQL();
      //Extracting data from MySQL
      obj.ExtractDataFromMySQL(msqlCon);
   }
}

輸出

Connected to Oracle database.....
Contents of student table in Oracle database:
Name: Raju, Age: 19, Percentage: 85
Name: Raja, Age: 17, Percentage: 67
Name: Mukthar, Age: 18, Percentage: 79
Name: David, Age: 19, Percentage: 90
Connected to MySQL database......
Contents of Employee table in MySQL database:
Name: Amit, Salary: 30000, City: Hyderabad
Name: Kalyan, Salary: 40000, City: Vishakhapatnam
Name: Renuka, Salary: 50000, City: Delhi
Name: Archana, Salary: 15000, City: Mumbai

更新於: 2019-07-30

1K+ 閱讀量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.