如何设置 class 路径并使用 java 连接到 DB2

How to set class path and connect to DB2 using java

我不知道如何在 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.

请按照下面的link创建DB2应用程序->IBM Example about DB2

您唯一需要做的就是:

  1. 将其添加到您的项目中
  2. 更改凭据和数据库信息
  3. 将 db2 jdbc jar 文件添加到您的项目中