[英]error when creating a table
I am trying to make a program where I would read data from txt files and store them in tables. 我正在尝试制作一个程序,可以从txt文件读取数据并将其存储在表中。 All .txt files would be in a specific directory.
所有.txt文件都将位于特定目录中。 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);
和
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)
On first view there is an error in your Create Statement related to parenthesis: 在第一个视图上,您的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)
您必须在CREATE TABLE语句中用大括号括起来的字段(它们在双引号外面,应该在里面):
String createtable = "CREATE TABLE " + tablename + " ( DATE VARCHAR(255), HOUR VARCHAR(255), LITRES VARCHAR(255) )";
Change your getCreateTable1
to the following. 将您的
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.