DAO层仅使用一些参数来创建对象

时间:2018-09-03 11:12:52

标签: java user-interface model-view-controller crud dao

我正在尝试将具有DAO层的人添加到管理人的管理人中,但是它仅需要一些参数。 人:

wb+

管理员

public class Person {

    protected String name;
    protected String username;
    protected String password;


    public Person() {

        this.name = "";
        this.username = "";
        this.password = "";
        }

    public Osoba( String name, String username, String password) {

        this.name = name;
        this.username = username;
        this.password = password;

    }
    public Person(Person original) {

        this.name = original.name;
        this.username = original.username;
        this.password = original.password;

    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", username="
                + username + ", password=" + password +"]";
    }

}

}

用于创建新管理员的DAO:

public class Admin extends Person  {

    private double salary;


public Administrator() {
    this.name = "";
    this.username = "";
    this.password = "";
    this.salary= 0;

}
public Admin(String name, String username, String password, double salary) {

    this.name = name;   
    this.username = username;
    this.password = password;
    this.salary = salary;

}
public Admin(Admin original) {
    super(original);
    this.name = original.name;
    this.username = original.username;
    this.salary = original.salary;
    this.salary = original.salary;
}


public int getSalary() {
    return salary;
}
public void setSalary(double salary) {
    salary = salary;
}

@Override
public String toString() {
    return "Administrator [salary=" + salary + "]";
}

在DAO上,我正在使用SQLite在数据库中插入数据,其他需要创建的对象(如Book)正在使用此模式

用于插入数据的partsGUI:

    public static int createAdmin(Admin d) {
    int status = 0;
    con = SQLite.dbConnector();
    try {
        PreparedStatement ps = con.prepareStatement("insert into admin values(?,?,?,?)");
        ps.setString(1, d.getName());
        ps.setString(2, d.getUsername());
        ps.setString(3, d.getPassword());
        ps.setString(4, d.getSalary());
        int rs = ps.executeUpdate();

    }catch(Exception e) {e.printStackTrace();}
    return status;
}    

以及将gui与dao连接的部分:

    nameAdmin = new JTextField();
    nameAdmin.setColumns(10);
    korisnickoAdministratora.setBounds(625, 249, 114, 21);
    frame.getContentPane().add(nameAdmin);

    usernameAdmin = new JTextField();
    usernameAdmin.setColumns(10);
    usernameAdmin.setBounds(625, 312, 114, 21);
    frame.getContentPane().add(usernameAdmin);

    passwordAdmin = new JTextField();
    passwordAdmin.setColumns(10);
    passwordAdmin.setBounds(625, 279, 114, 21);
    frame.getContentPane().add(passwordAdmin);

    salaryAdmin = new JTextField();
    salaryAdmin.setColumns(10);
    salaryAdmin.setBounds(625, 344, 114, 21);
    frame.getContentPane().add(salaryAdministratora);

现在的问题是,这使仅具有薪水参数的新管理员和System.out.prinln(d)仅打印以下内容:

    JButton btnAddAdmin = new JButton("AddAdmin");
    btnAddAdmin.setBounds(545, 391, 192, 27);
    frame.getContentPane().add(btnAddAdmin);
    btnAddAdmin.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent e) {
            try {

                double s = Double.parseDouble(salaryAdmin.getText());

                Admin d = new Admin(nameAdmin.getText(),usernameAdmin.getText(),passwordAdmin.getText(),s);
                System.out.println(d);
                AdminDAO.createAdmin(d);
                System.out.println(d);

            }catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }

    }
            );

1 个答案:

答案 0 :(得分:0)

您的Admin构造函数未正确调用父构造函数。而不是:

public Administrator() {
    this.name = "";
    this.username = "";
    this.password = "";
    this.salary= 0;

}
public Admin(String name, String username, String password, double salary) {
    this.name = name;   
    this.username = username;
    this.password = password;
  this.salary = salary;

}
public Admin(Admin original) {
    super(original);
    this.name = original.name;
    this.username = original.username;
    this.salary = original.salary;
    this.salary = original.salary;
}


// person

 public Person() {
    this.name = "";
    this.username = "";
    this.password = "";
}

public Osoba( String name, String username, String password) {
    this.name = name;
    this.username = username;
    this.password = password;
}

public Person(Person original) {
    this.name = original.name;
    this.username = original.username;
    this.password = original.password;
}

您应该执行以下操作:

public Adminstrator() {
    super();
    this.salary = 0;
}

public Administrator(String name, String username, String password, double salary) {
    super(name, username, password);
    this.salary = salary;
}

public Adminstrator(Administrator original) {
    super(original);
    this.salary = salary;
}

此位代码也有误,应将其删除:

public Osoba( String name, String username, String password) {
    this.name = name;
    this.username = username;
    this.password = password;
}

最后,我建议您对继承有所了解。