package src01;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
public class Menu extends AbstractTableModel implements ActionListener{
public static String title[]={"单号","发货人","收货人","发货地址","收货地址","手机号","创建时间"};
public static Object inf[][]={{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""}};
public static final String datd="com.mysql.jdbc.Driver";
public static final String datu="jdbc:mysql://localhost:3306/express";
public static final String dname="root";
public static final String dpass="123456";
public static final String sql="select id,fname,sname,faddress,saddress,number,ctime from expressInfo";
JFrame f=new JFrame("快递信息管理系统");
JButton display=new JButton("显示");
JButton search=new JButton("查找");
JButton modify=new JButton("修改");
JButton adda=new JButton("添加");
JButton delete=new JButton("删除");
JButton save=new JButton("保存文件");
JButton quit=new JButton("退出");
JTable tab=null;
DefaultTableModel tabmo=null;
Connection con=null; //数据库连接
Statement sta=null; //数据库操作
ResultSet rs=null; //保存查询结果
PrintStream ps=null;
public Menu() throws Exception{
Class.forName(datd); //加载驱动程序
con=DriverManager.getConnection(datu,dname,dpass);
sta=con.createStatement();
tabmo=new DefaultTableModel(inf,title);
tab=new JTable(tabmo);
JScrollPane js=new JScrollPane(tab);
JPanel jp=new JPanel();
jp.add(display);
jp.add(search);
jp.add(modify);
jp.add(adda);
jp.add(delete);
jp.add(save);
jp.add(quit);
f.add(jp,BorderLayout.NORTH);
f.add(js,BorderLayout.CENTER);
f.setSize(880,500);
f.setLocation(250,70);
f.setVisible(true);
display.addActionListener(this);
search.addActionListener(this);
modify.addActionListener(this);
adda.addActionListener(this);
delete.addActionListener(this);
save.addActionListener(this);
quit.addActionListener(this);
}
public int getColumnCount(){
return title.length;
}
public int getRowCount(){
return inf.length;
}
public Object getValueAt(int row,int col){
return inf[row][col];
}
public String getColumnName(int col){
return title[col];
}
public Class<?>getColumnClass(int col){
return this.getValueAt(0,col).getClass();
}
public boolean isCellEditable(int row,int col){
return false;
}
public void setValueAt(Object newv,int row,int col){
this.inf[row][col]=newv;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==display){
try{
this.show(sql,0,"");
}catch(Exception ex){}
}
if(e.getSource()==search){
try{
f.dispose();
new Search();
if(tabmo.getValueAt(0,0).equals("")){
JOptionPane.showMessageDialog(null,"数据库中不存在你查找的快递信息,请重新输入!","提示!",JOptionPane.INFORMATION_MESSAGE); ;
f.dispose();
}
}catch(Exception ex){}
}
if(e.getSource()==modify){
try{
f.dispose();
new Modify();
}catch(Exception ex){}
}
if(e.getSource()==adda){
try{
f.dispose();
new Adda();
}catch(Exception ex){}
}
if(e.getSource()==delete){
try{
f.dispose();
new Delete();
}catch(Exception ex){}
}
if(e.getSource()==save){
try{
this.Save();
JOptionPane.showMessageDialog(null,"保存文件成功,可以在D盘根目录查看文件!","提示!",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception ex){}
}
if(e.getSource()==quit){
try{
sta.close();
con.close();
System.exit(1);
}catch(Exception ex){}
}
}
public void show(String s,int p,String t)throws Exception{ //p=0:只显示数据,p=1:update数据在显示
//con=DriverManager.getConnection(datu,dname,dpass); //s:要显示的sql,t:数据库更新语句
//sta=con.createStatement();
if(p==0){
rs=sta.executeQuery(s); //实例化查询结果对象
}else{
sta.executeUpdate(t); //执行数据库更新操作
rs=sta.executeQuery(s);
}
int id;
String fname=null;
String sname=null;
String faddress=null;
String saddress=null;
String number=null;
String ctime=null;
int i=0;
while(rs.next()){
id=rs.getInt("id");
fname=rs.getString("fname");
sname=rs.getString("sname");
faddress=rs.getString("faddress");
saddress=rs.getString("saddress");
number=rs.getString("number");
ctime=rs.getString("ctime");
tabmo.setValueAt(id,i,0);
tabmo.setValueAt(fname,i,1);
tabmo.setValueAt(sname,i,2);
tabmo.setValueAt(faddress,i,3);
tabmo.setValueAt(saddress,i,4);
tabmo.setValueAt(number,i,5);
tabmo.setValueAt(ctime,i,6);
i+=1;
if (i>=tabmo.getRowCount()) {
tabmo.addRow(new Object[]{});
}
}
//sta.executeUpdate("create table stuce;");
}
public void Save()throws Exception{
String id[]=new String[1000];
String fname[]=new String[1000];
String sname[]=new String[1000];
String faddress[]=new String[1000];
String saddress[]=new String[1000];
String number[]=new String[1000];
String ctime[]=new String[1000];
int i=0;
Writer out= new FileWriter(new File("d:"+File.separator+"express.txt"));
rs=sta.executeQuery(sql);
while(rs.next()){
id[i]=rs.getString("id");
fname[i]=rs.getString("fname");
sname[i]=rs.getString("sname");
faddress[i]=rs.getString("faddress");
saddress[i]=rs.getString("saddress");
number[i]=rs.getString("number");
ctime[i]=rs.getString("ctime");
i+=1;
}
for(int j=0;j<i;j++){
out.write(id[j]+"\t");
out.write(fname[j]+"\t");
out.write(sname[j]+"\t");
out.write(faddress[j]+"\t");
out.write(saddress[j]+"\t");
out.write(number[j]+"\t");
out.write(ctime[j]);
out.write("\r");
}
out.close();
}
}