Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java在我的sql中更新数据库_Java_Mysql_Database_Jdbc - Fatal编程技术网

使用java在我的sql中更新数据库

使用java在我的sql中更新数据库,java,mysql,database,jdbc,Java,Mysql,Database,Jdbc,我在使用java更新MySQL中的数据库时遇到问题。具体来说,这个问题发生在我更新分数(GUI界面的最后一个分数)之后。更新成功,但当我尝试检索更新的记录时。它给我一个空指针异常(将记录图片的值设置为空).由于编写的代码太多,我决定只插入为更新数据库而编写的代码。我遇到的主要问题是在更新数据库中的最后一条记录之后,当检索记录时,图片的值被设置为null,这给了我一个java.lang.null指针异常。请在这里帮助我。提前感谢。 更新代码 private void upActionPerfor

我在使用java更新MySQL中的数据库时遇到问题。具体来说,这个问题发生在我更新分数(GUI界面的最后一个分数)之后。更新成功,但当我尝试检索更新的记录时。它给我一个空指针异常(将记录图片的值设置为空).由于编写的代码太多,我决定只插入为更新数据库而编写的代码。我遇到的主要问题是在更新数据库中的最后一条记录之后,当检索记录时,图片的值被设置为null,这给了我一个java.lang.null指针异常。请在这里帮助我。提前感谢。 更新代码

 private void upActionPerformed(java.awt.event.ActionEvent evt) {                                   

 //checks if user wants to update an empty field or record.   
    if ( m.equals("") || f.equals("") || l.equals("") || g.equals("") || d.equals("") || p.equals("") || a.equals("")) {
        JOptionPane.showMessageDialog(null, "Please fill in all the details");

    }else if(fi.equals("")){
       JOptionPane.showMessageDialog(null,"Please re-update picture");
    }
    else{
   int confirmUpdate = JOptionPane.showConfirmDialog(null, "Are you sure you want to update this record?", "Confirm Update", JOptionPane.YES_NO_OPTION);
     if (confirmUpdate == JOptionPane.YES_OPTION) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/ass1", "root", "root");
//SQL update statement to update record based on prmary key mn.
            ps = con.prepareStatement("Update Student SET fn=?,ln=?,gn=?,dp=?,pn=?,ad=?,mt=?,ph=?,ch=?,zo=?,pp=?,pc=?,pz=?,tt=?,av=?,gd=?,re=?,pic=?,fil=? Where mn='" + m + "'");


            ps.setString(1, f);
            ps.setString(2, l);
            ps.setString(3, g);
            ps.setString(4, d);
            ps.setString(5, p);
            ps.setString(6, a);

            ps.setString(7, h);
            ps.setString(8, s);
            ps.setString(9, i);
            ps.setString(10, y);
            ps.setString(11, yp);
            ps.setString(12, pm);
            ps.setString(13, zp);
            ps.setString(14, al);
            ps.setString(15, ge);
            ps.setString(16, de);
            ps.setString(17, rk);

            ps.setBytes(18, pi);
            ps.setString(19, fi);

            ps.executeUpdate();
            JOptionPane.showMessageDialog(null, "Data Updated Successfully", "Data Updated", JOptionPane.INFORMATION_MESSAGE);
            dispose();
         new view().setVisible(true);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error:" + e);
        }
      }
    }
}                                  
请注意,导致空指针异常的最后一条记录的变量为rk。这是根据变量zp表示的上一条记录计算得出的。希望我的问题清楚。提前谢谢

"Update Student SET fn=?,ln=?,gn=?,dp=?,pn=?,ad=?,mt=?,ph=?,ch=?,
zo=?,pp=?,pc=?,pz=?,tt=?,av=?,gd=?,re=?,pic=?,fil=? 
Where mn='"

我在UPDATE命令中看不到RK,即使按Ctrl-F尝试查找也没有成功。

如果使用“fn=”,为什么要使用“where mn=”+“xyz+”?“我没有使用fn,我是根据“mn”的值更新“fn”,它是主键。谢谢。好的,让我这么说。为什么要使用安全正确的方法为fn指定变量和准备好的语句,并使用不安全且易受sql注入影响的方法为mn连接字符串?:-)请建议一个更好的方法。谢谢。更新学生集合fn=?,ln=?,gn=?,dp=?,pn=?,ad=?,mt=?,ph=?,ch=?,zo=?,pp=?,pc=?,pz=?,tt=?,av=?,gd=?,re=,pic=,fil=?其中mn=?Hi,rk不在update命令中,但在“ps.setString”语句中。update命令中等于rk的变量为“re”。