📄 houseinfo.java
字号:
ResultSet rs = db.getResult(sql);
if (rs.first()) {
ID = rs.getString("ID");
}
else {
rs.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
return ID;
}
//根据房主姓名得到房主ID
//与房产坐落类似,住房信息表中住户信息项存储的是房主ID,所以在保存时首先要将房主姓名转化为房主ID
String getOwnerID(String owner) {
String sql = "select * from OwnerInfo where name='" + owner + "'";
String ID = new String();
try {
ResultSet rs = db.getResult(sql);
if (rs.first()) {
ID = rs.getString("ID");
}
else {
//当用户所填写房主姓名在数据库中不存在时,提示用户首先增加该房主信息
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "当前住户不存在,请先增加其基本信息!");
rs.close();
return "";
}
}
catch (Exception e) {
e.printStackTrace();
}
return ID;
}
void groupSetEnabled(boolean enabled) {
txtArea.setEnabled(enabled);
txtBuyPrice.setEnabled(enabled);
txtRoomNum.setEnabled(enabled);
txtBuyTime.setEnabled(enabled);
txtHouseName.setEnabled(enabled);
txtYTArea.setEnabled(enabled);
txtOwner.setEnabled(enabled);
cboHouseLocation.setEnabled(enabled);
cboHeading.setEnabled(enabled);
cboUseType.setEnabled(enabled);
}
void btnSetEnabled(boolean add, boolean edit, boolean delete, boolean save) {
btnAdd.setEnabled(add);
btnEdit.setEnabled(edit);
btnDelete.setEnabled(delete);
btnSave.setEnabled(save);
}
void clearData() {
txtArea.setText("");
txtBuyPrice.setText("");
txtRoomNum.setText("");
txtBuyTime.setText("");
txtHouseName.setText("");
txtYTArea.setText("");
txtOwner.setText("");
cboHouseLocation.setSelectedItem(null);
cboHeading.setSelectedItem(null);
cboUseType.setSelectedItem(null);
}
boolean checkData() {
//坐落是否为空
if (cboHouseLocation.getSelectedIndex() < 0) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "房屋坐落不能为空!");
return false;
}
//房间号是否为空
if (txtHouseName.getText().trim().equals("")) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "房间号不能为空!");
txtHouseName.setFocusable(true);
return false;
}
return true;
}
void btnSave_actionPerformed(ActionEvent e) {
if (!checkData()) {
return;
}
String sql = new String(); //数据库操作语句
String id = new String(); //住房ID
String locationID = new String(); //坐落ID
String name = new String(); //房间号
String heading = new String();//朝向
String roomNum = new String();//间数
String useType = new String();//使用方式
String buyTime = new String();//购买时间
String buyPrice = new String();//购买价格
String area = new String();//面积
String ytArea = new String();//阳台面积
String ownerID = new String();//住户ID
//调用getLocationID函数根据坐落得到坐落ID
locationID = getLocationID(cboHouseLocation.getSelectedItem().toString());
//房主姓名不为空时,得到房主ID
if (!txtOwner.getText().equals("")) {
ownerID = getOwnerID(txtOwner.getText());
//如果房主ID为空,即该房主信息在数据库中不存在,跳出保存过程
if (ownerID.equals("")) {
return;
}
}
if (cboHeading.getSelectedIndex() >= 0) { //朝向
heading = db.CodeDesConvert("Heading", "",cboHeading.getSelectedItem().toString(), 0);
}
if (cboUseType.getSelectedIndex() >= 0) { //使用方式
useType = db.CodeDesConvert("UseType", "",cboUseType.getSelectedItem().toString(), 0);
}
if (!txtRoomNum.getText().equals("")) { //间数
roomNum = txtRoomNum.getText().trim();
}
if (!txtBuyPrice.getText().equals("")) { //购房价格
buyPrice = txtBuyPrice.getText().trim();
}
if (!txtArea.getText().equals("")) { //面积
area = txtArea.getText().trim();
}
if (!txtYTArea.getText().equals("")) { //使用面积
ytArea = txtYTArea.getText().trim();
}
if (operType.equals("add")) {
//得到新住房ID
id = db.getID("HouseInfo","ID");
setCurID(id);
//生成插入语句
sql = "insert into HouseInfo values('" + id + "','" + locationID + "','" +
txtHouseName.getText() + "','" + heading + "','" + roomNum + "','" +
useType + "','" + txtBuyTime.getText() + "','" + buyPrice +
"','" + area + "','" + ytArea + "','" + ownerID + "')";
}//生成更新语句
else if (operType.equals("edit")) {
sql = "update HouseInfo set BuildID='" + locationID + "',";
sql = sql + " Name='" + txtHouseName.getText() + "',";
sql = sql + " Heading='" + heading + "',";
sql = sql + " Number='" + roomNum + "',";
sql = sql + " UseType='" + useType + "',";
sql = sql + " BuyTime='" + txtBuyTime.getText() + "',";
sql = sql + " BuyPrice='" + buyPrice + "',";
sql = sql + " Area='" + area + "',";
sql = sql + " YTArea='" + ytArea + "',";
sql = sql + " OwnerID='" + ownerID + "'";
sql = sql + " where ID='" + curID + "'";
}
if (db.executeSql(sql)) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "新增住房信息成功!");
groupSetEnabled(false);
btnSetEnabled(true, true, true, false);
setOperType("none");
}
else {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "新增住房信息失败!");
}
}
void btnEdit_actionPerformed(ActionEvent e) {
groupSetEnabled(true);
btnSetEnabled(true, true, true, true);
setOperType("edit");
}
void btnDelete_actionPerformed(ActionEvent e) {
if (!curID.equals("")) {
int ir = CommonDialog.showDialog(3, "房产管理系统", "确定要删除当前记录吗?");
if (ir == 2) {
return;
}
String sql = "delete from HouseInfo where ID='" + curID + "'";
if (db.executeSql(sql)) {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "删除记录成功!");
clearData();
groupSetEnabled(false);
btnSetEnabled(true, false, false, false);
setOperType("none");
}
else {
CommonDialog.showDialog(CommonDialog.OK, "房产管理系统", "删除记录失败,请重试!");
}
}
}
}
class HouseInfo_txtArea_actionAdapter
implements java.awt.event.ActionListener {
HouseInfo adaptee;
HouseInfo_txtArea_actionAdapter(HouseInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.txtArea_actionPerformed(e);
}
}
class HouseInfo_btnExit_actionAdapter
implements java.awt.event.ActionListener {
HouseInfo adaptee;
HouseInfo_btnExit_actionAdapter(HouseInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnExit_actionPerformed(e);
}
}
class HouseInfo_btnAdd_actionAdapter
implements java.awt.event.ActionListener {
HouseInfo adaptee;
HouseInfo_btnAdd_actionAdapter(HouseInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnAdd_actionPerformed(e);
}
}
class HouseInfo_btnSave_actionAdapter
implements java.awt.event.ActionListener {
HouseInfo adaptee;
HouseInfo_btnSave_actionAdapter(HouseInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnSave_actionPerformed(e);
}
}
class HouseInfo_btnEdit_actionAdapter
implements java.awt.event.ActionListener {
HouseInfo adaptee;
HouseInfo_btnEdit_actionAdapter(HouseInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnEdit_actionPerformed(e);
}
}
class HouseInfo_btnDelete_actionAdapter
implements java.awt.event.ActionListener {
HouseInfo adaptee;
HouseInfo_btnDelete_actionAdapter(HouseInfo adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnDelete_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -