【问题标题】:jdbc application update sql isnt working for me, any ideasjdbc 应用程序更新 sql 不适合我,任何想法
【发布时间】:2018-08-20 14:14:43
【问题描述】:
public void Update(String name, String mobile, String email, String car, String model, String year, String color, String plate_no, String plateletters,String gear, String km, String date,String licesnse,String status, String notes){
    Connection conn = null;
    try{
        conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
        System.out.println("connected");
        Statement stmt = (Statement) conn.createStatement();
        String ins = "update car"
                + "set Name = '"+name+"' , "
                + "Email = '"+email+"' , "
                + "Car = '"+car+"' , "
                + "Model = '"+model+"' , "
                + "Year = '"+year+"' , "
                + "Color = '"+color+"' , "
                + "Plateno = '"+plate_no+"' , "
                + "Plateletters = '"+plateletters+"' , "
                + "Gearbox = '"+gear+"' , "
                + "Km = '"+km+"' , "
                + "Date = '"+date+"' , "
                + "License = '"+licesnse+"' , "
                + "Status = '"+status+"' , "
                + "Notes = '"+notes+"'  "
                + "where Mobile = '"+mobile+"' ";

        stmt.execute(ins);

    }catch(SQLException e){
        JOptionPane.showMessageDialog(null, e.getMessage());
        System.err.println(e);
        return;
    }

}

**我已经尝试过准备好的声明和stmt.executeUpdate(ins);,但没有任何效果!!

请帮忙!**

错误:

java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 '= 'ahmed' 、 Email = 'ahmed@hotmail.com' 、 Car = 'honda' 、 Model = 'hondaz' 、 Yea' 在第 1 行附近使用正确的语法

【问题讨论】:

  • 那么为什么这个问题上的PHP标签或者phpMyAdmin会来那个
  • 标签垃圾邮件只会吸引反对票。您应该只标记使用的语言。
  • 您错过了使用准备好的语句的要点。通过语句,您使用? 占位符作为列值,然后将Java 变量绑定到这些占位符。 JDBC trails on prepared statements 可能是你接下来应该去的地方,看看你哪里出错了。
  • 你在carset之间缺少一个空格,所以实际语句是update carset Name,这是一个语法错误。但是请学习如何正确使用准备好的语句,因为您当前的代码是不安全的,因为它容易受到 SQL 注入的影响。

标签: java mysql jdbc


【解决方案1】:

您需要确保在 SQL 查询中的元素之间添加适当的空格:

 String ins = "update car"

应该是

 String ins = "update car "

这样做的一个好方法是在您的数据库查询工具(SQL 开发人员、phpMyAdmin 等)中编写和执行您的 SQL,并在尝试用 Java 重写之前确保您的语法正确。

【讨论】:

    【解决方案2】:

    使用preparedStatement 代替Statement

     Connection conn = null;
                    try{
                        conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
                        PreparedStatement pstm = conn.prepareStatement(
                                          "update car "
                                        + "set Name = ? ,"
                                        + "Email = ? ,"
                                        + "Car = ? ,"
                                        + "Model = ? ,"
                                        + "Year = ? ,"
                                        + "Color = ? ,"
                                        + "Plateno = ? ,"
                                        + "Plateletters = ? ,"
                                        + "Gearbox = ? ,"
                                        + "Km = ?,"
                                        + "Date = ?,"
                                        + "License = ?, "
                                        + "Status = ?, "
                                        + "Notes = ?  "
                                        + "where mobile = ?"
                        );
                        pstm.setObject(1, name);
                        pstm.setObject(2, email);
                        pstm.setObject(3, car);
                        pstm.setObject(4, model);
                        pstm.setObject(5, year);
                        pstm.setObject(6, color);
                        pstm.setObject(7, plate_no);
                        pstm.setObject(8, plateletters);
                        pstm.setObject(9, gear);
                        pstm.setObject(10, km);
                        pstm.setObject(11, date);
                        pstm.setObject(12, licesnse);
                        pstm.setObject(13, status);
                        pstm.setObject(14, notes);
                        pstm.setObject(15, mobile);
    
                        pstm.executeUpdate();
    
                    }catch(SQLException e){
                        e.printStackTrace();
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多