分为本地访问和远程连接.
本地访问有很多种方法,常用的有两种。
一种是使用命令行连接。
在cmd终端中键入DB2cmd
进入到了DB2的命令行,再键入DB2命令进入数据库。
键入 connect to sample (sample是数据库库名)
还有一种是使用控制中心。
DB2的远程连接
命令行连接
本文假定如下:
客户端为windows/linux/unix,客户端也已经安装了db2或者客户端
服务器端为windows/linux/unix
客户端ip:192.168.42.147,服务器端ip为192.168.42.102
步骤:
在客户端建立服务器端数据库的节点
在客户端命令行执行
catalog tcpip node test remote 192.168.42.102 server 50000
注:test为在客户端定义的节点,名字可以任意
192.168.42.102为服务端的ip
50000为DB2使用的端口
查找服务器端DB2使用的端口,
如果服务器端为linux,可以访问/etc/services文件,如果你的实例名为db2inst1,找到如下行
db2inst1 50000/tcp
如果服务器端为windows,查看X:\WINNT\System32\drivers\etc\services
绑定节点和客户端的数据库
执行下面的命令catalog db coshine at node test
注:coshine为服务器端中你想要连接的数据库名
Test就是步骤一种定义的节点
使绑定生效
执行 terminate
设定客户端db2codepage(代码页设置)即字符编码
先在服务器端查询db2codepage,执行db2set Call
[i] DB2_EXTENDED_OPTIMIZATION=ON
[i] DB2_DISABLE_FLUSH_LOG=ON
[i] AUTOSTART=YES
[i] DB2_STRIPED_CONTAINERS=ON
[i] DB2_HASH_JOIN=Y
[i] DB2COMM=tcpip
[i] DB2CODEPAGE=819
[i] DB2_PARALLEL_IO=*
可以看到服务器端的DB2CODEPAGE为819,所以客户端也必须设置为这个数字
执行db2set db2codepage=819
进行连接
执行 connect to coshine user user_name using password
注释
Db2codepage:即db2数据库的编码方式
db2codepage=1386(简体中文)
db2country=86(中国)
一个数据库一旦建立,他的代码页就没有办法改,
db2set DB2CODEPAGE=1386只能改变代码页环境,不能改变数据库的代码页
如果不能连接:检查如下是否正确
确保可以ping通服务器端:ping 192.168.42.102
确保服务器端的db2已经启动:db2start
确保客户端DB2COMM设置为tcpip
先查看db2comm设置:db2set Call
如果没有设置这个注册表,执行db2set db2comm=tcpip
将SVCENAME设置成/etc/services中的端口号或者服务名了吗?
执行db2 get dbm cfg查看,找到SVCENAME,如果当前值不是服务器端的端口号或者服务名,进行更新设置
执行:db2 update dbm cfg SVCENAME db2inst1
注:db2inst1为服务名,这个在/etc/services文件中db2inst1 50000/tcp
一致
JAVA连接DB2数据库例子
import java.lang.reflect.*;
import java.sql.*;
class db2JDBCVersion
{
static Driver driver = null;
static final String JDBC12 = "IBM DB2 JDBC 1.2";
static final String JDBC20 = "IBM DB2 JDBC 2.0";
//static final String url = "jdbc:db2:demo";
static final String url = "jdbc:db2://10.55.64.74:50000/";
static Method mCreateStatement = null;
static
{
try
{
driver = (Driver)Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
}
catch (Exception e)
{
e.printStackTrace();
printBadEnvironmentMessage();
}
}
public static void main(String argv[])
{
if (null == driver)
System.exit(-1);
Connection con = null;
try
{
if (argv.length == 0)
{
// Connect with default database-alias (SAMPLE), username and
// password
// con = DriverManager.getConnection(url + "SAMPLE");
con = DriverManager.getConnection(url );
}
else if (argv.length == 1)
{
// Connect with user-provided database-alias; default username and
// password
con = DriverManager.getConnection(url + argv[0]);
}
else if (argv.length == 2)
{
// Connect with default database-alias (SAMPLE); user-provided
// username and password
con = DriverManager.getConnection(url + "SAMPLE", argv[0], argv[1]);
}
else if (argv.length == 3)
{
// Connect with user-provided database-alias, username and password
con = DriverManager.getConnection(url + argv[0], argv[1], argv[2]);
System.out.println("con="+con.toString());
}
else
{
System.out.println(
"\nUsage: java db2JDBCVersion [database-alias | " +
"[database-alias] username password]\n");
System.exit(-1);
}
// getDriverName indicates JDBC version as of:
// DB2 v6.1 FixPak 8
// DB2 v7.1 FixPak 2a
DatabaseMetaData dbmd = con.getMetaData();
String JDBCVersion = dbmd.getDriverName();
if (JDBCVersion.indexOf("1.2") >= 0)
{
JDBCVersion = JDBC12;
}
else if (JDBCVersion.indexOf("2.0") >= 0)
{
JDBCVersion = JDBC20;
}
// Old version of DB2, use alternate method of determining JDBC version
// i.e. check if method Connection.createStatement(int, int) exists
else if (getJDBC20CreateStatementMethod() == null)
{
JDBCVersion = JDBC12;
}
else
{
JDBCVersion = JDBC20;
}
// If class files are JDBC 2.0 version, check that the
// native libraries are too
if (JDBCVersion == JDBC20 && !verifyJDBC20(con))
{
JDBCVersion = null;
}
con.close();
if (null == JDBCVersion)
{
printBadEnvironmentMessage();
System.exit(-1);
}
else
{
System.out.println(JDBCVersion);
}
}
catch (Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}
synchronized static private Method getJDBC20CreateStatementMethod()
{
if (null == mCreateStatement)
{
try
{
Class c = Class.forName("COM.ibm.db2.jdbc.app.DB2Connection");
Class params[] = new Class[]{ Integer.TYPE, Integer.TYPE };
mCreateStatement = c.getDeclaredMethod("createStatement", params);
}
catch (Exception e)
{
}
}
return mCreateStatement;
}
static private void printBadEnvironmentMessage()
{
System.out.println(
"Bad DB2 JDBC environment; please run usejdbc1 or usejdbc2 again " +
"and check for errors.");
}
static private boolean verifyJDBC20(Connection con)
{
Method m = getJDBC20CreateStatementMethod();
if (null == m)
{
return false;
}
Object params[] = null;
Statement stmt = null;
try
{
params = new Object[]{
new Integer(1004), // ResultSet.TYPE_SCROLL_INSENSITIVE
new Integer(1007) }; // ResultSet.CONCUR_READ_ONLY
stmt = (Statement)m.invoke(con, params);
}
catch (Exception e)
{
return false;
}
// Attempt to execute a batch statement, if the native library is
// really the JDBC 1.2 version, then the following exception will
// be thrown:
// InvocationTargetException->UnsatisfiedLinkError
try
{
Class c[] = new Class[]{ Class.forName("java.lang.String") };
m = stmt.getClass().getDeclaredMethod("addBatch", c);
params = new Object[]{ "insert into dummy values (1)" };
m.invoke(stmt, params);
m = stmt.getClass().getDeclaredMethod("executeBatch", null);
m.invoke(stmt, null);
}
catch (InvocationTargetException e)
{
// Should get here if JDBC 2.0 is working properly
Throwable t = e.getTargetException();
if (t instanceof UnsatisfiedLinkError)
{
return false; // Indicates wrong DB2 JDBC library
}
}
catch (Exception e)
{
return false;
}
finally
{
try
{
stmt.close();
}
catch (Exception e)
{
}
try
{
con.rollback();
}
catch (Exception e)
{
}
}
return true;
}
}