⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbczpanel.java

📁 参考了JAVA方面MIS系统开发的书籍做的一个 电力收费的系统,包里面是可以直接运行的JAR文件了...SRC是源文件可一些需要的包...数据库是用的MSQLserver2000,"java程序数据
💻 JAVA
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
import edu.njust.cs.*;
public class DBCZPanel extends CommonPanel{	
	public DBCZPanel(MainApp f,Connection con){
		super(f,con);
		f.labStatusContent.setText("  您正在进行电表出帐、审核");		
		columnNames=new String[] {"电表编号","出帐年月",
						"本月用电量","本月应收电费","是否审核通过"};
	    dataType=new Class[] {String.class,String.class,
					 Double.class,Double.class,Boolean.class};
		createTable();
		//将新增按钮定义为出帐功能
		this.btnAdd.setText("  出帐  "); 
		//将编辑按钮定义为审核功能
		this.btnEdit.setText("  审核  ");
		//按需要设置每列宽度		
		this.setTableColumnWidth(0,100);
		this.setTableColumnWidth(1,100);
		this.setTableColumnWidth(2,200);
		this.setTableColumnWidth(3,200);
		this.setTableColumnWidth(4,200);		
		//使用DoubleRender渲染器,使得数值保持两位小数
		TableColumn col=table.getColumn(columnNames[2]);
	    col.setCellRenderer(new DoubleRender());		
		col=table.getColumn(columnNames[3]);
	    col.setCellRenderer(new DoubleRender());
	}
	public Object []constructLineForDBAdd(DBCZEditor d){
		Object []lineForDBAdd=new Object[model.getColumnCount()];
		lineForDBAdd[0]=d.getDBID();
		lineForDBAdd[1]=d.getYearMonth();
		lineForDBAdd[2]=d.getBYDL();
		lineForDBAdd[3]=d.getBYDF();
	    lineForDBAdd[4]=d.getIsChecked();;
		return lineForDBAdd;
	}
	public Object []constructLineForDBUpdate(DBCZEditor d){
		Object []lineForDBUpdate=new Object[model.getColumnCount()];	
		lineForDBUpdate[0]=d.getBYDL();
		lineForDBUpdate[1]=d.getBYDF();
		lineForDBUpdate[2]=d.getIsChecked();
	    lineForDBUpdate[3]=d.getDBID();
		lineForDBUpdate[4]=d.getYearMonth();
		return lineForDBUpdate;
	}
	//出帐
	public void add(){	
		Thread a=new Thread(new Runnable(){
			public void run(){
				account();
			}
			});
		a.start();		
	}	
	//出帐
	public void account()	{		
		//输入出帐年月,例如2005-02
		GregorianCalendar gc=new GregorianCalendar();
		Timestamp t=DateUtil.getTSFormGC(gc);
		String czny=DateUtil.getYMDFromTS(t);
		czny=czny.substring(0,7);		
		czny=JOptionPane.showInputDialog(this,
										 "请输入出帐年月(XXXX-XX)",czny);
		while(czny!=null){
			if(DateUtil.isValidYMFormat(czny))
				break;
			else
				czny=JOptionPane.showInputDialog(this,
										  "请输入出帐年月(XXXX-XX)",czny);
		}
		if(czny==null)
			return;
		//出帐年月前推一个月
		//先变成2005-02-01的格式
		String lastczny=czny+"-01"; 
		Timestamp ts=DateUtil.getTSFromYMD(lastczny);
		gc=DateUtil.getGCFromTS(ts);
		gc.add(Calendar.MONTH,-1);			
		ts=DateUtil.getTSFormGC(gc);
		lastczny=DateUtil.getYMDFromTS(ts).substring(0,7);		
		//读出所有电表到一个临时表格
		String []cn=new String[] {"电表编号"};
	    Class []dt=new Class[] {String.class};
		CustomTableModel tempModel=new CustomTableModel(0,cn.length,cn,dt);
		SqlUtil.readDBToTable(con,"select DBID from DBT",tempModel,dt);		
		//显示等待画面
		SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					showWaitingScreen();
				}
			});				
		this.progressBar.setMaximum(tempModel.getRowCount());
		this.progressBar.setMinimum(0);
		//依次对每块电表出帐
		for(int i=0;i<tempModel.getRowCount();i++){
			this.labProgress.setText("正在对电表"+tempModel.getValueAt(i,0)+"出帐...");
			this.progressBar.setValue(i);
			//当前电表上月读数
			String sql="select DBDS from CBXXT where DBID='"+
					   tempModel.getValueAt(i,0).toString()+"' and "+
                       " CBYearMonth='"+lastczny+"'";
			Double syds=(Double)SqlUtil.readFieldValueFromDB(con,sql);
			//当前电表本月读数
			sql="select DBDS from CBXXT where DBID='"+
				tempModel.getValueAt(i,0).toString()+"' and "+
				" CBYearMonth='"+czny+"'";
			Double byds=(Double)SqlUtil.readFieldValueFromDB(con,sql);
			//取得当前电表的费率
			sql="select FLQty  from FLT left outer join DBT on FLT.FLID=DBT.FLID"+
				" where DBT.DBID='"+tempModel.getValueAt(i,0).toString()+"'";
			Double fl=(Double)SqlUtil.readFieldValueFromDB(con,sql);
			//本月用电量
			double bydl;
			//本月用电费
			double bydf;			
			if(syds!=null&&syds.doubleValue()!=Double.NEGATIVE_INFINITY&&
			   byds!=null&&byds.doubleValue()!=Double.NEGATIVE_INFINITY){
				//有上月读数和本月读数
				//本月用电量=(本月读数-上月读数)
				bydl=byds.doubleValue()-syds.doubleValue();
				if(fl!=null)
					bydf=bydl*fl.doubleValue();
				else
					bydf=Double.NEGATIVE_INFINITY;					
			}else if(byds!=null&&byds.doubleValue()!=Double.NEGATIVE_INFINITY){
				//只有本月读数
				//本月用电量=本月读数
				bydl=byds.doubleValue();
				if(fl!=null)
					bydf=bydl*fl.doubleValue();
				else
					bydf=Double.NEGATIVE_INFINITY;	
			}else{
				bydl=Double.NEGATIVE_INFINITY;		
				bydf=Double.NEGATIVE_INFINITY;		
			}
			//写入电表出帐表
			Object []keys={tempModel.getValueAt(i,0),czny};			
			//未出过帐,则写入出帐信息表,否则忽略
			sql="select * from DBCZT where DBID=? and CZYearMonth=?";
			if(!SqlUtil.isRecordExist(con,sql,keys)){
				Object []lineForDBAdd={tempModel.getValueAt(i,0),
									   czny,new Double(bydl),new Double(bydf),
									   new Boolean(false)};
				sql="insert into DBCZT VALUES (?,?,?,?,?)";
				SqlUtil.addRowToDB(con,sql,lineForDBAdd);
			}
		}
		//读出出帐年月的所有出帐记录
		String readSql="select * from dbczt where CZYearMonth='"+czny+"'";
		SqlUtil.readDBToTable(con,readSql,model,dataType);	
		//隐藏等待画面
		SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					hideWaitingScreen();
				}
			});	
	}
	public void modify(){
		int selected=table.getSelectedRow();		
		if(selected>=0&&selected<model.getRowCount()){
			DBCZEditor d=new DBCZEditor(father,"修改",true);
			d.show();			
			if(d.getActionCode()==EditorAction.OK)	{ 
				String updateSql="update DBCZT set BYDL=?,"+
								 " BYDF=?,isChecked=? WHERE DBID=?"+
								 " and CZYearMonth=?";
				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 txtYearMonth=new JTextField();
		txtYearMonth.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,txtYearMonth);
		//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==0){ //用户按下确定按钮
			 String querySql="select * from DBCZT left outer join DBT "+
							 " ON DBT.DBid=DBCZT.DBID  where "+
							 " DBT.DBName like '%"+txtName.getText()+"%' and "+
							 " DBCZT.CZYearMonth  like '%"+txtYearMonth.getText()+"%'";
			 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 strYearMonthToDelete=model.getValueAt(selected,1).toString().trim();
				String deleteSql="delete from DBCZT where DBID=? and CZYearMonth=?";
				Object []keys={strIDToDelete,strYearMonthToDelete};
				if(SqlUtil.deleteFromDB(con,deleteSql,keys))
					model.removeRow(selected);
				else
					JOptionPane.showMessageDialog(this,"删除时出错!",
												  "提示",
												  JOptionPane.INFORMATION_MESSAGE);
			}
		}
		else
			JOptionPane.showMessageDialog(this,"请选定一行,然后再删!",
										  "提示",
										  JOptionPane.INFORMATION_MESSAGE);		
	}
	//内部类,用于显示/编辑雇员信息的一个模式对话框
	class DBCZEditor extends CommonEditor {			
		private JLabel labDBID=new JLabel("  电表编号  ");
		private JTextField txtDBID=new JTextField();		
		private JLabel labYearMonth=new JLabel("  出帐年月  ");
		private JTextField txtYearMonth=new JTextField();		
		private JLabel labBYDL=new JLabel("  本月电量  ");
		private JTextField txtBYDL=new JTextField();		
		private JLabel labBYDF=new JLabel("    本月电费    ");
		private JTextField txtBYDF=new JTextField();				
		private JCheckBox chkIsChecked=new JCheckBox("审核通过");
		public DBCZEditor(MainApp f,String s,boolean b){
			super(f,s,b,DBCZPanel.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,labYearMonth,insets);
			LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
						   GridBagConstraints.CENTER,100,0,3,0,1,1,txtYearMonth,insets);			
			LayoutUtil.add(p,GridBagConstraints.NONE,
						   GridBagConstraints.CENTER,0,0,0,1,1,1,labBYDL,insets);
			LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
						   GridBagConstraints.CENTER,100,0,1,1,1,1,txtBYDL,insets);
			LayoutUtil.add(p,GridBagConstraints.NONE,
						   GridBagConstraints.CENTER,0,0,2,1,1,1,labBYDF,insets);
			LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
						   GridBagConstraints.CENTER,100,0,3,1,1,1,txtBYDF,insets);			
			LayoutUtil.add(p,GridBagConstraints.NONE,
						   GridBagConstraints.WEST,0,0,0,2,2,1,chkIsChecked,insets);			
			txtBYDL.addActionListener(tfl);
			txtBYDF.addActionListener(tfl);			
		    getContentPane().add(p,BorderLayout.CENTER);			
			//将对话框窗口定位在父窗口的居中位置
			setSizeAndPosition(this,450,150);		
			//依据增加或是修改,设置pre next按钮是否可用
			setPreNextButton(s);
			if(!s.equals("新增")) 	{
				this.txtDBID.setEditable(false);
				this.txtYearMonth.setEditable(false);
			}
		}		
		//将表格中选中的记录读入编辑框
		public void initEditor(){			
			int selected=table.getSelectedRow();		
			this.txtDBID.setText((String)model.getValueAt(selected,0));
			this.txtYearMonth.setText((String)model.getValueAt(selected,1));
			if(model.getValueAt(selected,2)==null)
				this.txtBYDL.setText("");
			else
				this.txtBYDL.setText(df.format(model.getValueAt(selected,2)));			
			if(model.getValueAt(selected,3)==null)
				this.txtBYDF.setText("");
			else
				this.txtBYDF.setText(df.format(model.getValueAt(selected,3)));					
			if(model.getValueAt(selected,4).toString().trim().equals("true"))
				this.chkIsChecked.setSelected(true);
			else
				this.chkIsChecked.setSelected(false);			
		}
		public String getDBID(){
			return txtDBID.getText().trim();
		}
		public String getYearMonth(){
			return txtYearMonth.getText().trim();
		}		
		//将电量、电费由字符串类型转化为Double类型
		//如果不是合法的数值字符串,取值Double.NEGATIVE_INFINITY
		public Double getBYDL(){
			double result=Double.NEGATIVE_INFINITY;
			try{
				result=Double.parseDouble(txtBYDL.getText().trim());
			}catch(NumberFormatException e){
				result=Double.NEGATIVE_INFINITY;
			}
			return new Double(result);
		}
		public Double getBYDF(){
			double result=Double.NEGATIVE_INFINITY;
			try{
				result=Double.parseDouble(txtBYDF.getText().trim());
			}catch(NumberFormatException e){
				result=Double.NEGATIVE_INFINITY;
			}
			return new Double(result);
		}		
		public String getIsChecked(){
			if(this.chkIsChecked.isSelected())
				    return "Y";
			else	return "N";
		}
		public int getActionCode(){
			return this.actionCode;
		}
		public void okClicked(){			
		   //电量、电费为数值类型
			try{
				    Double.parseDouble(this.txtBYDL.getText().trim());
					Double.parseDouble(this.txtBYDF.getText().trim());
			}catch(NumberFormatException e){
					JOptionPane.showMessageDialog(father,
												  "电量或电费为非数值类型!",
												  "提示",
												  JOptionPane.INFORMATION_MESSAGE);	
				    return ;
			}			
			//隐藏对话框
			this.actionCode=EditorAction.OK;
			this.setVisible(false);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -