一.
package mzl;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.io.*;
import javax.swing.table.*;
import javax.swing.text.*;
import java.util.*;
import javax.imageio.*;
import java.applet.*;
public class NameAndAddress extends JFrame
implements ActionListener
{
private JTextArea display;
private JButton delete,find,add,renew,update,look;
private JLabel mean,word,mobile,phone,compony,address;
private JTextField meantext,wordtext,mobiletext,phonetext,comptext,addrtext;
private JPanel jp;
protected JScrollPane jsp;
private JSplitPane jst;
private Connection conn;
protected Statement stmt;
protected JTable jt=new JTable();;
private Vector row=new Vector();
private Vector cols=new Vector();
private Vector headName=new Vector();
private DefaultTableModel dtm=new DefaultTableModel();
UpdateFrame udf=new UpdateFrame(this);
public NameAndAddress()
{
super("名址录");
/// this.add(time);
/**
*设置表格标题
*/
String[] a={"姓名","职位","手机","公司","电话","地址"};
headName=new Vector();
for(int i=0;i<a.length;i++)
{
headName.add(a[i]);
}
dtm.setDataVector(row,headName);
jt.setModel(dtm);
jp=new JPanel();
jp.setLayout(null);
word=new JLabel("姓名:");
word.setBounds(10,10,40,20);
wordtext=new JTextField();
wordtext.setBounds(60,10,60,20);
mean=new JLabel("职位:");
mean.setBounds(130,10,40,20);
meantext=new JTextField();
meantext.setBounds(180,10,60,20);
mobile=new JLabel("手机:");
mobile.setBounds(250,10,40,20);
mobiletext=new JTextField();
mobiletext.setBounds(300,10,60,20);
phone=new JLabel("座机:");
phone.setBounds(370,10,40,20);
phonetext=new JTextField();
phonetext.setBounds(420,10,60,20);
compony=new JLabel("公司:");
compony.setBounds(10,40,40,20);
comptext=new JTextField();
comptext.setBounds(60,40,180,20);
address=new JLabel("地址:");
address.setBounds(250,40,40,20);
addrtext=new JTextField();
addrtext.setBounds(300,40,180,20);
renew=new JButton("重置");
renew.setBounds(220,80,60,20);
renew.addActionListener(this);
delete=new JButton("删除");
delete.setBounds(150,80,60,20);
delete.addActionListener(this);
find=new JButton("刷新");
find.setBounds(10,80,60,20);
find.addActionListener(this);
add=new JButton("添加");
add.setBounds(80,80,60,20);
add.addActionListener(this);
look=new JButton("查询");
look.setBounds(290,80,60,20);
look.addActionListener(this);
update=new JButton("修改");
update.setBounds(360,80,60,20);
update.addActionListener(this);
jp.add(mean);jp.add(word);jp.add(mobile);jp.add(phone);jp.add(compony);jp.add(address);
jp.add(meantext);jp.add(wordtext);jp.add(mobiletext);jp.add(phonetext);jp.add(comptext);
jp.add(addrtext);
jp.add(delete);jp.add(find);jp.add(add);jp.add(renew);jp.add(update);jp.add(look);
jsp=new JScrollPane(jt);
jst=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jp,jsp);
jst.setDividerLocation(110);
jst.setDividerSize(10);
jst.setOneTouchExpandable(true);
this.add(jst);
this.setSize(600,400);
this.setResizable(true);
/**
*服务器端界面居中显示
*/
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setBounds((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2,600,400);
this.setVisible(true);
/**
*窗口关闭
*/
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try
{
stmt.close();
conn.close();
}
catch(SQLException ee)
{
ee.printStackTrace();
}
System.exit(0);
}
});
/**
*连接数据库
*/
try
{
conn = getConnection();
stmt = conn.createStatement();
System.out.println("数据库连接成功");
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
execuiteQuery();
}
public void actionPerformed(ActionEvent e)
{
/**
*重置按钮
**/
if(e.getSource()==renew)
{
wordtext.setText("");
meantext.setText("");
mobiletext.setText("");
phonetext.setText("");
comptext.setText("");
addrtext.setText("");
}
/**
*添加按钮
*/
if(e.getSource()==add)
{
String g=wordtext.getText().trim();
String h=meantext.getText().trim();
String m=mobiletext.getText().trim();
String p=phonetext.getText().trim();
String c=comptext.getText().trim();
String a=addrtext.getText().trim();
ResultSet rs=null;
if(g.equals("")||h.equals("")||m.equals("")||p.equals("")||c.equals("")||a.equals(""))
{
JOptionPane.showMessageDialog(null, "请输入所有信息", "提示",
JOptionPane.WARNING_MESSAGE);
}
else
{
try
{
int b=jt.getSelectedRowCount();
stmt.execute("insert into inform(sname,sjob,smobile,sphone,scompony,saddress) values('"+
g+"','"+h+"','"+m+"','"+p+"','"+c+"','"+a+"')");
row.removeAllElements();
execuiteQuery();
JOptionPane.showMessageDialog(null, "添加成功,请刷新!");
}
catch(Exception eg)
{
System.out.println("add");
eg.printStackTrace();
}
}
}
/**
*删除按钮
*/
if(e.getSource()==delete)
{
int selectedNum=jt.getSelectedRowCount();
int[] selectedRows=jt.getSelectedRows();
if(selectedNum!=0)
{
try
{
if(JOptionPane.showConfirmDialog(this,
"确实要删除该信息吗?/n删除的信息将不能恢复,继续?",
"删除确定", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE) == 0)
{
Object[] selectedName=new Object[selectedNum];
for(int i=0;i<selectedNum;i++)
{
try {
selectedName[i]=jt.getValueAt(selectedRows[i],0);
}
catch (Exception ex) {System.out.println ("ddd");
}
}
for(int i=selectedNum-1;i>=0;i--)
{ System.out.println (selectedName[i]);
stmt.execute("delete from inform where sname="+"'"+selectedName[i]+"'");
row.removeElementAt(selectedRows[i]);
}
dtm.setDataVector(row,headName);
JOptionPane.showMessageDialog(null, "删除成功!");
}
}
catch(Exception ee)
{
System.out.println ("delete");
}
finally
{
System.out.println ("finally");
}
}
else
{
JOptionPane.showMessageDialog(null, "未选中内容");
}
}
/**
*刷新按钮
*/
if(e.getSource()==find)
{
row.removeAllElements();
execuiteQuery();
}
/**
*修改按钮
*/
if(e.getSource()==update)
{
udf.update();
}
/**
*查询按钮
*/
if(e.getSource()==look)
{
String g=wordtext.getText().trim();
String h=meantext.getText().trim();
String m=mobiletext.getText().trim();
String p=phonetext.getText().trim();
String c=comptext.getText().trim();
String a=addrtext.getText().trim();
ResultSet rs;
try
{
if(!g.equals("")||!h.equals("")||!m.equals("")||!p.equals("")||!c.equals("")||!a.equals(""))
{
rs=stmt.executeQuery("select * from inform where sname="+"'"+g+"'"+
"or "+"sjob='"+h+"'"+
"or "+"smobile='"+m+"'"+
"or "+"scompony='"+c+"'"+
"or "+"sphone='"+p+"'"+
"or "+"saddress='"+a+"'"
);
if(!rs.next())
{
JOptionPane.showMessageDialog(null, "未找到符合记录");
}
row.removeAllElements();
while(rs.next())
{
cols=new Vector();
cols.add(rs.getString("sname"));
cols.add(rs.getString("sjob"));
cols.add(rs.getString("smobile"));
cols.add(rs.getString("scompony"));
cols.add(rs.getString("sphone"));
cols.add(rs.getString("saddress"));
row.add(cols);
}
dtm.setDataVector(row,headName);
rs.close();
}
else
{
JOptionPane.showMessageDialog(null, "未填写查询条件");
}
}
catch(SQLException ea)
{
}
}
}
public static Connection getConnection()
throws IOException,SQLException,ClassNotFoundException
{
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=word.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection(strurl,"","") ;
}
/**
*检索数据库
*/
public void execuiteQuery()
{
try
{
ResultSet rs=stmt.executeQuery("select * from inform");
while(rs.next())
{
cols=new Vector();
cols.add(rs.getString("sname"));
cols.add(rs.getString("sjob"));
cols.add(rs.getString("smobile"));
cols.add(rs.getString("scompony"));
cols.add(rs.getString("sphone"));
cols.add(rs.getString("saddress"));
row.add(cols);
}
dtm.setDataVector(row,headName);
rs.close();
}
catch(SQLException e)
{
}
}
/**
*main方法
*/
public static void main(String[] args)
{
NameAndAddress frame=new NameAndAddress();
Toolkit tool=frame.getToolkit();
Image myimage=tool.getImage("images//icon.gif");
frame.setIconImage(myimage);
}
}
二.
package mzl;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.io.*;
import javax.swing.table.*;
import java.util.*;
public class UpdateFrame extends JFrame implements ActionListener
{
JButton ok,no;
JLabel name,job,phone,mobile,address,compony;
JTextField ntext,jtext,ptext,mtext,atext,ctext;
NameAndAddress naa;
int rowNumber;
public UpdateFrame(NameAndAddress naa)
{
super("修改窗口");
this.naa=naa;
ok=new JButton("确定");
ok.setBounds(30,200,60,20);
ok.addActionListener(this);
no=new JButton("取消");
no.setBounds(110,200,60,20);
no.addActionListener(this);
name=new JLabel("姓名:");
name.setBounds(10,10,40,20);
job=new JLabel("职位:");
job.setBounds(10,40,40,20);
phone=new JLabel("手机:");
phone.setBounds(10,70,40,20);
mobile=new JLabel("公司:");
mobile.setBounds(10,100,40,20);
compony=new JLabel("电话:");
compony.setBounds(10,130,40,20);
address=new JLabel("地址:");
address.setBounds(10,160,40,20);
ntext=new JTextField();
ntext.setBounds(60,10,60,20);
jtext=new JTextField();
jtext.setBounds(60,40,60,20);
mtext=new JTextField();
mtext.setBounds(60,70,100,20);
ctext=new JTextField();
ctext.setBounds(60,100,100,20);
ptext=new JTextField();
ptext.setBounds(60,130,160,20);
atext=new JTextField();
atext.setBounds(60,160,160,20);
add(name);add(job);add(phone);add(mobile);add(compony);add(address);
add(ntext);add(jtext);add(ptext);add(mtext);add(ctext);add(atext);
add(ok);add(no);
this.setLayout(null);
this.setBounds(400,200,250,300);
this.setVisible(false);
this.setResizable(false);
this.setAlwaysOnTop(true);
}
public void update()
{
try
{
rowNumber=naa.jt.getSelectedRow();
this.ntext.setText((String)naa.jt.getValueAt(rowNumber,0));
this.jtext.setText((String)naa.jt.getValueAt(rowNumber,1));
this.ptext.setText((String)naa.jt.getValueAt(rowNumber,2));
this.mtext.setText((String)naa.jt.getValueAt(rowNumber,3));
this.ctext.setText((String)naa.jt.getValueAt(rowNumber,4));
this.atext.setText((String)naa.jt.getValueAt(rowNumber,5));
this.setVisible(true);
}
catch(Exception ea)
{
JOptionPane.showMessageDialog(null,"请选择一条记录");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
try
{
naa.stmt.execute("update inform set sname="+"'"+ntext.getText()+"'"+
",sjob="+"'"+jtext.getText()+"'"+
",smobile="+"'"+mtext.getText()+"'"+
",sphone="+"'"+ptext.getText()+"'"+
",scompony="+"'"+ctext.getText()+"'"+
",saddress="+"'"+atext.getText()+"'"+
" where sname="+"'"+(String)naa.jt.getValueAt(rowNumber,0)+"'");
naa.jt.setValueAt(ntext.getText(),rowNumber,0);
naa.jt.setValueAt(jtext.getText(),rowNumber,1);
naa.jt.setValueAt(mtext.getText(),rowNumber,2);
naa.jt.setValueAt(ctext.getText(),rowNumber,3);
naa.jt.setValueAt(ptext.getText(),rowNumber,4);
naa.jt.setValueAt(atext.getText(),rowNumber,5);
this.show(false);
}
catch(Exception ee)
{
System.out.println("dddd");
}
}
if(e.getSource()==no)
{
this.show(false);
}
}
}