如何使用JDBC API从数据库现有表中检索记录?


A. 你可以使用SELECT查询读取/获取表中记录的内容。这将以结果表的形式返回数据,这些结果表称为结果集。

语法

SELECT column1, column2, columnN FROM table_name;
Or,
SELECT * FROM table_name;

要使用JDBC API检索表中某一行的内容,你需要:

  • 注册驱动程序:使用**DriverManager**类的**registerDriver()**方法注册驱动程序类。将驱动程序类名作为参数传递给它。

  • 建立连接:使用**DriverManager**类的**getConnection()**方法连接到数据库。将URL(字符串)、用户名(字符串)、密码(字符串)作为参数传递给它。

  • 创建语句:使用**Connection**接口的**createStatement()**方法创建一个Statement对象。

  • 执行查询:使用Statement接口的executeQuery()方法执行查询。

示例

假设我们在MySQL数据库中有一个名为mydatabase的数据库,其中包含一个名为customers的表,其中有12条记录,如下所示:

+----+-----------+------+---------+----------------+
| ID | NAME      | AGE  | SALARY | ADDRESS         |
+----+-----------+------+---------+----------------+
| 1  | Amit      | 25   | 3000.00 | Hyderabad      |
| 2  | Kalyan    | 27   | 4000.00 | Vishakhapatnam |
| 3  | Renuka    | 30   | 5000.00 | Delhi          |
| 4  | Archana   | 24   | 1500.00 | Mumbai         |
| 5  | Koushik   | 30   | 9000.00 | Kota           |
| 6  | Hardik    | 45   | 6400.00 | Bhopal         |
| 7  | Trupthi   | 33   | 4360.00 | Ahmedabad      |
| 8  | Mithili   | 26   | 4100.00 | Vijayawada     |
| 9  | Maneesh   | 39   | 4000.00 | Hyderabad      |
| 10 | Rajaneesh | 30   | 6400.00 | Delhi          |
| 11 | Komal     | 29   | 8000.00 | Ahmedabad      |
| 12 | Manyata   | 25   | 5000.00 | Vijayawada     |
+----+-----------+------+---------+----------------+

下面的JDBC程序建立与MySQL的连接,检索customers表中的所有记录并显示它们。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ReadRecordsExample {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to retrieve records
      String query = "Select * from Customers";
      //Executing the query
      ResultSet rs = stmt.executeQuery(query);
      System.out.println("Contents of the Customers table:");
      while(rs.next()) {
         System.out.print("ID: "+rs.getInt("ID")+", ");
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Age: "+rs.getInt("Age")+", ");
         System.out.print("Salary: "+rs.getInt("Salary")+", ");
         System.out.print("Address: "+rs.getString("Address"));
         System.out.println();
      }
   }
}

输出

Connection established......
Contents of the Customers table:
ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad
ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam
ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi
ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Mumbai
ID: 5, Name: Koushik, Age: 30, Salary: 9000, Address: Kota
ID: 6, Name: Hardik, Age: 45, Salary: 6400, Address: Bhopal
ID: 7, Name: Trupthi, Age: 33, Salary: 4360, Address: Ahmedabad
ID: 8, Name: Mithili, Age: 26, Salary: 4100, Address: Vijayawada
ID: 9, Name: Maneesh, Age: 39, Salary: 4000, Address: Hyderabad
ID: 10, Name: Rajaneesh, Age: 30, Salary: 6400, Address: Delhi
ID: 11, Name: Komal, Age: 29, Salary: 8000, Address: Ahmedabad
ID: 12, Name: Manyata, Age: 25, Salary: 5000, Address: Vijayawada

更新于:2019年7月30日

843 次浏览

启动你的职业生涯

通过完成课程获得认证

开始
广告