db2 自定义java,将DB2与Java连接

I need to connect DB2 with Java.

Kindly help me to connect

DB2 with java in ECLIPSE.

It would be of great help if you could guide me step by step

Please let me know how to add classpath in eclipse

code snippet:

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;

public class connection {

public static void main(String[] argv) {

try {

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

}

catch (ClassNotFoundException e) {

System.out.println("Please include Classpath Where your DB2 Driver is located");

e.printStackTrace();

return;

}

System.out.println("DB2 driver is loaded successfully");

Connection conn = null;

PreparedStatement pstmt = null;

ResultSet rset=null;

boolean found=false;

try {

conn = DriverManager.getConnection("jdbc:db2:sabarish","db2admin","Murugasaranam");

if (conn != null)

{

System.out.println("DB2 Database Connected");

}

else

{

System.out.println("Db2 connection Failed ");

}

pstmt=conn.prepareStatement("Select * from bo");

rset=pstmt.executeQuery();

if(rset!=null)

{

while(rset.next())

{

found=true;

System.out.println("Class Code: "+rset.getString("clcode"));

System.out.println("Name: "+rset.getString("name"));

}

}

if (found ==false)

{

System.out.println("No Information Found");

}

} catch (SQLException e) {

System.out.println("DB2 Database connection Failed");

e.printStackTrace();

return;

}

}

}

On running the code I got the following exceptions:

java.lang.ClassNotFoundException: COM.ibm.db2.jdbc.app.DB2Driver

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at connection.main(connection.java:11)

解决方案

You need to correct the package name.

Class.forName("com.ibm.db2.jdbc.app.DB2Driver");

To add .jar in your project => Project + Properties + Java Build Path + Select "Libraries" tab + Add External Jars...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值