How to insert a DATALINK object into a table using JDBC?

A DATALINK object represents an URL value which refers to an external resource (outside the current database/data source), which can be a file, directory etc.

You can store a DATALINK into an SQL table using the setURL() method of the PreparedStatement interface. This method accepts an integer value representing an index of the bind variable, an URL object and, inserts the given URL object in the column represented by the bind variable in the specified index.


Let us create a table with name tutorials_data in MySQL database using CREATE statement as shown below −

CREATE TABLE tutorials_data (
   tutorial_title VARCHAR(100),
   tutorial_author VARCHAR(40),
   submission_date date, tutorial_link VARCHAR(255)

Now, we will insert 4 records in tutorials_data table using INSERT statements −

insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Java', 'Krishna Kasyap', DATE('2019-09-01'), '');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('JFreeCharts', 'Satish Kumar', DATE('2019-05-01 '), '');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Android', 'Sai Ram', DATE('2019-03-01'), '');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date,tutorial_link) values('Cassandra', 'Pruthvi Raj', DATE('2019-04-06'), '');

Following JDBC program inserts a new record into the tutorials_data table. Here, we are inserting the DATALING object into the tutorial_link column of the tutorials_data table using the setURL() method of the PreparedStatement.

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class StoringDataLinkObjects {
   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://localhost/sampledatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Query to insert data into tutorials_data table
      String query = "insert into tutorials_data ("
         + "tutorial_title, "
         + "tutorial_author, "
         + "submission_date, "
         + "tutorial_link) values(?, ?, ?, ? )";
      //Creating the preparedStatement object
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "JavaFX");
      pstmt.setString(2, "Sarmista sharma");
      pstmt.setDate(3, new Date(1525169078000L));
      //Setting the URL value
      pstmt.setURL(4, new URL(""));
      System.out.println("Record inserted........");


Connection established......
Record inserted........


If you get the contents of the tutorials_data table, you observe the newly inserted record as −

mysql> select * from tutorials_data;
| tutorial_id | tutorial_title | tutorial_author | submission_date | tutorial_link                             |
| 1           | Java           | Krishna Kasyap  | 2019-09-01      |        |
| 2           | JFreeCharts    | Satish Kumar    | 2019-05-01      | |
| 3           | Android        | Sai Ram         | 2019-03-01      |    |
| 4           | Cassandra      | Pruthvi Raj     | 2019-04-06      |  |
| 5           | JavaFX         | Sarmista sharma | 2018-05-01      |     |
7 rows in set (0.00 sec)