【发布时间】:2019-09-03 06:21:40
【问题描述】:
我正在尝试将 HashMap 结果存储到 MySQL 数据库中。数据库包含两列对应HashMap的key和value。但是在将 HashMap 结果插入数据库表时出现了一些错误。以下是例外。
Exception in thread "main"
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near 'Key, Value)
VALUES ('17/05/2018','11894060.45')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1541)
at com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2605)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1469)
at com.ceino.Database.executeUpdate(Database.java:46)
at com.ceino.CSVwrite.writeCSVFile(CSVwrite.java:198)
at com.ceino.CSVwrite.main(CSVwrite.java:120)
数据库.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
private static String DRIVER_NAME = "com.mysql.jdbc.Driver";
private static String URL = "jdbc:mysql://localhost:3306/Customer";
private static String USERNAME = "root";
private static String PASSWORD = "";
private static Connection conn = null;
public Database() {
try {
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Database Connection Initialized.");
}
public void closeConnection() {
if (conn == null) return;
try {
conn.close();
conn = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public boolean execute(String sql) throws SQLException {
if (conn
== null)
throw new SQLException("Connection null!");
Statement statement = conn.createStatement();
boolean res = statement.execute(sql);
statement.close();
return res;
}
public int executeUpdate(String sql) throws SQLException {
if (conn
== null)
throw new SQLException("Connection null!");
Statement statement = conn.createStatement();
int res = statement.executeUpdate(sql);
statement.close();
return res;
}
public ResultSet executeQuery(String sql) throws SQLException {
if (conn == null)
throw new SQLException("Connection null!");
Statement statement = conn.createStatement();
ResultSet res = statement.executeQuery(sql);
statement.close();
return res;
}
}
FileWrite.java
static Database db = new Database();
private static void writeCSVFile(HashMap<String, String> result) throws SQLException {
for (Map.Entry<String, String> next : result.entrySet()) {
String rslt1 = next.getKey();
String rslt2 = next.getValue();
db.executeUpdate("INSERT INTO amount (Key, Value) VALUES ('" + rslt1 + "','" + rslt2 + "');");
}
db.closeConnection();
}
【问题讨论】:
-
请格式化您的代码。我尽力格式化了。
-
请您尝试删除 ;来自查询。
-
@KarthikeyanVaithilingam 谢谢。你能帮我解决问题吗?
-
@SujayMohan 我试过了。但同样的问题。
标签: java mysql exception jdbc hashmap