如何编写一个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://127.0.0.1/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+ 阅读量

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告