我不知道如何在java项目中设置类路径。
这是我运行的代码:
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:DB2PDBA","USERID","PASSWORD");
if (conn != null)
{
System.out.println("DB2 Database Connected");
}
else
{
System.out.println("Db2 connection Failed ");
}
我的错误是:
java.lang.ClassNotFoundException: com.ibm.db2.jdbc.app.DB2Driver
最佳答案
当我们说在类路径中放置某些内容时,它的意思是:
you should put the related jar files that your app needs to run the entire application somewhere in your project.
请点击下面的链接创建一个 DB2 应用程序 -> IBM Example about DB2
您唯一需要做的是:
- 将其添加到您的项目中
- 更改凭据和数据库信息
- 将 db2 jdbc jar 文件添加到您的项目
关于java - 如何使用 java 设置类路径并连接到 DB2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53572045/