📄 virtualperson.java
字号:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Virtualperson extends People {
protected DBOperator dbOperator;
public Virtualperson(String name, String address, String password,
String account, String pin) {
super(name, address, password, account, pin);
dbOperator = new DBOperator("jdbc:odbc:VCDB");
}
public Virtualperson(String name, String address, String password,
String account) {
super(name, address, password, account, null);
dbOperator = new DBOperator("jdbc:odbc:VCDB");
}
public boolean add() {
try {
Connection conn = dbOperator.getConnection();
PreparedStatement stat = conn
.prepareStatement("SELECT * FROM Virtualperson WHERE Virtualname = ?");
stat.setString(1, this.GetName());
ResultSet result = stat.executeQuery();
if (result.next()) {
System.out.println("The Name has already exist!");
return false;
} else {
stat =conn.prepareStatement("insert into Virtualperson(Password, Virtualname ,Address,Account) values(?, ?,?,?)");
stat.setString(1, this.GetPassword());
stat.setString(2, this.GetName());
stat.setString(3, this.GetAddress());
stat.setString(4, this.GetAccount());
stat.executeUpdate();
}
conn.close();
addOperator("add", 0, this.GetName(), "Virtualperson");
return true;
} catch (SQLException ex) {
System.out.println(ex);
return false;
}
}
public synchronized void delete() {
try {
// Delete the record from the database
Connection conn = dbOperator.getConnection();
PreparedStatement stat = conn
.prepareStatement("SELECT * FROM Virtualperson WHERE Virtualname = ?");
stat.setString(1, this.GetName());
ResultSet result = stat.executeQuery();
result.deleteRow();
conn.close();
addOperator("delete", 0, "VirtualCommunity", this.GetName());
// refreshResultSet();
// Remove the row in the table
// tableModel.removeRow(
// listSelectionModel.getLeadSelectionIndex());
} catch (SQLException ex) {
// jlblStatus.setText(ex.toString());
System.out.println(ex);
}
}
/* public String changemessage(String name, String password,String Address,String Account) throws SQLException {
Connection conn = dbOperator.getConnection();
try {
PreparedStatement stat = conn
.prepareStatement("SELECT * FROM Virtualperson WHERE Virtualname = ?");
stat.setString(1, name);
ResultSet result = stat.executeQuery();
if (result.next()) {
System.out.println("The Name has already exist!");
conn.close();
return "The Name has already exist!";
}
stat = conn.prepareStatement("SELECT * FROM Virtualperson WHERE Virtualname = ? and Password = ?");
stat.setString(1, this.GetName());
stat.setString(2, this.GetPassword());
ResultSet result1 = stat.executeQuery();
if (!result1.next()) {
System.out.println("The Name are not exist!");
conn.close();
return "Your had not register a member or you inputed a wrong password!";
}
else {
stat =conn.prepareStatement("UPDATE Virtualperson"
+ " SET Virtualname = ? and Password = ? and Address = ? and Account = ?"
+ " WHERE Virtualname = ? and Password = ?");
stat.setString(1, name);
stat.setString(2, password);
stat.setString(3, Address);
stat.setString(4, Account);
stat.setString(5, this.GetName());
stat.setString(6, this.GetPassword());
stat.executeUpdate();
}
conn.close();
addOperator("Changemessage", 0, name, "Virtualperson");
return "Succeed in changing message!";
} catch (SQLException ex) {
System.out.println(ex);
return "Sorry,there\'s something wrong witn SQL,please retry letter!";
}
}*/
protected void addOperator(String operator, double money,
String object, String goods) {
try {
Connection conn = dbOperator.getConnection();
PreparedStatement stat = conn
.prepareStatement("insert into Operator(Virtualname, operator, money, object,goods) values (?, ?, ?,?,?)");
stat.setString(1, this.GetName());
stat.setString(2, operator);
stat.setDouble(3, money);
stat.setString(4, object);
stat.setString(5, goods);
stat.executeUpdate();
conn.close();
} catch (SQLException ex) {
System.out.println(ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -