我正在尝试将一个简单的插入到我的表中,但我的程序说我有一个SQL语法错误。有什么想法吗?
SQL CODE
CREATE TABLE ticket (
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
seat VARCHAR(50) NOT NULL,
sep VARCHAR(50) NOT NULL,
price INT(50) NOT NULL,
foroom VARCHAR(50) NOT NULL,
printer INT(15) NOT NULL,
PRIMARY KEY (seat)
);
JAVA CODE
try
{
System.out.println("Attempting to connect...\n");
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/baseis?autoReconnect=true&useSSL=false","root","");
System.out.println("Connection Succesful!!!");
String sql = "INSERT INTO ticket (start_time)" + " VALUES (?)";
PreparedStatement prepared = connection.prepareStatement(sql);
prepared.setString(1,"2016-07-17 19:00:00");
prepared.executeUpdate(sql);
}
catch(SQLException error)
{
System.out.println("Error: " + error.getMessage());
}
ERROR
Error: 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 '?)' at line 1
答案 0 :(得分:3)
您需要为所有NOT NULL
列提供值,并且不要将sql
字符串提供给executeUpdate()
:
// set some values for the record
String seat = "";
String sep = "";
int price = 0;
String foroom = "";
int printer = 0;
String sql = "INSERT INTO ticket (start_time, end_time, seat, sep, price, foroom, printer) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement prepared = connection.prepareStatement(sql);
prepared.setString(1, "2016-07-17 19:00:00");
prepared.setString(2, "2016-07-17 20:00:00");
prepared.setString(3, seat);
prepared.setString(4, sep);
prepared.setInt(5, price);
prepared.setString(6, foroom);
prepared.setInt(7, printer);
prepared.executeUpdate();
答案 1 :(得分:1)
您需要为声明为“NOT NULL”的所有列提供值
调试此类问题的最佳方法是捕获发送到数据库的实际SQL语句。