简体   繁体   English

使用JDBC连接到MySQL的问题

[英]problem using JDBC to connect to mysql

I'm trying to set up a connection between my applet and my mysql server using jdbc 我正在尝试使用jdbc在applet和mysql服务器之间建立连接

I added the jar mysql-connector-java-5.1.14-bin.jar to the project 我将jar mysql-connector-java-5.1.14-bin.jar添加到项目中

then I used this code 然后我用这段代码

public void databaseTesting(){
        Connection con;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root","");
            System.err.println("connected !");

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM `test`.`test`;");
            while (rs.next()) {
                int A = rs.getInt("columnA");
                int B = rs.getInt("columnB");
                System.err.println("A "+A+" B "+B);
            }
        } catch (SQLException e) {
            System.err.println("failed to connect");
        } catch (InstantiationException e) {
            System.err.println("InstantiationException");
        } catch (IllegalAccessException e) {
            System.err.println("IllegalAccessException");
        } catch (ClassNotFoundException e) {
            System.err.println("ClassNotFoundException");
        }
    }

and for some reason I keep getting ClassNotFoundException. 由于某种原因,我一直在收到ClassNotFoundException。


edit 编辑

the jar is added to the build path and appears in the Referenced Libraries. 该jar将添加到构建路径中,并显示在“引用的库”中。

the exception is thrown by 由抛出异常

Class.forName("com.mysql.jdbc.Driver").newInstance(); Class.forName(“ com.mysql.jdbc.Driver”)。newInstance();

Anybody got an idea why ? 有人知道为什么吗?

Thanks in advance jason 在此先感谢杰森

Do you run this application through eclipse? 您是否通过Eclipse运行此应用程序? Is it a Dynamic web project, if so then try adding the jar file to the WEB-INF\\lib folder 它是一个动态Web项目吗,如果是这样,请尝试将jar文件添加到WEB-INF \\ lib文件夹中

Then the JAR is not in the runtime classpath. 然后,JAR不在运行时类路径中。

Since you explicitly mentioned "project", I'll assume that you're using an IDE. 由于您明确提到“项目”,因此我假设您使用的是IDE。 You have to add the JAR file to the so-called Build Path (which represents both the compiletime and runtime classpath). 您必须将JAR文件添加到所谓的“ 构建路径” (它代表编译时和运行时类路径)。 In Eclipse for example, rightclick the JAR file you dropped in the project folder, choose Build Path > Add to Build Path and that should be it. 例如,在Eclipse中,右键单击放置在项目文件夹中的JAR文件,选择“ 构建路径”>“添加到构建路径”

替代文字

See also: 也可以看看:


Update : If it keeps complaining, then it is still not in the runtime classpath. 更新 :如果它一直在抱怨,那么它仍然不在运行时类路径中。 Either you did it wrong or the runtime environment didn't use this JAR. 您做错了或者运行时环境未使用此JAR。 Did you run it as a Java Application or as a Java Applet? 您是否将其作为Java应用程序或Java Applet运行? (even though it's a bad practice to do JDBC inside an applet). (即使在applet中执行JDBC是一种不好的做法)。 If you're actually running this as an applet, it has got to be in the runtime classpath of the applet as well. 如果您实际上是将其作为applet运行,则它也必须位于applet的运行时类路径中。 You can specify it in the archive attribute/parameter of the applet. 您可以在小程序的archive属性/参数中指定它。

Your code is OK! 您的代码还可以! It will work when: 在以下情况下将起作用:

  1. Your mysql-connector-java-5*-bin.jar is in place. 您的mysql-connector-java-5 * -bin.jar就位。
  2. The login & password are correct. 登录名和密码正确。
  3. Your database exists 您的数据库存在
  4. Your table exists 您的表存在
  5. Your ColumnA and ColumnB are integers 您的ColumnA和ColumnB是整数

    If you are using an IDE just add the jar to the Libraries. 如果您使用的是IDE,只需将jar添加到库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM