似乎语法很好但不知道该怎么办?请弄明白。
以下是要插入的行之一: INSERT INTO SmartMiner(Outlook VARCHAR(64),Temp VARCHAR(64),Humidity VARCHAR(64),Windy VARCHAR(64),Play VARCHAR(64))价值观('晴天,热,高,假,没有')
异常: java.sql.BatchUpdateException:SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行“VARCHAR(64),Temp VARCHAR(64),Humidity VARCHAR(64),Windy VARCHAR(64),Play VARCHAR”附近使用正确的语法
public void FileRead(String st, String tableName) {
Statement stmt = null;
String sql = null;
BufferedReader br = null;
try {
FileReader fr = new FileReader(st);
br = new BufferedReader(fr);
String line = br.readLine();
System.out.println(line);
String[] tokens = line.split(",");
for(int x=0;x<tokens.length;x++){
System.out.println(tokens[x]);
}
setConnection();
//connection.setAutoCommit(false);
stmt = connection.createStatement();
DatabaseMetaData metadata = connection.getMetaData();
ResultSet resultSet;
resultSet = metadata.getTables(null, null, "tableName", null);
if(resultSet!=null){
// next() checks if the next table exists ...
System.out.println("Table exists");
sql = "DROP TABLE IF EXISTS " + tableName;
System.out.println("sql :" + sql);
stmt.executeUpdate(sql);
}
String query = "";
String comma = "";
for (String token : tokens) {
query += comma + token;
comma = ",";
}
sql = "CREATE TABLE SmartMiner (" + query + ")";
System.out.println("sql :" + sql);
stmt.executeUpdate(sql);
line = br.readLine();
if(line != null){
System.out.println(line);
}
while (line != null) {
sql = "INSERT INTO "+tableName+"("+query+")"+"VALUES("+line+")";
System.out.println("sql :" + sql);
stmt.addBatch(sql);
line = br.readLine();
}
stmt.executeBatch();
//connection.commit();
closeConnection();
}
答案 0 :(得分:1)
VALUES ('sunny,hot,high,false,no')
- 这是完全错误的。这是一个文本字段,而不是5.给定表名后面提到的列数。
应该是:
VALUES ('sunny','hot','high','false','no')
。
您需要修复生成VALUES
子句(即您的line
变量)的逻辑。它似乎只是从文件中读取一行,但是该行未正确格式化,或者您需要在解析它并构建查询时做更多的工作。