java.lang.ClassNotFoundException:Eclipse 中的 com.mysql.jdbc.Driver [英] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

查看:32
本文介绍了java.lang.ClassNotFoundException:Eclipse 中的 com.mysql.jdbc.Driver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码有什么问题,调试时有很多错误.我正在为单例类编写代码以连接数据库 mysql.

What is wrong with the code there are lots of error while debugging. I am writing a code for a singleton class to connect with the database mysql.

这是我的代码

package com.glomindz.mercuri.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySingleTon {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "test";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "";

    private static MySingleTon myObj;   
    private Connection Con ;
    private MySingleTon() {
        System.out.println("Hello");
        Con= createConnection();
    }

    @SuppressWarnings("rawtypes")
    public Connection createConnection() {
        Connection connection = null;
        try {
            // Load the JDBC driver
            Class driver_class = Class.forName(driver);
            Driver driver = (Driver) driver_class.newInstance();
            DriverManager.registerDriver(driver);
            connection = DriverManager.getConnection(url + dbName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * Create a static method to get instance.
     */
    public static MySingleTon getInstance() {
        if (myObj == null) {
            myObj = new MySingleTon();
        }
        return myObj;
    }

    public static void main(String a[]) {
        MySingleTon st = MySingleTon.getInstance();
    }
}

我是 Java 新手.请帮忙.

I am new to java. Please help.

推荐答案

项目中似乎没有包含mysql连接库.按照建议的解决方案之一解决问题:

It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:

  • MAVEN 项目解决方案

在 pom.xml 项目文件中添加 mysql-connector 依赖:

Add the mysql-connector dependency to the pom.xml project file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

这里是所有版本:https://mvnrepository.com/artifact/mysql/mysql-connector-爪哇

  • 所有项目解决方案

手动将 jar 库添加到项目中.

Add the jar library manually to the project.

右键项目-->构建路径-->配置构建路径

Libraries Tab press Add External JarSelect 你的 jar.

In Libraries Tab press Add External Jar and Select your jar.

您可以在此处

  • 说明:

在构建项目时,java 向您抛出异常,因为未找到来自 mysql 连接库的文件(com.mysql.jdbc.Driver 类).解决办法是在项目中加入库,java会找到com.mysql.jdbc.Driver

When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver

这篇关于java.lang.ClassNotFoundException:Eclipse 中的 com.mysql.jdbc.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆