首先感谢之前帮助过我的人。
我目前遇到的问题是这行代码
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");
}
}
}