本文章分两个部分:
1、启DB2服务,便于后续的连接测试
2、java访问DB2
1、启DB2服务
网上查了一些DB2的安装,很复杂,直接去docker hub上看看有没有镜像,发现真就镜像,那么有镜像就好说了直接拉取镜像run起来就行了。
前置条件:安装好docker环境
接下来:
docker hub上DB2镜像
它给出了quick start:
直接run:
docker run -d -p 50000:50000 --name my-db2 --privileged=true -e DB2INST1_PASSWORD=password -e DBNAME=testdb -e LICENSE=accept ibmcom/db2
镜像对外端口号为50000,到时访问这个端口号就行了。
docker ps
可以看到服务起来了。(如果没有起来,查一下是不是端口号被占用了)
这部分可以参考这篇博客
2、java连接DB2
// java 连接类
public class SingleConnectionDataSource implements DataSource {
private JdbcProperty jdbcProperty;
public SingleConnectionDataSource(JdbcProperty jdbcProperty) {
this.jdbcProperty = jdbcProperty;
}
@Override
public Connection getConnection() throws SQLException {
try {
Class.forName(jdbcProperty.getDriverName());
} catch (ClassNotFoundException e) {
throw new RuntimeException("Failed to load jdbc drive class " +
jdbcProperty.getDriverName());
}
Connection con;
try {
con = DriverManager.getConnection(jdbcProperty.getUrl(),
jdbcProperty.getUserName(),
jdbcProperty.getPassword());
} catch (SQLException e) {
throw new RuntimeException("Failed to create connection " +
e.getMessage());
}
return con;
}
@Override
public Connection getConnection(String username, String password)
throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public PrintWriter getLogWriter() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public int getLoginTimeout() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public java.util.logging.Logger getParentLogger()
throws SQLFeatureNotSupportedException {
throw new UnsupportedOperationException();
}
public JdbcProperty getJdbcProperty() {
return jdbcProperty;
}
}
主类:
public class db2TestMain {
public static void main(String[] args) {
// jdbc连接都是连接到 数据库级别
String url = "jdbc:db2://127.0.0.1:50000/testdb";
String username = "db2inst1";
String password = "password";
// 注意要连接db2的 property要有schema, schema是大写的
JdbcProperty jdbcProperty =
new JdbcProperty.Builder(url, username, password)
.driverName("com.ibm.db2.jcc.DB2Driver")
.schema("DB2INST1").build();
SingleConnectionDataSource dataSource =
new SingleConnectionDataSource(jdbcProperty);
List<String> tables = listTables(dataSource);
Collections.sort(tables);
System.out.println("所有表:" + tables);
}
// 根据DataSource 来列出所有表
public List<String> listTables(DataSource dataSource) {
try (Connection conn = dataSource.getConnection()) {
List<String> tables = Lists.newArrayList();
DatabaseMetaData metaData = conn.getMetaData();
String schemaPattern = null;
String schema = ((SingleConnectionDataSource) dataSource)
.getJdbcProperty().getSchema();
if (StringUtils.isNotBlank(schema)) {
schemaPattern = schema;
}
ResultSet rs = metaData.getTables(null, schemaPattern, "%", null);
while (rs.next()) {
tables.add(rs.getString("TABLE_NAME"));
}
return tables;
} catch (SQLException e) {
throw new RuntimeException("SQL_EXECUTION_ERROR"+ " list tables");
}
}
}
一些maven依赖,主要是db2的依赖:
<dependencies>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- db2依赖 -->
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>jcc</artifactId>
<version>11.5.0.0</version>
</dependency>
</dependencies>