JDBC连接和测试错误

发布于 2024-12-17 19:11:08 字数 4109 浏览 5 评论 0原文

我尝试运行此代码,但它给了我很多错误,我如何知道我自己的

 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,我已经下载了连接器/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)

I tried to run this code but it gave me numerous errors, and How do I know my Own

 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

and I had this error

when I ran this program and I selected option number 1 it gave me this error

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

after I click ok it gave me this error

I am using MySQL, I've downloaded my connector/j and I've set it's classpath, h

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

Any help? and how do I set the default driver in eclipse helios?
EDIT:

I updated the my codee my problem now is this

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-12-24 19:11:08

这:

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 上退出您的方法,这是一个很大的错误,并且不应允许您的方法进一步执行。
因此,您的程序将进一步执行并抛出第二个错误。

This:

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

is wrong.
Like your source-code comment suggested it needs to be:

Class.forName("myDriver.ClassName");

And using mysql it probably is:

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

Your url is used to create a connection with:

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

Class.forName loads a driver class. It is not responsible for making the connection.

By the way:
You get two errors, because you don't exit your method on the ClassNotFoundError, which is quite a heave error and your method should not be allowed to execute further.
Therefore your program will execute further and throw the second error.

陈独秀 2024-12-24 19:11:08

Class.forName 用于加载驱动程序类,

它应该是这样的

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

,你的 url 应该是这样的

url ="jdbc:mysql://localhost:3306/yourDataBaseName"

Class.forName is for loading the driver class

it should be something like this

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

and your url should be like this

url ="jdbc:mysql://localhost:3306/yourDataBaseName"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文