http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0412cline/index.html 具体看看上面地址吧包含主方法的 Java 类 <![CDATA[ /* * Created on Nov 27, 2004 * */ package com.ibm.sqlj.main; import com.ibm.sqlj.Select; /** * @author Owen Cline * */ public class SQLJJave { public static void main(String[] args) { Select select = new Select(); select.selectEmployee("000110"); } } ]]>
<![CDATA[ /* * Created on Nov 27, 2004 * */ package com.ibm.sqlj; import java.sql.*; import sqlj.runtime.ref.*; /** * @author Owen Cline * */ public class Select { // First, load the JDBC driver static { try { Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver").newInstance (); } catch (Exception e) { System.out.println ("\n Error loading DB2 Driver...\n"); System.out.println (e); System.exit(1); } } public void selectEmployee(String empNo) { Connection con = null; DefaultContext ctx = null; try { String firstName = null; String lastName = null; // use the DB2 SAMPLE database String url = "jdbc:db2:SAMPLE";
// Get the connection con = DriverManager.getConnection(url); // Set the default context ctx = new DefaultContext(con); DefaultContext.setDefaultContext(ctx); // Lookup the employee given the employee number #sql { SELECT FIRSTNME, LASTNAME INTO :firstName, :lastName FROM EMPLOYEE WHERE EMPNO = :empNo } ; System.out.println ("Employee " + firstName + " " + lastName); ctx.close(); con.close(); } catch( Exception e ) { System.out.println (e); } } } ]]>
具体看看上面地址吧包含主方法的 Java 类
<![CDATA[
/*
* Created on Nov 27, 2004
*
*/
package com.ibm.sqlj.main;
import com.ibm.sqlj.Select;
/**
* @author Owen Cline
*
*/
public class SQLJJave {
public static void main(String[] args) {
Select select = new Select();
select.selectEmployee("000110");
}
}
]]>
<![CDATA[
/*
* Created on Nov 27, 2004
*
*/
package com.ibm.sqlj;
import java.sql.*;
import sqlj.runtime.ref.*;
/**
* @author Owen Cline
*
*/
public class Select {
// First, load the JDBC driver
static
{
try
{
Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver").newInstance ();
}
catch (Exception e)
{ System.out.println ("\n Error loading DB2 Driver...\n");
System.out.println (e);
System.exit(1);
}
}
public void selectEmployee(String empNo) {
Connection con = null;
DefaultContext ctx = null;
try
{
String firstName = null;
String lastName = null;
// use the DB2 SAMPLE database
String url = "jdbc:db2:SAMPLE";
// Get the connection
con = DriverManager.getConnection(url);
// Set the default context
ctx = new DefaultContext(con);
DefaultContext.setDefaultContext(ctx);
// Lookup the employee given the employee number
#sql { SELECT FIRSTNME, LASTNAME INTO :firstName, :lastName
FROM EMPLOYEE
WHERE EMPNO = :empNo } ;
System.out.println ("Employee " + firstName + " " + lastName);
ctx.close();
con.close();
}
catch( Exception e )
{
System.out.println (e);
}
}
}
]]>