cei*_*-vg 0 java mysql sql-server jdbc insert
我正在尝试向表中进行简单插入,但我的程序说我有 SQL 语法错误。有任何想法吗?
SQL代码
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)
);
Run Code Online (Sandbox Code Playgroud)
JAVA代码
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());
}
Run Code Online (Sandbox Code Playgroud)
错误
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
Run Code Online (Sandbox Code Playgroud)
您需要为所有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();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1710 次 |
最近记录: |