java - JDBC 连接和测试错误

标签 java mysql jdbc driver connector-j

我试图运行这段代码,但它给了我很多错误,我怎么知道我自己的错误

 import javax.swing.JOptionPane;
import java.sql.*;
public class JdbcConnection{

    static String userid="root", password = "123192";
    static String url = "jdbc:mysql://localhost:3306/Testdb";   // String url = "jdbc:mySubprotocol:myDataSource"; ?
    static Statement stmt;
    static Connection con;
    public static void main(String args[]){

        JOptionPane.showMessageDialog(null,"JDBC Programming showing Creation of Table's");
        int choice = -1;

        do{
            choice = getChoice();
            if (choice != 0){
                getSelected(choice);
            }
        }
        while ( choice !=  0);
            System.exit(0);
    }

    public static int getChoice()
    {
        String choice;
        int ch;
        choice = JOptionPane.showInputDialog(null,
            "1. Create Employees Table\n"+
            "2. Create Products Table\n"+
            "0. Exit\n\n"+
            "Enter your choice");
        ch = Integer.parseInt(choice);
        return ch;

    }

    public static void getSelected(int choice){
        if(choice==1){
            createEmployees();
        }
        if(choice==2){
            createOrders();
        }
    }

    public static Connection getConnection()
    {



        try {
            Class.forName("com.mysql.jdbc.Driver"); //Class.forName("myDriver.ClassName"); ?

        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }

        try {
            con = DriverManager.getConnection(url,
                                     userid, password);

        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }

        return con;
    }

    /*CREATE TABLE Employees (
            Employee_ID INTEGER,
            Name VARCHAR(30)
        );*/
    public static void createEmployees()
    {
        Connection con = getConnection();

        String createString;
        createString = "create table Employees (" +
                            "Employee_ID INTEGER, " +
                            "Name VARCHAR(30))";
        try {
            stmt = con.createStatement();
            stmt.executeUpdate(createString);
            stmt.close();
            con.close();

        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
        JOptionPane.showMessageDialog(null,"Employees Table Created");
    }

    /*CREATE TABLE Orders (
            Prod_ID INTEGER,
            ProductName VARCHAR(20),
            Employee_ID INTEGER
        );*/

    public static void createOrders()
    {
        Connection con = getConnection();

        String createString;
        createString = "create table Orders (" +
                            "Prod_ID INTEGER, " +
                            "ProductName VARCHAR(20), "+
                            "Employee_ID INTEGER )";


        try {
            stmt = con.createStatement();
            stmt.executeUpdate(createString);

            stmt.close();
            con.close();

        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
        JOptionPane.showMessageDialog(null,"Orders Table Created");
    }


}//End of class

我有这个错误

当我运行这个程序并选择选项 1 时,它给了我这个错误

ClassNotFoundException: jdbc:mysql://localhost:3306/
SQLException: No database selected

在我点击确定后它给了我这个错误

我正在使用 MySQL,我已经下载了我的 connector/j 并设置了它的类路径,h

C:\Program Files\Java\jdk1.6.0_24\bin;C:\Program Files\Java\jre6\lib\ext

有什么帮助吗?以及如何在 eclipse helios 中设置默认驱动程序? 编辑:

我更新了我的代码,我现在的问题是这个

Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at JdbcConnection.getChoice(JdbcConnection.java:33)
    at JdbcConnection.main(JdbcConnection.java:15)

最佳答案

这个:

Class.forName("jdbc:mysql://localhost:3306/");

错了。 就像您的源代码评论建议的那样:

Class.forName("myDriver.ClassName");

使用 mysql 可能是:

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

您的 url 用于创建连接:

con = DriverManager.getConnection(url, userid, password);

Class.forName加载驱动程序类。它不负责建立连接。

顺便说一句: 你得到两个错误,因为你没有在 ClassNotFoundError 上退出你的方法,这是一个相当严重的错误,你的方法不应该被允许进一步执行。
因此,您的程序将继续执行并抛出第二个错误。

关于java - JDBC 连接和测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254588/

相关文章:

mysql - 当其中一个联接表中没有行可用时,左联接不返回任何行

java - BeanUtils.copyProperties 缺少深度嵌套变量?

java - Spring 中可选字段的 Autowiring

java - 如何从java中的json对象替换 "emailRecipients"键值

mysql - 需要优化 TIMESTAMPDIFF 查询

java - 优化查询 : DBMS_METADATA. GET_DDL (Oracle)

java - 如何避免使用 Thread.Sleep

mysql - 如何通过从另一个表中选择列来向 mysql 表添加列

java - 如何刷新与jtable绑定(bind)的Jscrollpane中的数据?

java - 如何使用服务名称而不是 SID 连接到 Oracle