运行时错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [英] runtime error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

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

问题描述

我是mysql和jdbc的新手,我收到了这个标题中的错误。我一整天都在搜索,找不到适合我的解决方案。

I am new to mysql and jdbc and I am getting the error in this title. I have been searching all day and cannot find a solution that works for me.

我尝试过:
卸载/重新安装mysql,将mysql-connector-java-5.1.25-bin.jar和ojdbc7.jar复制粘贴到同一个作为我试图运行的.class文件的位置,在不同的目录中重建程序,可能还有其他一些东西。

What I have tried: uninstall/reinstall mysql, copy paste mysql-connector-java-5.1.25-bin.jar and ojdbc7.jar to same location as the .class file I am trying to run, rebuilt the program in a different directory, and probably a couple other things.

我使用notepad ++进行编码,并使用windows命令提示符进行编译和运行。它编译得很好,但我尝试运行

I am using notepad++ for coding and the windows command prompt to compile and run. it compiles fine but I try to run with


C:\Projects\bin> java -cp。 ClientBase

C:\Projects\bin>java -cp . ClientBase

输出为:


java.lang.ClassnNotFoundException:com.mysql.jdbc.Driver

java.lang.ClassnNotFoundException: com.mysql.jdbc.Driver


at java.net.URLClassloader $ 1.run(URLClassLoader。 java:336)

at java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

at java.lang.ClassLoader.loadClass(ClassLoader.java:432)

at sun。 misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)

at java.lang.ClassLoader.loadClass(ClassLoader.java:356)

at java.lang.Class。 forName0(本机方法)

在java.lang.Class.forName(Class.java:188)

在ClientBase.main(ClientBase.java:21)

Goodbye。

at java.net.URLClassloader$1.run(URLClassLoader.java:336)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:432)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at ClientBase.main(ClientBase.java:21)
Goodbye.




// import packages
import java.sql.*;

// create class ClientBase
public class ClientBase{
            // JDBC driver name and database URL
            static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";                
            static final String DB_URL = "jdbc:mysql://localhost/CLIENTBASE";          



 // Database credentials
    static final String USER = "root";
    static final String PASS = "";

// Begin method main
public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;

    try{
        // register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        // Open connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        // Execute a query
        System.out.println("Creating statement...");
        stmt = conn.createStatement();
        String sql;
        sql = "SELECT id, name, address, address 2, city, phone, state, zip, fax FROM CLIENTBASE";
        ResultSet rs = stmt.executeQuery(sql);

        // Extract data from result set
        while(rs.next()){
            // Retrieve by column name
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String address = rs.getString("address");
            String address2 = rs.getString("address2");
            String city = rs.getString("city");
            String phone = rs.getString("phone");
            String state = rs.getString("state");
            String zip = rs.getString("zip");
            String fax = rs.getString("fax");

            // Display values
            System.out.print("ID: " + id);
            System.out.print(" Name: " + name);
            System.out.println("Address:" + address);
            System.out.println(address2);
            System.out.print("City:" + city);
            System.out.print(" State: " + state);
            System.out.println(" Zip: " + zip);
            System.out.print("Phone: " + phone);
            System.out.println(" Fax: " + fax);
        } // end while

        // clean up
        rs.close();
        stmt.close();
        conn.close();
    }catch(SQLException se){
        // Handle errors for JDBC
        se.printStackTrace();
    }catch(Exception e){
        // Handle errors for Class.forName
        e.printStackTrace();
    }finally{
        // finally block used to close resources
        try{
            if(stmt!=null)
                stmt.close();
        }catch(SQLException se){
            se.printStackTrace();
        } // end finally
    } // end try
System.out.println("Goodbye.");
} // End method main
} // end class ClientBase

I还应该说我将在这个代码的在线教程。它并不是他们如何拥有它,因为我决定制作一些与他们不同的东西,但它通常是相同的。我不认为这是一个代码问题,但错误是什么。

I should also say that I am going off an online tutorial for this code. It is not exactly how they have it as I decided to make something a little different than theirs, but it is generally the same. I don't think it is a code problem though from what the error is.

任何帮助将不胜感激!我疯了!

Any help would be appreciated! I'm going crazy!

推荐答案

您需要在Runtime类路径中添加一个连接器库:

You need to add a connector library to the Runtime classpath:

java -cp .;mysql-connector-java-5.1.25-bin.jar ClientBase

我的示例使用Windows类路径分隔符;,在其他系统上可能不同( Linux / Mac上的:。它还假设 mysql-connector-java-5.1.25-bin.jar 位于同一文件夹中。如果不是这样,那么就把路径放到库而不是普通名称。

My example uses Windows classpath separator ";", on other systems it may be different (":" on Linux/Mac). It also assumes, that mysql-connector-java-5.1.25-bin.jar is located on the same folder. If it's not the case, then put a path to the library instead of the plain name.

ClientBase 代表这里的Java类文件名

ClientBase stands for Java class file name here

c:\>javac Test.java
c:\>java -cp .;F:\CK\JavaTest\JDBCTutorial\mysql-connector-java-5.1.18-bin Test

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

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