此代码出现Duplicate entry '2' for key 'PRIMARY'这种错误,

1.package edu.jmi.xyh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import edu.jmi.xyh.bean.Student;

public class StudentDao {

public int save(Student student){

    DBOperation dboperation= new DBOperation();

    Connection connection=null;
    try{
        connection=dboperation.getConnection();
        Statement st=connection.createStatement();

        String sql="insert into student(stuId,stuName,stuPwd)values('"
                +student.getStuId()+"','"+student.getStuName()+"','"+student.getStuPwd()+"')";


        st.execute(sql);        
        System.out.println(sql);
        return 0;
    }catch(Exception ex){
        ex.printStackTrace();

        return -1;
    }finally{
        dboperation.closeConnect(connection);
    }
}
public int update(Student student){

    DBOperation dboperation= new DBOperation();

    Connection connection=null;
    try{
        connection=dboperation.getConnection();
        Statement st=connection.createStatement();

        String sql="update student set stuName='"+student.getStuName()+
                "', stuPwd='"+student.getStuPwd()+"' where stuId='"+
                 student.getStuId()+"'";
        st.execute(sql);
           System.out.println(sql);
        return 0;
    }catch(Exception ex){
        ex.printStackTrace();

        return -1;
    }finally{
        dboperation.closeConnect(connection);
    }
}
public int delete(Student student){

    DBOperation dboperation= new DBOperation();

    Connection connection=null;
    try{
        connection=dboperation.getConnection();

        String sql="delete from student where stuId='?'";
        PreparedStatement pst=connection.prepareStatement(sql);
        pst.setString(1, student.getStuId());
        pst.execute();
         /* Statement st=connction.createStatement();

           String sql="delete from student where Stuid='"+student.getStuid()+"'";
           st.execute(sql);*/
        System.out.println(sql);
        return 0;
    }catch(Exception ex){
        ex.printStackTrace();

        return -1;
    }finally{
        dboperation.closeConnect(connection);
    }
}
public List<Student> query(){

    DBOperation dboperation= new DBOperation();

    Connection connection=null;
    List<Student> list = new ArrayList<Student>();
    try{

        connection=dboperation.getConnection();
        Statement st=connection.createStatement();

        String sql="select* from student";

        ResultSet rs=st.executeQuery(sql);

        if(rs.next()){
            Student student=new Student();
            String stuId=rs.getString("stuId");
            String stuName=rs.getString("stuName");
            String stuPwd=rs.getString("stuPwd");
            student.setStuId(stuId);
            student.setStuName(stuName);
            student.setStuPwd(stuPwd);

            list.add(student);
        }
        System.out.println(sql);
        return list;
    }catch(Exception ex){
        ex.printStackTrace();

        return null;
    }finally{
        dboperation.closeConnect(connection);
    }
}
public static Student query(String id) {

       DBOperation dbOperation=new DBOperation();

       Connection connction=null;

       try{
           connction=dbOperation.getConnection();
           Statement st=connction.createStatement();

           String sql="select * from student where stuid='"+id+"'";

           ResultSet rs=st.executeQuery(sql);


           Student student=null;
           if(rs.next()){

               student=new Student();
               String stuId=rs.getString("stuId");
                String stuName=rs.getString("stuName");
                String stuPwd=rs.getString("stuPwd");
                student.setStuId(stuId);
                student.setStuName(stuName);
                student.setStuPwd(stuPwd);
           }
           return student;
       }catch(Exception ex){
           ex.printStackTrace();

           return null;
       }finally{
           dbOperation.closeConnect(connction); 
       }
}
public static void main(String args[]){
    StudentDao studentDao=new StudentDao();
     List<Student> list=studentDao.query();
       //for each语句 iterator
       for(Student student:list){
           System.out.println(student.getStuName());
       }

}

}
2.package edu.jmi.xyh.view;

import java.awt.BorderLayout;

public class MainView extends JFrame {

private JPanel contentPane;
protected Object id;
protected Object pw;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainView frame = new MainView();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MainView() {
    setTitle("\u6B22\u8FCE\u767B\u5F55\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 545, 444);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnNewMenu = new JMenu("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406");
    menuBar.add(mnNewMenu);

    JMenu mnNewMenu_1 = new JMenu("\u5B66\u751F\u4FE1\u606F");
    mnNewMenu.add(mnNewMenu_1);

    JMenu menu = new JMenu("\u6CE8\u518C");
    menu.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            RegisterView gui =new RegisterView(); //生成主窗体对象
            gui.setMainView(MainView.this);//将登录对象设置到主窗体对象,便于在主窗体中引用
            gui.setID(id);//将用户名设置到主窗体对象
            gui.setVisible(true);   //显示出窗体
            //隐藏登录窗口
            MainView.this.setVisible(false); //隐藏登录窗体
        }
    });
    mnNewMenu_1.add(menu);

    JMenu menu_1 = new JMenu("\u7BA1\u7406");
    mnNewMenu_1.add(menu_1);

    JMenu menu_2 = new JMenu("\u67E5\u8BE2");
    mnNewMenu_1.add(menu_2);

    JMenu menu_3 = new JMenu("\u9009\u8BFE\u4FE1\u606F");
    mnNewMenu.add(menu_3);

    JMenu menu_4 = new JMenu("\u73ED\u7EA7\u7BA1\u7406");
    menuBar.add(menu_4);

    JMenu menu_5 = new JMenu("\u8BFE\u7A0B\u7BA1\u7406");
    menuBar.add(menu_5);

    JMenu menu_6 = new JMenu("\u7528\u6237\u7BA1\u7406");
    menuBar.add(menu_6);

    JMenu menu_7 = new JMenu("\u9000\u51FA");
    menu_7.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            System.exit(0);
        }
    });
    menuBar.add(menu_7);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel label = new JLabel("\u6B22 \u8FCE \u60A8 \u4F7F \u7528 \u5B66 \u751F");
    label.setFont(new Font("宋体", Font.PLAIN, 20));
    label.setBounds(218, 52, 200, 40);
    contentPane.add(label);

    JLabel label_1 = new JLabel("\u7BA1 \u7406 \u7CFB \u7EDF");
    label_1.setFont(new Font("宋体", Font.PLAIN, 20));
    label_1.setBounds(246, 93, 131, 24);
    contentPane.add(label_1);
}

public void initView() {
    // TODO Auto-generated method stub

}

}
3.package edu.jmi.xyh.view;

import java.awt.BorderLayout;

public class RegisterView extends JFrame {

private JPanel contentPane;
private JTextField txID;
private JTextField txName;
private JPasswordField txPw;



/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                RegisterView frame = new RegisterView();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public RegisterView() {
    setTitle("\u5B66\u751F\u4FE1\u606F\u6CE8\u518C");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 361, 327);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel label = new JLabel("\u5B66 \u751F \u4FE1 \u606F \u6CE8 \u518C");
    label.setFont(new Font("宋体", Font.PLAIN, 20));
    label.setBounds(109, 10, 197, 27);
    contentPane.add(label);

    JLabel lblId = new JLabel("\u7528 \u6237 ID\uFF1A");
    lblId.setBounds(78, 47, 73, 15);
    contentPane.add(lblId);

    JLabel label_2 = new JLabel("\u7528 \u6237 \u540D\uFF1A");
    label_2.setBounds(78, 85, 73, 15);
    contentPane.add(label_2);

    JLabel lblNewLabel = new JLabel("\u5BC6    \u7801\uFF1A");
    lblNewLabel.setBounds(78, 124, 73, 15);
    contentPane.add(lblNewLabel);

    txID = new JTextField();
    txID.setBounds(157, 47, 110, 21);
    contentPane.add(txID);
    txID.setColumns(10);

    txName = new JTextField();
    txName.setBounds(161, 82, 106, 21);
    contentPane.add(txName);
    txName.setColumns(10);

    txPw = new JPasswordField();
    txPw.setBounds(161, 121, 106, 21);
    contentPane.add(txPw);

    JButton btnNewButton = new JButton("\u6CE8\u518C");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String id=txID.getText();
            if(id==null||id.length()==0||id.length()>20){
                JOptionPane.showMessageDialog(null, "ID输入不正确!");
            }
            //继续验证用户名和密码
            String pw=txPw.getText();
            String name=txName.getText();
            Student student=new Student();

            student.setStuId(id);
            student.setStuName(name);
            student.setStuPwd(pw);
            StudentDao studentDao=new StudentDao();
            int result=studentDao.save(student);
            if(result==4)
                JOptionPane.showMessageDialog(null, "注册成功!");
            else{
                JOptionPane.showMessageDialog(null, "注册失败!");
                System.exit(0);
            }
            System.out.println("id="+id+"pw="+pw);
        }
    });
    btnNewButton.setBounds(47, 183, 93, 23);
    contentPane.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("\u9000\u51FA");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            MainView mainView = new MainView();

            RegisterView.this.setVisible(false);
            mainView.setVisible(true);
        }
    });
    btnNewButton_1.setBounds(194, 183, 93, 23);
    contentPane.add(btnNewButton_1);
}

public void setMainView(MainView mainView) {
    // TODO Auto-generated method stub

}

public void setID(Object id) {
    // TODO Auto-generated method stub

}

}

2个回答

应该是由于你在数据库作为主键的列插入了两条相同的值吧

最通俗的理解就是主键重复,企图在插入两条主键相同的记录

qq_30895105
_sean 回复java1996: ![图片说明](http://img.ask.csdn.net/upload/201609/01/1472710341_266273.png)个是你注册后返回的值吧 这个值是多少呢?
大约 4 年之前 回复
java1996
java1996 不管输入什么值都显示注册失败,注册成功还是跳出注册失败的,注册失败的数据还会保持进数据库,这是怎么回事
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐