首先感谢以前帮助过我的人。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 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");
}
}
}
使用'e.printStackTrace()'到你的'catch'block中。获取堆栈跟踪并添加到您的帖子。 – Smit
请这样做,我的猜测是你缺少'Connection.setCatalog(dbName);'db创建和表创建之间,但实际的错误信息需要确认。 – rsanchez
为了使事情更清楚,并添加到Smit的注释中,将'System.out.println(e.getMessage())'添加到'catch'块(和'e.printStackTrace()')以获得错误消息和它发生的地方。 – Barranka