【问题标题】:Java creating tables in MySQL DatabaseJava 在 MySQL 数据库中创建表
【发布时间】:2013-10-01 17:17:39
【问题描述】:

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

我现在遇到的问题是这行代码

    statement.executeUpdate(myTableName);

或者用这些代码行

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

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

要么很简单,要么很复杂

谁能指出这个Java MySQL编程新手在哪里犯了错误,希望不是错误,在此先感谢

这是其余的完整代码

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

【问题讨论】:

  • 在您的catchblock 中使用e.printStackTrace()。获取堆栈跟踪并添加到您的帖子中。
  • 请这样做,我猜你在创建数据库和创建表之间缺少Connection.setCatalog(dbName);,但需要确认实际的错误消息。
  • 只是为了让事情更清楚,并添加到 Smit 的评论中,将 System.out.println(e.getMessage()) 添加到您的 catch 块(和 e.printStackTrace())以获取错误消息及其发生的位置。

标签: java mysql database jdbc


【解决方案1】:

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

  • 如何编写正确的创建表说明。
  • 如何在 Java 中创建 MySQL 表。
  • Smit 说的是读取 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), "
            + "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");
    }
}

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这可能是一个可能的原因。
    猜你喜欢
    • 2013-10-01
    • 2010-10-17
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多