如何通过DAO更新mysql中的实体ID?

时间:2017-12-12 06:59:43

标签: java dao netbeans-8

//这是我在AccountDAO中的代码

@Override
    public void editAccount(Accounts account) throws ErrorException {

            response = FAILED;
            Connection connection = null;
            PreparedStatement pStatement = null;
            String sql = "UPDATE all_accounts SET 
                         accountID=?,accPassword=?,accStatus=?"
                         + "WHERE accountID "+ account.getAccountID();
            ResultSet resultSet = null;
        try {    
                if(manager == null){
                       manager = (DBManager) 
                                 DBManagerImplementation.getInstance();
            }

            connection = manager.getConnection();
            pStatement = connection.prepareStatement(sql);

            pStatement.setString(1, account.getPassword());
            pStatement.setString(2, account.getStatus());

            pStatement.execute();
        } catch (SQLException ex) {
            System.out.println("Error in editng/adding new 
                                 employee!!"+ex.toString());

        }catch (Exception ex) {
             System.out.println("Erroorr. super error!!"+ex.toString());
        } finally{
            DataDispatcher.dispatch(resultSet, pStatement, connection);
        }
    }

但是在编译之后这是我的错误:

  

编辑/添加新内容时出错   员工!com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:   您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在第1行'111'附近

3 个答案:

答案 0 :(得分:1)

这是您的查询的样子

"UPDATE all_accounts SET 
accountID=?,accPassword=?,accStatus=?"
+ "WHERE accountID "+ account.getAccountID();

一些突出的事物

  • WHERE子句
  • 之前没有空格
  • WHERE条款没有转让,即。

WHERE accountID ="+ account.getAccountID();

答案 1 :(得分:1)

在你的sql中,你使用3个参数,但你只设置了两个。 它应该在第一个参数之前设置id。也要注意在之前没有空格和WHERE accountID你需要使用WHERE accountID =

答案 2 :(得分:0)

您无需在以下查询中设置accountId

package Practice;

import java.awt.*;
import javax.swing.JFrame;

public class Screen {

    private GraphicsDevice videoCard;

    public Screen(){

        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        videoCard = env.getDefaultScreenDevice();
    }

    public void setFullScreen(DisplayMode dm, JFrame window){

        window.setUndecorated(true);
        window.setResizable(false);
        videoCard.setFullScreenWindow(window);

        if(dm != null && videoCard.isDisplayChangeSupported()){
            try{
                videoCard.setDisplayMode(dm);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

    public Window getFullScreenWindow(){

        return videoCard.getFullScreenWindow();
    }

    public void restoreScreen(){

        Window w = videoCard.getFullScreenWindow();

        if(w != null){

            w.dispose();
        }

        videoCard.setFullScreenWindow(null);
    }

}

删除 String sql = "UPDATE all_accounts SET accountID=?,accPassword=?,accStatus=?" + " WHERE accountID ="+ account.getAccountID(); 并使用此查询

accountID