📄 customermanage.java
字号:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.MessageBox;
import com.swtdesigner.SWTResourceManager;
public class CustomerManage extends Composite {
private Text text;
private Combo combo_1;
private Combo combo;
private MessageBox messageBox;
private Table table;
private String id;
private final String pwd;
private String name = "";
private int c_id;
private boolean lastsearch = true;
/**
* Create the composite
* @param parent
* @param style
*/
public CustomerManage(final Composite parent,final int style,final String id,final String pwd) {
super(parent, style);
this.id = id;
this.pwd = pwd;
this.setBounds(0, 0, 699, 470);
table = new Table(this, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(55, 170, 602, 280);
final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
newColumnTableColumn.setWidth(80);
newColumnTableColumn.setText("客户编号");
final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_1.setWidth(80);
newColumnTableColumn_1.setText("客户姓名");
final TableColumn newColumnTableColumn_2 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_2.setWidth(50);
newColumnTableColumn_2.setText("性别");
final TableColumn newColumnTableColumn_3 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_3.setWidth(100);
newColumnTableColumn_3.setText("联系电话");
final TableColumn newColumnTableColumn_4 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_4.setWidth(100);
newColumnTableColumn_4.setText("地址");
final TableColumn newColumnTableColumn_5 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_5.setWidth(80);
newColumnTableColumn_5.setText("邮编");
final TableColumn newColumnTableColumn_6 = new TableColumn(table, SWT.NONE);
newColumnTableColumn_6.setWidth(100);
newColumnTableColumn_6.setText("E-mail");
final Button button = new Button(this, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
AddCustomer addCustomer = new AddCustomer(parent.getShell(),id,pwd);
addCustomer.open();
table.clearAll();
table.removeAll();
}
});
button.setText("添加客户");
button.setBounds(55, 70, 85, 22);
table.clearAll();
table.removeAll();
final Button button_1 = new Button(this, SWT.NONE);
button_1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
if(table.getSelectionIndices().length>1)
{
messageBox = new MessageBox(parent.getShell(),SWT.ICON_INFORMATION);
messageBox.setMessage("只能选择 1 条查询记录");
messageBox.setText("修改客户信息");
messageBox.open();
}
else
if(table.getSelectionIndices().length<1)
{
messageBox = new MessageBox(parent.getShell(),SWT.ICON_INFORMATION);
messageBox.setMessage("请选择 1 条查询记录");
messageBox.setText("修改客户信息");
messageBox.open();
}
else
{
TableItem temp = table.getItems()[table.getSelectionIndex()];
int cid = Integer.parseInt(temp.getText(0));
String cname = temp.getText(1);
boolean gender = temp.getText(2).equals("男")?true:false;
String phone = temp.getText(3);
String address = temp.getText(4);
String code = temp.getText(5);
String email = temp.getText(6);
EditCustomer editCustomer = new EditCustomer(parent.getShell(),id,pwd,
cid,cname,gender,phone,address,code,email);
editCustomer.open();
if(lastsearch)
searchCustomer(name,combo_1.getSelectionIndex() == 0);
else
searchCustomer(c_id,combo_1.getSelectionIndex() == 0);
}
}
});
button_1.setText("修改客户");
button_1.setBounds(146, 70, 85, 22);
final Button button_2 = new Button(this, SWT.NONE);
button_2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
if(table.getSelectionIndices().length<1)
{
messageBox = new MessageBox(parent.getShell(),SWT.ICON_INFORMATION);
messageBox.setMessage("请选择至少 1 条查询记录");
messageBox.setText("删除客户信息");
messageBox.open();
}
else
{
messageBox = new MessageBox(parent.getShell(),SWT.ICON_QUESTION|SWT.OK|SWT.CANCEL);
messageBox.setMessage("确定要删除这 " + table.getSelectionIndices().length + " 条记录吗?");
messageBox.setText("删除确认");
if(messageBox.open() == SWT.OK)
{
String del_id = "";
for(int i = 0; i < table.getSelectionIndices().length;)
{
TableItem temp = table.getItems()[table.getSelectionIndices()[i]];
del_id = del_id + "'" +temp.getText(0) + "'";
i++;
if(i < table.getSelectionIndices().length)
del_id = del_id + ",";
}
delCustomer(del_id);
if(lastsearch)
searchCustomer(name,combo_1.getSelectionIndex() == 0);
else
searchCustomer(c_id,combo_1.getSelectionIndex() == 0);
messageBox = new MessageBox(parent.getShell(),SWT.ICON_INFORMATION);
messageBox.setMessage("成功删除 " + table.getSelectionIndices().length + " 条记录");
messageBox.setText("删除");
messageBox.open();
}
}
}
});
button_2.setText("删除客户");
button_2.setBounds(237, 70, 85, 22);
final Label label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
label.setBounds(0, 45, 700, 2);
final Label label_5 = new Label(this, SWT.NONE);
label_5.setBounds(165, 132, 85, 17);
label_5.setVisible(false);
text = new Text(this, SWT.BORDER);
text.setBounds(260, 128, 85, 20);
text.setVisible(false);
combo = new Combo(this, SWT.READ_ONLY);
combo.setItems(new String[] {"按客户编号", "按客户姓名"});
combo.setBounds(260, 105, 86, 20);
combo.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(final SelectionEvent e) {
if(combo.getSelectionIndex() == 0)
label_5.setText("请输入客户编号");
else
if(combo.getSelectionIndex() == 1)
label_5.setText("请输入客户姓名");
label_5.setVisible(true);
text.setVisible(true);
}
});
final Label label_1 = new Label(this, SWT.NONE);
label_1.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NONE));
label_1.setText("查询客户:");
label_1.setBounds(55, 108, 80, 17);
combo_1 = new Combo(this, SWT.READ_ONLY);
combo_1.setItems(new String[] {"精确查询", "模糊查询"});
combo_1.setBounds(440, 105, 85, 20);
final Label label_2 = new Label(this, SWT.NONE);
label_2.setAlignment(SWT.RIGHT);
label_2.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NONE));
label_2.setText("请选择查询字段");
label_2.setBounds(130, 108, 120, 17);
final Label label_3 = new Label(this, SWT.NONE);
label_3.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NONE));
label_3.setAlignment(SWT.RIGHT);
label_3.setText("查询方式");
label_3.setBounds(352, 108, 80, 17);
final Button button_6 = new Button(this, SWT.NONE);
button_6.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
table.clearAll();
table.removeAll();
lastsearch = true;
if(combo.getSelectionIndex() == 1)
{
name = text.getText();
searchCustomer(name,combo_1.getSelectionIndex() == 0);
messageBox = new MessageBox(parent.getShell(),SWT.ICON_INFORMATION);
messageBox.setMessage("共找到 " + table.getItemCount() + " 条记录");
messageBox.setText("查询客户信息");
messageBox.open();
lastsearch = true;
}
else
if(combo.getSelectionIndex() == 0)
{
try
{
c_id = Integer.parseInt(text.getText());
searchCustomer(c_id,combo_1.getSelectionIndex() == 0);
messageBox = new MessageBox(parent.getShell(),SWT.ICON_INFORMATION);
messageBox.setMessage("共找到 " + table.getItemCount() + " 条记录");
messageBox.setText("查询客户信息");
messageBox.open();
}
catch(NumberFormatException m)
{
messageBox = new MessageBox(parent.getShell(),SWT.ICON_ERROR);
messageBox.setMessage("客户编号只能为数字\n\n" +
"请重新输入");
messageBox.setText("客户编号");
messageBox.open();
lastsearch = false;
}
}
}
});
button_6.setText("查 询");
button_6.setBounds(542, 104, 85, 22);
final Label label_4 = new Label(this, SWT.NONE);
label_4.setFont(SWTResourceManager.getFont("经典行楷繁", 22, SWT.NONE));
label_4.setAlignment(SWT.CENTER);
label_4.setText("客户信息管理平台");
label_4.setBounds(0, 10, 700, 29);
//
}
public void searchCustomer(String name,boolean exact){//sql test
Connection con;
Statement stmt;
String gender;
try{
table.clearAll();
table.removeAll();
DBCon dbc = new DBCon();
con = DriverManager.getConnection(dbc.url,id,pwd);
stmt = con.createStatement();
String query;
if(exact)
{
query = "select * from Customer where Name = '" + name + "'";
}
else
{
query = "select * from Customer where Name like '%" + name + "%'";
}
ResultSet rs = stmt.executeQuery(query);
while( rs.next()){
new TableItem(table, SWT.BORDER);
if(rs.getString("Gender").equals("0"))
gender = "女";
else
gender = "男";
table.getItem(table.getItemCount()-1).setText(new String[]{rs.getString("ID"),rs.getString("Name"),
gender,rs.getString("Phone"),rs.getString("Address"),rs.getString("Code"),rs.getString("E-mail")});
}
dbc.dbClose(con,rs);
}
catch(Exception e){
e.printStackTrace();
}
}
public void searchCustomer(int c_id,boolean exact){//sql test
Connection con;
Statement stmt;
String gender;
try{
table.clearAll();
table.removeAll();
DBCon dbc = new DBCon();
con = DriverManager.getConnection(dbc.url,id,pwd);
stmt = con.createStatement();
String query;
if(exact)
{
query = "select * from Customer where ID = " + c_id;
}
else
{
query = "select * from Customer where ID like '%" + c_id + "%'";
}
ResultSet rs = stmt.executeQuery(query);
while( rs.next()){
new TableItem(table, SWT.BORDER);
if(rs.getString("Gender").equals("0"))
gender = "女";
else
gender = "男";
table.getItem(table.getItemCount()-1).setText(new String[]{rs.getString("ID"),rs.getString("Name"),
gender,rs.getString("Phone"),rs.getString("Address"),rs.getString("Code"),rs.getString("E-mail")});
}
dbc.dbClose(con,rs);
}
catch(Exception e){
e.printStackTrace();
}
}
public void delCustomer(String c_id){
Connection con;
try{
DBCon dbc = new DBCon();
con = DriverManager.getConnection(dbc.url,id,pwd);
CallableStatement cs = con.prepareCall("{call dbo.sp_DelCustomer(?)}");
cs.setString(1, c_id);
cs.execute();
dbc.dbClose(con);
}
catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -