要连接数据库,您需要
选择所需的数据库,使用DriverManager类的registerDriver()方法或名为Class的类的forName()方法注册特定数据库的Driver类。
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接口的以下方法:
此方法返回一个布尔值,当结果集对象包含更多行时为true,否则为false。
这些方法用于检索每一行中的列值。如果特定的列是整数类型,则需要使用方法getInt()
;如果它是字符串类型,则需要使用method getString()
。
//执行查询 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{ //注册驱动程序 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //获得连接 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{ //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/testdb"; Connection mySqlCon = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connected to MySQL database......"); return mySqlCon; } public void ExtractDataFromMySQL(Connection con) throws SQLException { //创建语句 Statement stmt = con.createStatement(); //执行查询 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 { //创建语句 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(); //连接到Oracle- Connection oracleCon = obj.connectToOracle(); //从Oracle提取数据 obj.ExtractDataFromOracle(oracleCon); //连接到MySQL- Connection msqlCon = obj.connectToMySQL(); //从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