使用JDBC连接到mysql的问题

使用JDBC连接到mysql的问题

问题描述:

我正在尝试使用jdbc在applet和mysql服务器之间建立连接

I'm trying to set up a connection between my applet and my mysql server using jdbc

我将jar mysql-connector-java-5.1.14-bin.jar添加到项目中

I added the jar mysql-connector-java-5.1.14-bin.jar to the project

然后我使用了这段代码

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");
        }
    }

,由于某种原因,我不断收到ClassNotFoundException.

and for some reason I keep getting ClassNotFoundException.

编辑

将jar添加到构建路径,并显示在引用的库"中.

the jar is added to the build path and appears in the Referenced Libraries.

该异常由

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

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

有人知道为什么吗?

预先感谢杰森

您是否通过Eclipse运行此应用程序?这是一个动态Web项目吗,如果是这样,请尝试将jar文件添加到WEB-INF \ lib文件夹

Do you run this application through eclipse? Is it a Dynamic web project, if so then try adding the jar file to the WEB-INF\lib folder