Java在MySQL数据库中创建表 [英] Java creating tables in MySQL Database

查看:145
本文介绍了Java在MySQL数据库中创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢以前帮助我的那些。



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

  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语句语句;
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);
语句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 AUTO_INCREMENT,
+initialues VARCHAR(2),
+agentDate DATE,
+agentCount INT(64)
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(dbAddress + dbName,userName,password);
statement = con.createStatement();
//下一行有问题
statement.executeUpdate(myTableName);
System.out.println(Table Created);
}
catch(SQLException e){
System.out.println(Table Creation上发生错误);
}
catch(ClassNotFoundException e){
System.out.println(找不到Mysql驱动程序);
}
}
}


解决方案>

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

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


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.

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

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

解决方案

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

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

这篇关于Java在MySQL数据库中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆