Java 連線 DB2的工具類

passport_daizi發表於2012-05-29
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class CONN {
    public static Connection createConn()
    {
        Connection conn=null;
        try {
            //Class.forName("com.mysql.jdbc.Driver");       
            Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
            String url = "jdbc:db2://127.0.0.1:50000/sample";
            Properties props = new Properties();
            props.setProperty("user", "db2admin");
            props.setProperty("password", "ibmdb2");
            conn=DriverManager.getConnection(url,props);
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static PreparedStatement prepare(Connection conn,String sql)
    {
        PreparedStatement stat=null;
        try {
            stat=conn.prepareStatement(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stat;
    }
    public static void close(Connection conn)
    {
        if(conn==null) return;
        try {
            conn.close();
            conn=null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement stat)
    {
        if(stat==null) return;
        try {
            stat.close();
            stat=null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(ResultSet rs)
    {
        if(rs==null) return;
        try {
            rs.close();
            rs=null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Connection conn = createConn();
        System.out.println(conn);
        String sql="(select * from DB2ADMIN.user where id not in ( select id from DB2ADMIN.user order by id asc fetch first 10 rows only  ) order by id asc  ) fetch first 10 rows only";
        PreparedStatement ps;
        try {
            ps = conn.prepareStatement(sql);
            try {
                ResultSet rs=ps.executeQuery();
                while(rs.next())
                {
                    //System.out.println(rs.getString("userCount"));//(1);//.getString("name"));
                    System.out.println(rs.getString("id"));
                    System.out.println(rs.getString("name"));
                    System.out.println(rs.getString("password"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn.close();
            ps.close();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

相關文章