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

查看:169
本文介绍了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 PROJECTS SOLUTION

  • MAVEN PROJECTS SOLUTION

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

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-java


  • 所有项目解决方案

  • ALL PROJECTS SOLUTION

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

Add the jar library manually to the project.

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

In Libraries选项卡 添加外部Jar 选择你的jar。

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

你可以找到mysql-connector的zip 此处

You can find zip for mysql-connector here


  • 说明:

构建项目时,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天全站免登陆