Java 在 MySQL 数据库中创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19016363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:24:29  来源:igfitidea点击:

Java creating tables in MySQL Database

javamysqldatabasejdbc

提问by K Spriggs

First of all thanks to those that helped me previously.

首先感谢之前帮助过我的人。

The issue that I'm having at the moment, is with either this line of code

我目前遇到的问题是这行代码

    statement.executeUpdate(myTableName);

or with these lines of code

或使用这些代码行

    String myTableName = "CREATE TABLE AgentDetail (" 
        + "idNo INT(64) NOT NULL AUTO_INCREMENT,"  
        + "initials VARCHAR(2)," 
        + "agentDate DATE,"  
        + "agentCount INT(64))";  

When it code reaches these points, it generates an error which is caught by the SQLException block.

当它的代码到达这些点时,它会生成一个错误,该错误被 SQLException 块捕获。

It is either very simple or it is very complicated

要么很简单,要么很复杂

Could anybody point out where this newbie to Java MySQL programming has made the error and hopefully not errors, thanks in advance

任何人都可以指出这个 Java MySQL 编程新手在哪里犯了错误,希望不是错误,提前致谢

Here is the Rest of the code in full

这是完整的其余代码

    public class DbStuff {
    private String jdbcDriver = "com.mysql.jdbc.Driver";
    private String dbAddress = "jdbc:mysql://localhost:3306/";
    private String userPass = "?user=root&password=";
    private String dbName = "TIGER19";
    private String userName = "root";
    private String password = "";

    private PreparedStatement preStatement;
    private Statement statement;
    private ResultSet result;
    private Connection con;

    public DbStuff() {
        try {
            Class.forName(jdbcDriver);
            con = DriverManager.getConnection(dbAddress + dbName, userName, password);
        } 
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 
        catch (SQLException e) {
            createDatabase();
            createTableCub1();
        }
    }

    private void createDatabase() {
        try {
            Class.forName(jdbcDriver);
            con = DriverManager.getConnection(dbAddress + userPass);
            Statement s = con.createStatement();
            int myResult = s.executeUpdate("CREATE DATABASE IF NOT EXISTS " + dbName);
        } 
        catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    private void createTableCub1() {
        String myTableName = "CREATE TABLE AgentDetail (" 
            + "idNo INT(64) NOT NULL AUTO_INCREMENT,"  
            + "initials VARCHAR(2)," 
            + "agentDate DATE,"  
            + "agentCount INT(64))";  
        try {
            Class.forName(jdbcDriver);
            con = DriverManager.getConnection(dbAddress + dbName, userName, password);
            statement = con.createStatement();
            //The next line has the issue
            statement.executeUpdate(myTableName);
            System.out.println("Table Created");
        }
        catch (SQLException e ) {
            System.out.println("An error has occurred on Table Creation");
        }
        catch (ClassNotFoundException e) {
            System.out.println("An Mysql drivers were not found");
        }
    }
    }

采纳答案by Shamim Ahmmed

Your table creation SQL statement is not correct. To set a column auto increment in mysql it has to be primary key.

您的表创建 SQL 语句不正确。要在 mysql 中设置列​​自动增量,它必须是主键。

CREATE TABLE AgentDetail ( 
        idNo INT(64) NOT NULL AUTO_INCREMENT, 
        initials VARCHAR(2),
        agentDate DATE,  
        agentCount INT(64),PRIMARY KEY (`idNo`));

回答by K Spriggs

First of all thank you for the help and advice. It was very helpful indeed. As a result of this I have managed to learn three thing,

首先感谢您的帮助和建议。这确实很有帮助。因此,我设法学到了三件事,

  • How to write a proper instruction in how to create a table.
  • How to create a MySQL table in Java.
  • Something that Smit said, which was to read the e.printStackTrace() information.
  • 如何编写有关如何创建表格的正确说明。
  • 如何在 Java 中创建 MySQL 表。
  • Smit 说的就是读取 e.printStackTrace() 信息。

Here is the code that I have come up with.

这是我想出的代码。

private void createTableCub1() {
    String myTableName = "CREATE TABLE AgentDetail (" 
            + "idNo INT(64) NOT NULL AUTO_INCREMENT,"  
            + "initials VARCHAR(2)," 
            + "agentDate DATE,"  
            + "agentCount INT(64), "
            + "PRIMARY KEY(idNo))";  
    try {
        Class.forName(jdbcDriver);
        con = DriverManager.getConnection(dbAddress + dbName, userName, password);
        statement = con.createStatement();
        //This line has the issue
        statement.executeUpdate(myTableName);
        System.out.println("Table Created");
    }
    catch (SQLException e ) {
        System.out.println("An error has occured on Table Creation");
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        System.out.println("An Mysql drivers were not found");
    }
}

Of course I would welcome and appreciate any and all feedback

当然,我会欢迎并感谢任何和所有反馈