英文:
sql query exception in java
问题
我正在尝试在NetBeans中将我的Java项目与数据库连接。我有一个名为"task_table"的表,我正在尝试在该表上添加一些值。
以下是我的代码:
Connection con1;
PreparedStatement insert;
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String name1 = addtaskname.getText();
String name2 = addtaskdate.getText();
String name3 = addtasktime.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/jesin_virtual", "root", "");
insert = con1.prepareStatement("insert into task_table(`Task Name`,`Task Date`,`Task Time`) values (?,?,?)");
insert.setString(1, name1);
insert.setString(2, name2);
insert.setString(3, name3);
insert.executeUpdate();
JOptionPane.showMessageDialog(this, "Task Added successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
我遇到了以下异常:
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
英文:
I am trying to connect my java project with database in netbeans. I have a table named task_table and i'm trying to add some values on that table.
Here is my code
Connection con1;
PreparedStatement insert;
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String name1= addtaskname.getText();
String name2= addtaskdate.getText();
String name3= addtasktime.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/jesin_virtual","root","");
insert = con1.prepareStatement("insert into task_table(Task Name,Task Date,Task Time) values (?,?,?)");
insert.setString(1, name1);
insert.setString(2,name2 );
insert.setString(3,name3);
insert.executeUpdate();
JOptionPane.showMessageDialog(this, "Task Added sucessfully");
and i got this exception:
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
答案1
得分: 1
问题出在这里:
insert into task_table(Task Name,Task Date,Task Time) values (?,?,?)
^ ^ ^
如果列名确实包含空格,则需要在列名周围使用反引号(`Task Name`),否则将列名更改为实际的名称(可能是Task_Name吗?)。
英文:
The problem is here:
insert into task_table(Task Name,Task Date,Task Time) values (?,?,?)
^ ^ ^
If the column names really contain blanks, then you need backticks around the names (`Task Name`), else change the names to what they really are (Task_Name maybe?).
答案2
得分: 0
请更正以下内部的SQL查询...
如果您的数据库列名包含空格,请使用以下查询
"insert into task_table("任务名称","任务日期","任务时间") values (?,?,?)"
如果您的数据库列名包含下划线,请使用以下查询
"insert into task_table(Task_Name,Task_Date,Task_Time) values (?,?,?)"
英文:
Please correct SQL query inside as below...
if in your database column names contains space so please use below query
"insert into task_table("Task Name","Task Date","Task Time") values (?,?,?)"
if in your database column names contains _ so please use below query
"insert into task_table(Task_Name,Task_Date,Task_Time) values (?,?,?)"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论