如何使用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:///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
廣告