我正在尝试将人添加到具有DAO层的固有人员的管理员中,但它只需要一些参数。 人:
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 +"]";
}
}
管理
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:
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;
}
在DAO上,我正在使用SQLite将数据插入数据库,需要创建的其他对象(如Book(正在使用此模式
用于插入数据的零件图形用户界面:
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);
以及将 GUI 与 DAO 连接起来的部分:
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();
}
}
}
);
现在的问题是,这使得新管理员只使用薪水参数和System.out.prinln(d(打印这个:
Admin [salary=123.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;
}
最后,我建议你好好读一读关于继承的知识。