DB2的JDBC连接测试时间:2021-04-28 16:00:01 简单的Java测试类 package com.common.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * @title 数据库操作 * @description 数据库操作 * @since Java5 */public class JDBCUtil {private JDBCUtil() {}static {try {String driver = "com.ibm.db2.jcc.DB2Driver";// 装载驱动Class.forName(driver);} catch (ClassNotFoundException e) {throw new ExceptionInInitializerError(e);} catch (Exception ex) {throw new RuntimeException("database driver load error");}}/** * 获取数据库连接 * @param url url * @param user 用户名 * @param password 密码 * @return 数据库连接 * @throws SQLException */public static Connection getConnection(String url, String user, String password) throws SQLException {return DriverManager.getConnection(url, user, password);}/** * 释放数据库相关对象 * * @param rs 结果集 * @param st 声明 * @param conn 连接 */public static void free(ResultSet rs, Statement st, Connection conn) {try {if (rs != null) {rs.close();}} catch (SQLException e) {e.printStackTrace();} finally {try {if (st != null) {st.close();}} catch (SQLException e) {e.printStackTrace();} finally {if (conn != null) {try {if (!conn.isClosed()) {conn.close();}} catch (SQLException e) {e.printStackTrace();}}}}}public static void main(String[] args) {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = JDBCUtil.getConnection("jdbc:db2://localhost:50000/CONTENT", "administrator", "abcde");st = conn.createStatement();String sql = "select * from user_information";rs = st.executeQuery(sql);while(rs.next()) {System.out.println(rs.getString(1));}System.out.println("test for connection correct");} catch (SQLException e) {// 添加日志记录该异常e.printStackTrace();System.out.println("test for connection exception");} finally {JDBCUtil.free(rs, st, conn);}}} 1 test for connection correct