jdbc连接db2

时间:2014-10-10 09:29:19
【文件属性】:

文件名称:jdbc连接db2

文件大小:1KB

文件格式:TXT

更新时间:2014-10-10 09:29:19

jdbc,db2

用JDBC连接DB2的例子: 用JDBC连接DB2的例子: import java.sql.*; import COM.ibm.db2.jdbc.app.*; public class DB2Test { public static void main(String[] args) { String JDBCDriver = "COM.ibm.db2.jdbc.app.DB2Driver"; String DbSource ="jdbc:DB2:sample"; try { Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); Connection conn = DriverManager.getConnection(DbSource,"administrator","123456789"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * from sales"); if (rs.next()) System.out.println(rs.getString(1)); else System.out.println("totle:0"); rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException cnfe) { System.err.println("Unable to load driver."); cnfe.printStackTrace(); } catch (SQLException se){ System.err.println("sql error."); se.printStackTrace(); } catch (Exception E) { System.err.println("other exception"); E.printStackTrace(); } } }


网友评论