【问题标题】:jsp mysql inserting errorjsp mysql插入错误
【发布时间】:2017-05-17 21:07:11
【问题描述】:

这是 y 插入语法

String add="INSERT INTO time_table VALUES("+coursename+"','"+coursecode+"','"+days+"','"+year+"','"+dep+"','"+time+"','"+hall+"','"+lecturer+"','"+credithours+"')"; 

错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
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 '','5','2','2','8','1','9','PA','2')' at line 1 

这就是我收到的错误。

【问题讨论】:

  • 你的time_table表结构是什么?如果您不指定列名,则预计您将按照与数据库中定义的顺序相同的顺序为所有列传递数据。

标签: java mysql jsp jdbc


【解决方案1】:

我认为您的查询的正确版本是这样的:

"INSERT INTO time_table VALUES('"+coursename+"','"+coursecode+"','"+days+"','"+year+"','"+dep+"','"+time+"','"+hall+"','"+lecturer+"','"+credithours+"')"; 

你在+coursename+的开头忘记了'

【讨论】:

    【解决方案2】:

    永远不要使用这种方式,它会导致语法错误,或者 SQL 注入,你必须使用 PreparedStatement 来代替,例如:

    String add = "INSERT INTO time_table VALUES(?, ?, ?, ?, ?,?,?,?,?)";
    
    try (PreparedStatement insert = connection.prepareStatement(add)) {
    
        insert.setString(1, coursename);
        insert.setString(2, coursecode);
        insert.setString(3, days);
        insert.setString(4, year);
        insert.setString(5, dep);
        insert.setString(6, time);
        insert.setString(7, hall);
        insert.setString(8, lecturer);
        insert.setString(9, credithours);
    
        insert.executeUpdate();
    }
    

    注意如果您的属性是 int 或 float 或 date 或 ... 在您的表中,那么您必须使用正确的集合,例如如果年份是 in 您可以使用 @987654322 @ 代替。


    您真正的问题在于您在此处错过' 的查询:

    INSERT INTO time_table VALUES('" + coursename + "'
    //----------------------------^
    

    【讨论】:

    • 事实上你们太有帮助了。愿上帝保佑你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2011-08-12
    • 2013-06-17
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多