How to retrieve a DATALINK object from a table using JDBC?

JDBCObject Oriented ProgrammingProgramming

<p>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..</p><p>You can retrieve a DATALINK object an SQL table using the <strong>getURL()</strong> method of the <strong>PreparedStatement</strong> 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.</p><h2>Example</h2><p>Let us create a table with name tutorials_data in MySQL database using CREATE statement as shown below &minus;</p><pre class="result notranslate">CREATE TABLE tutorials_data ( &nbsp; &nbsp;tutorial_id INT PRIMARY KEY AUTO_INCREMENT, &nbsp; &nbsp;tutorial_title VARCHAR(100), &nbsp; &nbsp;tutorial_author VARCHAR(40), &nbsp; &nbsp;submission_date date, tutorial_link VARCHAR(255) );</pre><p>Now, we will insert 5 records in tutorials_data table using INSERT statements &minus;</p><pre class="result notranslate">insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values(&#39;Java&#39;, &#39;Krishna Kasyap&#39;, DATE(&#39;2019-09-01&#39;), &#39;http://www.tutorialspoint.com/java&#39;); insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values(&#39;JFreeCharts&#39;, &#39;Satish Kumar&#39;, DATE(&#39;2019-05-01 &#39;), &#39;https://www.tutorialspoint.com/jfreechart&#39;); insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values(&#39;Android&#39;, &#39;Sai Ram&#39;, DATE(&#39;2019-03-01&#39;), &#39;https://www.tutorialspoint.com/android&#39;); insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values(&#39;Cassandra&#39;, &#39;Pruthvi Raj&#39;, DATE(&#39;2019-04-06&#39;), &#39;https://www.tutorialspoint.com/cassandra&#39;); insert into tutorials_data (tutorial_title, tutorial_author, submission_date, tutorial_link) values(&#39;JavaFX&#39;, &#39;Sarmista sharma&#39;, DATE(&#39;2018-05-01&#39;), &#39;https://www.tutorialspoint.com/javafx</pre><p>Following JDBC program establishes a connection with <strong>MySQl</strong> 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.</p><pre class="prettyprint notranslate">import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class RetrievingDataLinkObjects { &nbsp; &nbsp;public static void main(String args[])throws Exception{ &nbsp; &nbsp; &nbsp; //Registering the Driver &nbsp; &nbsp; &nbsp; DriverManager.registerDriver(new com.mysql.jdbc.Driver()); &nbsp; &nbsp; &nbsp; //Getting the connection &nbsp; &nbsp; &nbsp; String mysqlUrl = &quot;jdbc:mysql://localhost/sampledatabase&quot;; &nbsp; &nbsp; &nbsp; Connection con = DriverManager.getConnection(mysqlUrl, &quot;root&quot;, &quot;password&quot;); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Connection established......&quot;); &nbsp; &nbsp; &nbsp; //Creating a Statement object &nbsp; &nbsp; &nbsp; Statement stmt = con.createStatement(); &nbsp; &nbsp; &nbsp; //Query to retrieve the contents of the tutorials_data table &nbsp; &nbsp; &nbsp; String query = &quot;select * from tutorials_data&quot;; &nbsp; &nbsp; &nbsp; //Executing the statement &nbsp; &nbsp; &nbsp; ResultSet rs = stmt.executeQuery(query); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Contents of the table tutorials_data: &quot;); &nbsp; &nbsp; &nbsp; //Retrieving the contents of the table &nbsp; &nbsp; &nbsp; while(rs.next()){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print(&quot;ID: &quot;+rs.getInt(&quot;tutorial_id&quot;)+&quot;, &quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print(&quot;Title: &quot;+rs.getString(&quot;tutorial_title&quot;)+&quot;, &quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print(&quot;Author: &quot;+rs.getString(&quot;tutorial_author&quot;)+&quot;, &quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print(&quot;Submission date: &quot;+rs.getDate(&quot;submission_date&quot;)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Retrieving the DATALINK object using the getURL() method &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print(&quot;Tutorial link: &quot;+rs.getURL(&quot;tutorial_link&quot;)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">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</pre>
raja
Updated on 30-Jul-2019 22:30:26

Advertisements