📄 dbpanel.java
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import edu.njust.cs.*;
public class DBPanel extends CommonPanel{
public DBPanel(MainApp f,Connection con){
super(f,con);
f.labStatusContent.setText(" 您正在进行电表资料维护");
columnNames=new String[] {"电表编号","企业名称",
"安装地址","费率ID","抄表班ID","收费班ID","备注"};
dataType=new Class[] {String.class,String.
class,String.class,String.class,String.class,String.class,String.class};
createTable();
//按需要设置每列宽度
this.setTableColumnWidth(0,100);
this.setTableColumnWidth(1,150);
this.setTableColumnWidth(2,200);
this.setTableColumnWidth(3,80);
this.setTableColumnWidth(4,80);
this.setTableColumnWidth(5,80);
this.setTableColumnWidth(6,200);
}
public Object []constructLineForDBAdd(DBEditor d){
Object []lineForDBAdd=new Object[model.getColumnCount()];
lineForDBAdd[0]=d.getDBID();
lineForDBAdd[1]=d.getDBName();
lineForDBAdd[2]=d.getAddr();
lineForDBAdd[3]=d.getFLID();
lineForDBAdd[4]=d.getCBBID();
lineForDBAdd[5]=d.getSFBID();
lineForDBAdd[6]=d.getMemo();
return lineForDBAdd;
}
public Object []constructLineForDBUpdate(DBEditor d){
Object []lineForDBUpdate=new Object[model.getColumnCount()];
lineForDBUpdate[0]=d.getDBName();
lineForDBUpdate[1]=d.getAddr();
lineForDBUpdate[2]=d.getFLID();
lineForDBUpdate[3]=d.getCBBID();
lineForDBUpdate[4]=d.getSFBID();
lineForDBUpdate[5]=d.getMemo();
lineForDBUpdate[6]=d.getDBID();
return lineForDBUpdate;
}
public void add(){
DBEditor d=new DBEditor(father,"新增",true);
d.show();
if(d.getActionCode()==EditorAction.OK){
String querySql="select * from DBT where DBID=?";
Object []keys={d.getDBID()};
if(SqlUtil.isRecordExist(con,querySql,keys))
{
JOptionPane.showMessageDialog(this,
"该电表编号已经存在!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insertSql="insert into DBT VALUES (?,?,?,?,?,?,?)";
Object []lineForDBAdd=constructLineForDBAdd(d);
if(SqlUtil.addRowToDB(con,insertSql,lineForDBAdd)){
Object []lineForTable=SqlUtil.getLineForTableFromLineForDB(
lineForDBAdd,dataType);
model.addRow(lineForTable);
table.changeSelection(model.getRowCount()-1,0,false,false);
}
else
JOptionPane.showMessageDialog(this,"新增时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
public void modify()
{
int selected=table.getSelectedRow();
if(selected>=0&&selected<model.getRowCount()){
DBEditor d=new DBEditor(father,"修改",true);
d.show();
if(d.getActionCode()==EditorAction.OK){
String updateSql="update DBT set DBName=?,DBAddr=?,"+
" FLID=?, CBBID=?,SFBID=?, DBMEMO=? "+
" WHERE DBID=?";
Object [] lineForDBUpdate=constructLineForDBUpdate(d);
if(SqlUtil.updateRowInDB(con,updateSql,lineForDBUpdate)){
selected=table.getSelectedRow();
model.removeRow(selected);
Object []lineForTable=SqlUtil.getLineForTableFromLineForDB(
constructLineForDBAdd(d),dataType);
model.insertRow(selected,lineForTable);
table.changeSelection(selected,0,false,false);
}
else
JOptionPane.showMessageDialog(this,"修改属性时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
JOptionPane.showMessageDialog(this,"请先选定需修改的记录!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
//查询
public void search(){
//创建查询条件面板
final JTextField txtName=new JTextField();
txtName.addActionListener(tfl);
JTextField txtAddr=new JTextField();
txtAddr.addActionListener(tfl);
JPanel sp=new JPanel();
sp.setLayout(new GridBagLayout());
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,0,1,1,new JLabel("电表名称"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,1,1,1,txtName);
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,2,1,1,new JLabel("安装地址"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,3,1,1,txtAddr);
//txtName获得输入焦点
SwingUtilities.invokeLater(new Runnable(){
public void run(){
txtName.requestFocus();
}
});
//创建查询对话框
String[] options = { "确定", "取消"};
int result = JOptionPane.showOptionDialog(
father, // 父组件
sp, // 查询条件面板
"查询条件", // 对话框标题
JOptionPane.DEFAULT_OPTION, // 选项类型
JOptionPane.QUESTION_MESSAGE, // 消息类型
null, //图标
options, // 按钮
options[0] // 缺省按钮
);
if(result==JOptionPane.OK_OPTION){ //用户按下确定按钮
String querySql="select * from DBT where DBName like '%"+
txtName.getText()+"%' and "+
"DBAddr like '%"+txtAddr.getText()+"%'";
System.out.println(querySql);
SqlUtil.readDBToTable(con,querySql,model,dataType);
}
}
//删除指定记录
public void delete(){
int selected=table.getSelectedRow();
if(selected>=0&&selected<model.getRowCount()){
int selection=JOptionPane.showConfirmDialog (this,
"您确定删除预该条记录?",
"确认",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (selection==JOptionPane.YES_OPTION)
{
String strIDToDelete=model.getValueAt(selected,0).toString().trim();
String deleteSql="delete from DBT where DBID=?";
Object []keys={strIDToDelete};
if(SqlUtil.deleteFromDB(con,deleteSql,keys))
model.removeRow(selected);
else
JOptionPane.showMessageDialog(this,"删除时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
JOptionPane.showMessageDialog(this,"请选定一行,然后再删!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
//内部类,用于显示/编辑电表信息的一个模式对话框
class DBEditor extends CommonEditor {
private JLabel labDBID=new JLabel(" 电表编号 ");
private JTextField txtDBID=new JTextField();
private JLabel labDBName=new JLabel(" 电表名称 ");
private JTextField txtDBName=new JTextField();
private JLabel labAddr=new JLabel(" 安装地址 ");
private JTextField txtAddr=new JTextField();
private JLabel labFLID=new JLabel(" 费率ID ");
private JTextField txtFLID=new JTextField();
private JLabel labCBBID=new JLabel(" 抄表班ID ");
private JTextField txtCBBID=new JTextField();
private JLabel labSFBID=new JLabel(" 收费班ID ");
private JTextField txtSFBID=new JTextField();
private JLabel labMemo=new JLabel(" 备 注 ");
private JTextArea txtMemo=new JTextArea();
public DBEditor(MainApp f,String s,boolean b){
super(f,s,b,DBPanel.this.table);
//面板p中显示电表信息
JPanel p=new JPanel();
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.setLayout(new GridBagLayout());
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,0,1,1,labDBID,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,0,1,1,txtDBID,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,0,1,1,labDBName,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,0,1,1,txtDBName,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,1,1,1,labAddr,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,1,3,1,txtAddr,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,2,1,1,labFLID,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,2,1,1,txtFLID,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,2,1,1,labCBBID,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,2,1,1,txtCBBID,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,3,1,1,labSFBID,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,3,1,1,txtSFBID,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,4,1,1,labMemo,insets);
LayoutUtil.add(p,GridBagConstraints.BOTH,
GridBagConstraints.CENTER,100,100,0,5,4,1,
new JScrollPane(txtMemo),insets);
txtDBID.addActionListener(tfl);
txtDBName.addActionListener(tfl);
txtAddr.addActionListener(tfl);
txtFLID.addActionListener(tfl);
txtCBBID.addActionListener(tfl);
txtSFBID.addActionListener(tfl);
getContentPane().add(p,BorderLayout.CENTER);
//将对话框窗口定位在父窗口的居中位置
setSizeAndPosition(this,450,250);
//依据增加或是修改,设置pre next按钮是否可用
setPreNextButton(s);
if(!s.equals("新增"))
this.txtDBID.setEditable(false);
}
//将表格中选中的记录读入编辑框
public void initEditor(){
int selected=table.getSelectedRow();
this.txtDBID.setText((String)model.getValueAt(selected,0));
this.txtDBName.setText((String)model.getValueAt(selected,1));
this.txtAddr.setText((String)model.getValueAt(selected,2));
this.txtFLID.setText((String)model.getValueAt(selected,3));
this.txtCBBID.setText((String)model.getValueAt(selected,4));
this.txtSFBID.setText((String)model.getValueAt(selected,5));
this.txtMemo.setText((String)model.getValueAt(selected,6));
}
public String getDBID(){
return txtDBID.getText().trim();
}
public String getDBName(){
return txtDBName.getText().trim();
}
public String getAddr(){
return txtAddr.getText().trim();
}
public String getFLID(){
return txtFLID.getText().trim();
}
public String getCBBID(){
return txtCBBID.getText().trim();
}
public String getSFBID(){
return txtSFBID.getText().trim();
}
public String getMemo(){
return txtMemo.getText().trim();
}
public int getActionCode(){
return this.actionCode;
}
public void okClicked(){
//雇员编号不为空
if(this.txtDBID.getText().trim().equals("")||
this.txtDBID.getText().trim().length()!=8){
JOptionPane.showMessageDialog(father,
"请设定8位电表编号!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//隐藏对话框
this.actionCode=EditorAction.OK;
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -