如何使用JDBC從表中檢索DATALINK物件?


DATALINK物件表示一個URL值,該值引用外部資源(當前資料庫/資料來源之外),可以是檔案、目錄等。

您可以使用**PreparedStatement**介面的**getURL()**方法從SQL表中檢索DATALINK物件。此方法接受一個整數,表示ResultSet中列的索引,並返回指定索引中的URL物件。

示例

讓我們使用如下所示的CREATE語句在MySQL資料庫中建立一個名為tutorials_data的表:

CREATE TABLE tutorials_data (
   tutorial_id INT PRIMARY KEY AUTO_INCREMENT,
   tutorial_title VARCHAR(100),
   tutorial_author VARCHAR(40),
   submission_date date, tutorial_link VARCHAR(255)
);

現在,我們將使用INSERT語句在tutorials_data表中插入5條記錄:

insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Java', 'Krishna Kasyap', DATE('2019-09-01'), 'https://tutorialspoint.tw/java');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('JFreeCharts', 'Satish Kumar', DATE('2019-05-01 '), 'https://tutorialspoint.tw/jfreechart');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Android', 'Sai Ram', DATE('2019-03-01'), 'https://tutorialspoint.tw/android');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Cassandra', 'Pruthvi Raj', DATE('2019-04-06'), 'https://tutorialspoint.tw/cassandra');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('JavaFX', 'Sarmista sharma', DATE('2018-05-01'), 'https://tutorialspoint.tw/javafx

下面的JDBC程式與**MySQL**資料庫建立連線,並檢索tutorials_data表的內容。在這裡,我們使用getURL()方法獲取每一記錄下tutorial_link列的URL(DATALINK)值。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class RetrievingDataLinkObjects {
   public static void main(String args[])throws Exception{
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///sampledatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Query to retrieve the contents of the tutorials_data table
      String query = "select * from tutorials_data";
      //Executing the statement
      ResultSet rs = stmt.executeQuery(query);
      System.out.println("Contents of the table tutorials_data: ");
      //Retrieving the contents of the table
      while(rs.next()){
         System.out.print("ID: "+rs.getInt("tutorial_id")+", ");
         System.out.print("Title: "+rs.getString("tutorial_title")+", ");
         System.out.print("Author: "+rs.getString("tutorial_author")+", ");
         System.out.print("Submission date: "+rs.getDate("submission_date"));
         //Retrieving the DATALINK object using the getURL() method
         System.out.print("Tutorial link: "+rs.getURL("tutorial_link"));
         System.out.println();
      }
   }
}

輸出

Connection established......
Contents of the table tutorials_data:
ID: 1, Title: Java, Author: Krishna Kasyap, Submission date: 2019-09-01Tutorial link: https://tutorialspoint.tw/java
ID: 2, Title: JFreeCharts, Author: Satish Kumar, Submission date: 2019-05-01Tutorial link: https://tutorialspoint.tw/jfreechart
ID: 3, Title: Android, Author: Sai Ram, Submission date: 2019-03-01Tutorial link: https://tutorialspoint.tw/android
ID: 4, Title: Cassandra, Author: Pruthvi Raj, Submission date: 2019-04-06Tutorial link: https://tutorialspoint.tw/cassandra
ID: 5, Title: JavaFX, Author: Sarmista sharma, Submission date: 2018-05-01Tutorial link: https://tutorialspoint.tw/javafx

更新於:2019年7月30日

139 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告