jdbc应用程序更新sql不适合我,有什么想法吗

vq8itlhq  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(312)
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); 但什么都没用!!

请帮帮我**
错误:
sqlsyntaxerrorexception:您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以获取在“=”ahmed“,email=”附近使用的正确语法ahmed@hotmail.com'汽车='本田',车型='本田',是'在1号线

e4yzc0pl

e4yzc0pl1#

使用 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();
                }
liwlm1x9

liwlm1x92#

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

String ins = "update car"

应该是

String ins = "update car "

这样做的一个好方法是在数据库查询工具(sqldeveloper、phpmyadmin等)中编写和执行sql,并在尝试用java重新编写之前确保语法良好。

相关问题