在Java中的SQL查询异常

huangapple go评论120阅读模式
英文:

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 (?,?,?)"

huangapple
  • 本文由 发表于 2020年8月31日 18:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63668758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定