为 DB2 XML 数据开发 Java 应用程序
2009-11-12 00:00:00 来源:WEB开发网清单 2 包含一个 Helper 类,其中具有用于建立和关闭 DB2 数据库连接的方法。
清单 2. 用于获得和释放数据库连接的 Helper 类
public class Conn {
// for simplicity, I've hard-coded account and URL data.
private static String user = "user1";
private static String pwd = "mypassword";
private static String url = "jdbc:db2:test";
// this method gets a database connection
public static Connection getConn(){
Connection conn=null;
// load the appropriate DB2 driver and
// get a connection to the “test” database
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn = DriverManager.getConnection(url, user, pwd);
. . .
}
catch (Exception e) { e.printStackTrace(); }
return conn;
} // end getConn();
// this method closes a database connection
public static void closeConn(Connection conn){
try {
if(conn == null) { return; }
conn.close();
}
catch (Exception e) { e.printStackTrace(); }
finally {
try { conn.close(); }
catch (Exception e) { }
}
} // end closeConn();
} // end class
更多精彩
赞助商链接