
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Retrieve DataLink Object from 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 retrieve a DATALINK object an SQL table using the getURL() method of the PreparedStatement interface. This method accepts an integer value representing an index of the column in the ResultSet and returns the URL object 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 5 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'); insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values('JavaFX', 'Sarmista sharma', DATE('2018-05-01'), 'https://www.tutorialspoint.com/javafx
Following JDBC program establishes a connection with MySQl database and retrieves the contents of the tutorials_data table. Here, we are getting the URL(DATALINK) values of each record under the column tutorial_link using the getURL() method.
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://localhost/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(); } } }
Output
Connection established...... Contents of the table tutorials_data: ID: 1, Title: Java, Author: Krishna Kasyap, Submission date: 2019-09-01Tutorial link: http://www.tutorialspoint.com/java ID: 2, Title: JFreeCharts, Author: Satish Kumar, Submission date: 2019-05-01Tutorial link: https://www.tutorialspoint.com/jfreechart ID: 3, Title: Android, Author: Sai Ram, Submission date: 2019-03-01Tutorial link: https://www.tutorialspoint.com/android ID: 4, Title: Cassandra, Author: Pruthvi Raj, Submission date: 2019-04-06Tutorial link: https://www.tutorialspoint.com/cassandra ID: 5, Title: JavaFX, Author: Sarmista sharma, Submission date: 2018-05-01Tutorial link: https://www.tutorialspoint.com/javafx