?? ctypeandpre.java
字號:
package file1;
/*
* 功能描述:所有對客戶級別的操作的入口類
* @Author:黃順武
* Create Time:---
* Last Modified:2007-12-15
* Modify Reason:數據庫連接類DBConnection 的內部結構設計得到優化
*/
import java.sql.*;
import javax.swing.*;
import sun.jdbc.rowset.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CTypeAndPre extends JPanel implements ActionListener {
private JLabel cGrade = new JLabel("客戶級別:");
private JLabel pre = new JLabel("優惠標準:");
private JLabel firstPay = new JLabel("首付比例:");
private JTextField gradeTF = new JTextField(10);;
private JTextField preTF = new JTextField(10);
private JTextField firstPayTF = new JTextField(10);
private JPanel p1 = new JPanel();
private JTable recTable = null;
private JScrollPane recScrollPane = null;
private JPanel p2 = new JPanel();
private JButton add = new JButton("增加記錄");
private JButton delete = new JButton("刪除記錄");
private String[] head = { "ID", "客戶級別", "優惠標準", "首付比例" };
private int headNum = 0;
private String[][] data = null;
private String[] grade_ids = null;
public CTypeAndPre() {
headNum = head.length;
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 5));
p1.add(cGrade);
p1.add(gradeTF);
gradeTF.setBorder(null);
gradeTF.setBackground(Color.WHITE);
preTF.setBorder(null);
preTF.setBackground(Color.white);
p1.add(pre);
p1.add(preTF);
firstPayTF.setBorder(null);
firstPayTF.setBackground(Color.WHITE);
p1.add(firstPay);
p1.add(firstPayTF);
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
p2.add(add);
p2.add(delete);
add.setBorder(null);
add.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
delete.setBackground(Color.lightGray);
doIt();
add.addActionListener(this);
delete.addActionListener(this);
}
private void doIt() {
try {
DBConnection con = new DBConnection();
String sql = "select* from CGrade";
CachedRowSet crs = con.getResultSet(sql);
int row = 0;
while (crs.next()) {
row++;
}
if(row==0){
delete.setEnabled(false);
}else{
delete.setEnabled(true);
}
grade_ids = new String[row];
data = new String[row][headNum];
crs.beforeFirst();
row = 0;
while (crs.next()) {
grade_ids[row] = String.valueOf(crs.getInt(1));
data[row][0] = String.valueOf(crs.getInt(1));
data[row][1] = crs.getString(2);
data[row][2] = String.valueOf(crs.getFloat(3));
data[row][3] = String.valueOf(crs.getFloat(4));
row++;
}
recTable = new JTable(data, head);
recScrollPane = new JScrollPane(recTable);
this.setLayout(new BorderLayout(0, 15));
this.add(p1, BorderLayout.NORTH);
this.add(recScrollPane, BorderLayout.CENTER);
this.add(p2, BorderLayout.SOUTH);
this.validate();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == add) {
String grade = gradeTF.getText().trim();
if (grade.equals("")) {
JOptionPane.showMessageDialog(null, "客戶級別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (preTF.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "優惠標準不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (firstPayTF.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "首付比例不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
float pre = 0;
float ficrstPay = 0;
try {
pre = Float.valueOf(preTF.getText().trim());
ficrstPay = Float.valueOf(firstPayTF.getText().trim());
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "優惠標準和首付比例均必須為小數或1!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (pre <= 0 || ficrstPay <= 0) {
JOptionPane.showMessageDialog(null, "優惠標準和首付比例均必須為正數!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String query = "select* from CGrade where grade='" + grade + "'";
CachedRowSet crs = null;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "該級別已經存在!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insert = "insert into CGrade values('" + grade + "',"
+ pre + "," + ficrstPay + ")";
con.addSql(insert);
con.doDML();
doIt();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (e.getSource() == delete) {
String gradeidGet = (String) JOptionPane.showInputDialog(null,
"請選擇要刪除的客護級別ID!", "", JOptionPane.INFORMATION_MESSAGE,
null, grade_ids, grade_ids[0]);
if (gradeidGet == null) {
return;
}
try {
DBConnection con = new DBConnection();
int confirm = JOptionPane.showConfirmDialog(null, "您真的確認刪除嗎?",
"", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
String delete = "delete from CGrade where id="
+ gradeidGet;
con.addSql(delete);
con.doDML();
doIt();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -