Java更新derby数据库中的记录

时间:2017-11-29 13:03:36

标签: java sql derby joptionpane

这是我到目前为止的java代码,一个使用joptionpane管理员工的简单应用程序,我可以获取密码的东西,我可以将记录插入数据库工作没问题,但我不能让它更新记录,如果我试图更新任何记录它不起作用的记录只是保持不变

有人可以帮助我,谢谢

import javax.swing.JOptionPane;
import java.sql.*;
/**
 *
 * 
 */
public class EMP {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

      PreparedStatement prest;           
        int count = 0;
        String username = "";
        String password = "";


       while (count < 5 && (!(username.equals("Brian")) && (!password.equals("Student")))) {

                username = JOptionPane.showInputDialog(null, "Please Enter your Username ");

                password = JOptionPane.showInputDialog(null, "Please Enter your Password ");
                count++;

                if (username.equals("Brian") && password.equals("Student")) {
                   JOptionPane.showMessageDialog(null, " login successfully \n Welcome" + username );
                    break;

                }

                if (count >= 5) {
                   JOptionPane.showMessageDialog(null, "You have used to many attempts \n " + "you will not be able to sing in for an hour");
                   System.exit(0);

                } else {
                    JOptionPane.showMessageDialog(null, "Wrong username or password \n please try again  \n"+ "you have used " + count  + " out of 5 attpemts \n  before your account will be locked for 1 hour");


                }
        }

                String Option = JOptionPane.showInputDialog(null, "Please enter one of the options \n A. Add a new Employee \n B. Upate Empoyess record  \n C. Search Employee name by their id number \n D. List All employess \n E. Delete Employee\n F. Exit System \n Your Chosse   ");
            if (Option.equalsIgnoreCase("A")) {

                String id = JOptionPane.showInputDialog(null, "Please enter the Employess ID", JOptionPane.QUESTION_MESSAGE);
                String fname = JOptionPane.showInputDialog(null, "Please enter Employess First NAME", JOptionPane.QUESTION_MESSAGE);
                String sname = JOptionPane.showInputDialog(null, "Please enter Employess Surname NAME", JOptionPane.QUESTION_MESSAGE);
                String pss = JOptionPane.showInputDialog(null, "Please enter the Employess PPS Number", JOptionPane.QUESTION_MESSAGE);   
                String cnum = JOptionPane.showInputDialog(null, "Please ente thr Employess Contact Number", JOptionPane.QUESTION_MESSAGE);
                String address  = JOptionPane.showInputDialog(null, "Please enter Employess  Address", JOptionPane.QUESTION_MESSAGE);
                String dep = JOptionPane.showInputDialog(null, "Please enter Employess Deparment ", JOptionPane.QUESTION_MESSAGE);
                String sal = JOptionPane.showInputDialog(null, "Please enter Employess  salary", JOptionPane.QUESTION_MESSAGE);

            JOptionPane.showConfirmDialog(null, id + sname +  "Records have been entered ");

       try{

    String host = "jdbc:derby://localhost/ee ";
    String uName = "aa";
    String uPass = "bb";
Connection con = DriverManager.getConnection(host, uName, uPass);
//Connection con = DriverManager.getConnection(host);
//Customer Lookup by Last Name

//SQL

String sql =  "INSERT INTO emp (id, fname, sname, pss, cnum,  address,  dep, sal) VALUES (?,?,?,?,?,?,?,? )";

prest = con.prepareStatement(sql);

// Variable Customer Name Passes up to SQL as ?
    prest.setString(1,id);
    prest.setString(2,fname);
        prest.setString(3,sname);
    prest.setString(4,pss);
    prest.setString(5,cnum);
    prest.setString(6,address);
        prest.setString(7,dep);
        prest.setString(8,sal);
    prest.executeUpdate();


    }
    catch (SQLException err) {

    System.out.println(err.getMessage());
    }



} else if (Option.equalsIgnoreCase("B")) {

 String id = JOptionPane.showInputDialog(null, "Please enter the Employess ID", JOptionPane.QUESTION_MESSAGE);

                String fname = JOptionPane.showInputDialog(null, "Please enter Employess First NAME", JOptionPane.QUESTION_MESSAGE);
                String sname = JOptionPane.showInputDialog(null, "Please enter Employess Surname NAME", JOptionPane.QUESTION_MESSAGE);
                String pss = JOptionPane.showInputDialog(null, "Please enter the Employess PPS Number", JOptionPane.QUESTION_MESSAGE);   
                String cnum = JOptionPane.showInputDialog(null, "Please ente thr Employess Contact Number", JOptionPane.QUESTION_MESSAGE);
                String address  = JOptionPane.showInputDialog(null, "Please enter Employess  Address", JOptionPane.QUESTION_MESSAGE);
                String dep = JOptionPane.showInputDialog(null, "Please enter Employess Deparment ", JOptionPane.QUESTION_MESSAGE);
                String sal = JOptionPane.showInputDialog(null, "Please enter Employess  salary", JOptionPane.QUESTION_MESSAGE);

            JOptionPane.showConfirmDialog(null, id + sname +  "Records have been entered ");

       try{

    String host = "jdbc:derby://localhost/ee ";
    String uName = "aa";
    String uPass = "bb";
Connection con = DriverManager.getConnection(host, uName, uPass);
//Connection con = DriverManager.getConnection(host);
//Customer Lookup by Last Name

//SQL

String sql = "UPDATE emp SET  fname = [?],sname = [?],pss = [?],cnum = [?],address = [?],dep = [?],sal =[?], where id = [?]";



prest = con.prepareStatement(sql);


    prest.setString(1,id);
    prest.setString(2,fname);
        prest.setString(3,sname);
    prest.setString(4,pss);
    prest.setString(5,cnum);
    prest.setString(6,address);
        prest.setString(7,dep);
        prest.setString(8,sal);
    prest.executeUpdate();


    }
    catch (SQLException err) {

    System.out.println(err.getMessage());
    }
    }
                }

    }    

0 个答案:

没有答案