This is an example of how to use datalinks in your applications.
/////////////////////////////////////////
// PutGetDatalinks is an example application
// that shows how to use the JDBC
// API to handle datalink database columns.
/////////////////////////////////////////
import java.sql.*;
import java.net.URL;
import java.net.MalformedURLException;
public class PutGetDatalinks {
public static void main(String[] args)
throws SQLException
{
// Register the native JDBC driver.
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
} catch (Exception e) {
System.exit(1); // Setup error.
}
// Establish a Connection and Statement with which to work.
Connection c = DriverManager.getConnection("jdbc:db2:*local");
Statement s = c.createStatement();
// Clean up any previous run of this application.
try {
s.executeUpdate("DROP TABLE CUJOSQL.DLTABLE");
} catch (SQLException e) {
// Ignore it - assume the table did not exist.
}
// Create a table with a datalink column.
s.executeUpdate("CREATE TABLE CUJOSQL.DLTABLE (COL1 DATALINK)");
// Create a PreparedStatement object that allows you to add
// a new datalink into the database. Since conversing
// to a datalink cannot be accomplished directly in the database, you
// can code the SQL statement to perform the explicit conversion.
PreparedStatement ps = c.prepareStatement("INSERT INTO CUJOSQL.DLTABLE
VALUES(DLVALUE( CAST(? AS VARCHAR(100))))");
// Set the datalink. This URL points you to an article about
// the new features of JDBC 3.0.
ps.setString (1, "http://www-106.ibm.com/developerworks/java/library/j-jdbcnew/index.html");
// Process the statement, inserting the CLOB into the database.
ps.executeUpdate();
// Process a query and obtain the CLOB that was just inserted out of the
// database as a Clob object.
ResultSet rs = s.executeQuery("SELECT * FROM CUJOSQL.DLTABLE");
rs.next();
String datalink = rs.getString(1);
// Put that datalink value into the database through
// the PreparedStatement. Note: This function requires JDBC 3.0
// support.
/*
try {
URL url = new URL(datalink);
ps.setURL(1, url);
ps.execute();
} catch (MalformedURLException mue) {
// Handle this issue here.
}
rs = s.executeQuery("SELECT * FROM CUJOSQL.DLTABLE");
rs.next();
URL url = rs.getURL(1);
System.out.println("URL value is " + url);
*/
c.close(); // Connection close also closes stmt and rs.
}
}