java代码写的Mysql建表语句,老是提示语句错误,求帮助啊,万分感谢
privatestaticvoidcreateTable()throwsSQLException{ConnectiondbConnection=null;Prepared...
private static void createTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String createTableSQL = "CREATE TABLE DBUSER1("
+ "USER_ID NUMBER(5) NOT NULL, "
+ "USERNAME VARCHAR(20) NOT NULL, "
+ "CREATED_BY VARCHAR(20) NOT NULL, "
+ "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
+ ")";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(createTableSQL);
System.out.println(createTableSQL);
// execute create SQL stetement
preparedStatement.execute();
System.out.println("Table \"dbuser1\" is created!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
提示错误:
private static void createTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String createTableSQL = "CREATE TABLE DBUSER1("
+ "USER_ID NUMBER(5) NOT NULL, "
+ "USERNAME VARCHAR(20) NOT NULL, "
+ "CREATED_BY VARCHAR(20) NOT NULL, "
+ "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
+ ")";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(createTableSQL);
System.out.println(createTableSQL);
// execute create SQL stetement
preparedStatement.execute();
System.out.println("Table \"dbuser1\" is created!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}提示错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMBER(5) NOT NULL, USERNAME VARCHAR(20) NOT NULL, CREATED_BY VARCHAR(20) NOT NU' at line 1 展开
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String createTableSQL = "CREATE TABLE DBUSER1("
+ "USER_ID NUMBER(5) NOT NULL, "
+ "USERNAME VARCHAR(20) NOT NULL, "
+ "CREATED_BY VARCHAR(20) NOT NULL, "
+ "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
+ ")";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(createTableSQL);
System.out.println(createTableSQL);
// execute create SQL stetement
preparedStatement.execute();
System.out.println("Table \"dbuser1\" is created!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
提示错误:
private static void createTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String createTableSQL = "CREATE TABLE DBUSER1("
+ "USER_ID NUMBER(5) NOT NULL, "
+ "USERNAME VARCHAR(20) NOT NULL, "
+ "CREATED_BY VARCHAR(20) NOT NULL, "
+ "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
+ ")";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(createTableSQL);
System.out.println(createTableSQL);
// execute create SQL stetement
preparedStatement.execute();
System.out.println("Table \"dbuser1\" is created!");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}提示错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMBER(5) NOT NULL, USERNAME VARCHAR(20) NOT NULL, CREATED_BY VARCHAR(20) NOT NU' at line 1 展开
3个回答
2012-10-20
展开全部
把createTableSQL输出看看组成的sql语句是什么,然后应该能找到sql的问题;
更多追问追答
追问
System.out.println(createTableSQL);执行到这里,输出的语句是CREATE TABLE DBUSER1(USER_ID NUMBER(5) NOT NULL, USERNAME VARCHAR(20) NOT NULL, CREATED_BY VARCHAR(20) NOT NULL, CREATED_DATE DATE NOT NULL, PRIMARY KEY (USER_ID) )
追答
NUMBER 这个字段类型应该没写对吧
为numberic或者为int的,
另最好指定数据库引擎;
展开全部
MySQL没有NUMBER的,亲。换成INT就行了。成功了记得给好评!
追问
嗯,这个改过了。然后,这样写代码:String createTableSQL = "DROP TABLE IF EXISTS "+tablename+";CREATE TABLE "+tablename+"("+...... 如果加了一句,存在表时,删除这个表,又会出错,这是哪有问题呢
追答
这个还真没试过,一个字符串我们一般都是搞的的一句SQL,你这个搞的相当于是两条SQL语句了,没遇到过,所以不知道,我也是瞎猜,你可以试试先故意弄一个存在的表,只执行删除表这句,如果能删除,那就是不能一条字符串执行多个SQL操作。我知道在SQL界面是可以的,String里面的话,可能有些问题。如果不是业务一定需要的话,前面那句可以不要,你先手动执行就行看。但是这方面可以研究研究。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
mysql中没有number类型啊,当然要报错了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询