- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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)
- Related Articles
- How to retrieve a DATALINK object from a table using JDBC?
- How to insert Binary data into a table using JDBC?
- How to insert a row into a ResultSet object using JDBC?
- How to insert a record into a table in a database using JDBC API?
- How to insert data into a table with auto-incremented columns using JDBC?
- How to insert Date value into table in JDBC?
- How to insert/store JSON array into a database using JDBC?
- How to insert rows into a ResultSet in JDBC?
- How to convert a String into a Date object using JDBC API?
- How do we insert/store a file into MySQL database using JDBC?
- How to insert data into a CachedRowSet in JDBC? Explain?
- What is the MySQL datatype to store DATALINK object in JDBC
- How to sql insert items from a list or collection in to table using JDBC?
- How to insert new row into table at a certain index using jQuery?
- How to create a table in JDBC using another table?
