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

标签 java db2 classpath

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

您唯一需要做的是:

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

关于java - 如何使用 java 设置类路径并连接到 DB2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53572045/

相关文章:

java - 为什么 ApplicationContext 和 ClasspathXmlApplicationContext 在这里被视为错误

java - 将背景图像添加到重复的 JPanel

java - 在方法覆盖中使用子类型作为参数

java - Oracle 和 db2 的 CLASSPATH 与 java.library.path 设置

java - 如何将 Hibernate 中的 BigInteger 映射到 DB2

java - 在 Visual Studio Code 中导入 .jar,类路径

java - 我是否需要客户端、服务器和注册表上的所有类才能使 RMI 工作?

java - 如何在hibernate中使用hql查询进行级联

sql - DB2 将自动增量列添加到现有表

Ant 在构建时找不到 ant 类 (DateUtils)