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

JDBCObject Oriented ProgrammingProgramming

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.

Example

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

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)
);

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'), 'http://www.tutorialspoint.com/java');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('JFreeCharts', 'Satish Kumar', DATE('2019-05-01 '), 'https://www.tutorialspoint.com/jfreechart');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('Android', 'Sai Ram', DATE('2019-03-01'), 'https://www.tutorialspoint.com/android');
insert into tutorials_data (tutorial_title, tutorial_author, submission_date,tutorial_link) values('Cassandra', 'Pruthvi Raj', DATE('2019-04-06'), 'https://www.tutorialspoint.com/cassandra');

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;
import java.net.URL;
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("https://www.tutorialspoint.com/javafx"));
      pstmt.execute();
      System.out.println("Record inserted........");
   }
}

Output

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

Verification

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      | http://www.tutorialspoint.com/java        |
| 2           | JFreeCharts    | Satish Kumar    | 2019-05-01      | https://www.tutorialspoint.com/jfreechart |
| 3           | Android        | Sai Ram         | 2019-03-01      | https://www.tutorialspoint.com/android    |
| 4           | Cassandra      | Pruthvi Raj     | 2019-04-06      | https://www.tutorialspoint.com/cassandra  |
| 5           | JavaFX         | Sarmista sharma | 2018-05-01      | https://www.tutorialspoint.com/javafx     |
+-------------+----------------+-----------------+-----------------+-------------------------------------------+
7 rows in set (0.00 sec)
raja
Published on 04-Jul-2019 11:42:50
Advertisements