WEB开发网
开发学院数据库DB2 在使用 SQLJ 和 JDBC 时获取最优的 DB2 性能 阅读

在使用 SQLJ 和 JDBC 时获取最优的 DB2 性能

 2008-09-16 16:29:32 来源:WEB开发网   
核心提示: import java.sql.Connection;import java.sql.DriverManager;import sqlj.runtime.ref.DefaultContext;public class SqlJDemo{static{try{//register the D

import java.sql.Connection;
import java.sql.DriverManager;
import sqlj.runtime.ref.DefaultContext;
   
public class SqlJDemo
{
 static
 {
  try
  {
   //register the DB2 JDBC driver with DriverManager
   Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
   
 static Connection con;
 static DefaultContext ctx;
   
 #sql iterator NamedIterator (String empno, String projno, int actno );
 #sql iterator PosIterator (String, String, int);
   
 public static void main (String args[]){
   SqlJDemo demoApp = new SqlJDemo();
   demoApp.makeConnection();
   demoApp.insertData();
   try {
    //Commit any remaining transactions
    #sql [ctx] {commit};
    ctx.close();
    con.close();
   } catch(java.sql.SQLException sqle) {
    sqle.printStackTrace();
   }
 }
   
 public void makeConnection()
 {
  try
  {
   // get context and open connection to DB2
   ctx = DefaultContext.getDefaultContext();
   if (ctx == null)
   {
    //construct the URL (sample is the database name)
    String url = "jdbc:db2://localhost:50000/SAMPLE";
   
    //connect to the 'sample' database with user ID and password
    con = DriverManager.getConnection(url, "myusername", "mypassword");
    con.setAutoCommit(false); 
   
    ctx = new DefaultContext(con);
    DefaultContext.setDefaultContext(ctx);
   }
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
   
 public void insertData()
 {
  try
  {
   String empno = null;
   String projno = null;
   int actno;
   
   // Set values for host variables
   empno = "01";
   projno = "10";
   actno = 1;
   
   // insert data into EMP_ACT
   #sql [ctx] {INSERT INTO EMP_ACT (EMPNO, PROJNO, ACTNO)
                 values (:empno, :projno, :actno)};
   // commit inserted record
   #sql [ctx] {commit};
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
   
}

上一页  1 2 3 4 5 6 7 8 9 10  下一页

Tags:使用 SQLJ JDBC

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接