数据库操作:java连接DB2实例代码演示

package com.qiaole.conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB2 {
 public static void main(String[] args) {
  try {
   Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
   // COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver
   String url = "jdbc:db2://localhost:50000/sample";
   Connection conn = DriverManager.getConnection(url, "happy",
     "qiaole1");
   String sql = "SELECT * FROM HAPPY.TEST";
   PreparedStatement psStatement = conn.prepareStatement(sql);
   ResultSet rSet = psStatement.executeQuery();
   System.out.println("\tDB2查询数据");
   while (rSet.next()) {
    System.out.println(rSet.getInt(1) + "\t" + rSet.getString(2)
      + "\t" + rSet.getInt(3));
   }
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }

 }
}

 

需要添加的包:

posted on 2010-07-09 09:24  快乐每一天Joe  阅读(848)  评论(0编辑  收藏  举报

导航