0

I am trying to make a program where I would read data from txt files and store them in tables. All .txt files would be in a specific directory. The table would have specific fields where I would create for each file in my program. I have written the code below and as you can see the user give the directory where the files are, then the data would be shown in console and then I am trying to make the table which would take as name the name of the file without the .txt

But when I am running the program I get error in lines:

stmt.executeUpdate(createtable); and getCreateTable1(con, tablename);.

Could anyone help me why does this happen?

What i get is this:

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 'DATE VARCHAR(255), HOUR VARCHAR(255), LITRES VARCHAR(255)' at line 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: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 'DATE VARCHAR(255), HOUR VARCHAR(255), LITRES VARCHAR(255)' at line 1
at notepad.getCreateTable1(notepad.java:97)
    at notepad.main(notepad.java:76)
4

3 回答 3

1

在第一次查看时,您的 Create Statement 中存在与括号相关的错误:

    String createtable = "CREATE TABLE " + tablename +  ( " DATE VARCHAR(255), HOUR VARCHAR(255), LITRES VARCHAR(255)");


Your final statement is: CREATE TABLE tablename DATE VARCHAR(255), HOUR VARCHAR(255), LITRES VARCHAR(255)
于 2013-05-30T08:35:20.520 回答
0

您必须在 CREATE TABLE 语句中用大括号将字段括起来(它们在双引号之外,应该在里面):

String createtable = "CREATE TABLE " + tablename +   " ( DATE VARCHAR(255), HOUR VARCHAR(255), LITRES VARCHAR(255) )";
于 2013-05-30T08:35:10.143 回答
0

将您的更改getCreateTable1为以下内容。

private static String getCreateTable1 (Connection con, String tablename){
    Statement stmt = null;
    try {
        Class.forName("com.mysql.jdbc.Driver"); // No need of this
        stmt = con.createStatement();
        String dropTable = "DROP TABLE IF EXIST "+tablename;
        String createtable = "CREATE TABLE " + tablename +  "(  DATE VARCHAR(255), HOUR VARCHAR(255), LITRES VARCHAR(255));" ;
        System.out.println("Create a new table in the database");
        stmt.executeUpdate(dropTable);
        stmt.executeUpdate(createtable);
    } catch (Exception e) {
        System.out.println(((SQLException) e).getSQLState());
        System.out.println(e.getMessage());
        e.printStackTrace();
    } finally {
        if(stmt != null) {
            try {
                stmt.close();
            } catch(Exception e) {
                // Warn
            }   
        }            
    }

    return null;
}
于 2013-05-30T08:38:38.910 回答