如何使用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.com/java');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('JFreeCharts', 'Satish Kumar', DATE('2019-05-01 '), 'https://tutorialspoint.com/jfreechart');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Android', 'Sai Ram', DATE('2019-03-01'), 'https://tutorialspoint.com/android');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Cassandra', 'Pruthvi Raj', DATE('2019-04-06'), 'https://tutorialspoint.com/cassandra');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('JavaFX', 'Sarmista sharma', DATE('2018-05-01'), 'https://tutorialspoint.com/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.com/java
ID: 2, Title: JFreeCharts, Author: Satish Kumar, Submission date: 2019-05-01Tutorial link: https://tutorialspoint.com/jfreechart
ID: 3, Title: Android, Author: Sai Ram, Submission date: 2019-03-01Tutorial link: https://tutorialspoint.com/android
ID: 4, Title: Cassandra, Author: Pruthvi Raj, Submission date: 2019-04-06Tutorial link: https://tutorialspoint.com/cassandra
ID: 5, Title: JavaFX, Author: Sarmista sharma, Submission date: 2018-05-01Tutorial link: https://tutorialspoint.com/javafx

更新于:2019年7月30日

139 次浏览

启动你的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.