中学java通讯录.docx
《中学java通讯录.docx》由会员分享,可在线阅读,更多相关《中学java通讯录.docx(11页珍藏版)》请在冰豆网上搜索。
中学java通讯录
[中学]java通讯录
java通讯录
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.sql.*;
classMyJFrameextendsJFrameimplementsActionListener
{
JLabellid=newJLabel("编号"),lname=newJLabel("姓名"),
lsex=newJLabel("性别"),lah=newJLabel("爱好"),lphone=newJLabel("联系电话"),
laddress=newJLabel("地址"),lbz=newJLabel("备注");
JLabelaa=newJLabel("编号
姓名");
JTextFieldtid=newJTextField(10),tname=new
JTextField(10),
tphone=newJTextField(15),
taddress=newJTextField(30);
JButtonjadd=newJButton("添加");
JButtonjupd=newJButton("修改");
JButtonjdel=newJButton("删除");
JButtonjcls=newJButton("清空");
Choicetsex,tah,tbz;JPanelp1,p2;Listls1;
JMenuBarmb;
MyJFrame(Stringtitle)
{
super(title);//设置窗体的标题;
mb=newJMenuBar();//框架f上添加菜单
JMenum1=newJMenu("查询");JMenum2=newJMenu("退出");
JMenuItemopen=newJMenuItem("查看全部");
JMenuItemclose=newJMenuItem("查询个人");
JMenuItemexit=newJMenuItem("退出");
m1.add(open);//将菜单项加入到菜单m1中
m1.add(close);
m2.add(exit);mb.add(m1);mb.add(m2);
setJMenuBar(mb);
tsex=newChoice();tah=newChoice();tbz=newChoice();
tsex.addItem("男");tsex.addItem("女");
tah.addItem("上网");tah.addItem("运动");tah.addItem("逛街");tah.addItem("游戏");
tbz.addItem("朋友");tbz.addItem("家人");tbz.addItem("同事");tbz.addItem("同学");
this.setSize(650,400);//可以根据实际界面的大小改变;
this.setLocation(400,280);//设置窗体在桌面大位置;
Containercon=getContentPane();//返回此窗体的contentPane对象。
con.setBackground(Color.lightGray);//根据实际需要,设置窗体的背景色;
con.setLayout(newGridLayout(1,2));
ls1=newList();//con.add(ls1);
p2=newJPanel();p2.setLayout(newGridLayout(0,1));
p2.add(ls1);p2.setBorder(BorderFactory.createTitledBorde
r("编号姓名性别电话地址爱好备注"));con.add(p2);
p1=newJPanel();p1.setLayout(newGridLayout(9,2));
p1.setBorder(BorderFactory.createTitledBorder("录入联系人信息"));
con.add(p1);
p1.add(lid);p1.add(tid);
p1.add(lname);p1.add(tname);
p1.add(lsex);p1.add(tsex);
p1.add(lah);p1.add(tah);
p1.add(lphone);p1.add(tphone);
p1.add(laddress);p1.add(taddress);
p1.add(lbz);p1.add(tbz);
p1.add(jadd);p1.add(jdel);
p1.add(jcls);
p1.add(jupd);
jadd.addActionListener(this);
jcls.addActionListener(this);
jdel.addActionListener(this);
jupd.addActionListener(this);
open.addActionListener(this);
close.addActionListener(this);
exit.addActionListener(this);
setDefaultCloseOperation(3);//设置窗体点击关闭按钮是要执行的操作;
setVisible(true);//设置窗体显示出来;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exceptione2)
{
}
}
publicvoidactionPerformed(ActionEvente)
{
Stringnumber=tid.getText();
Stringname=tname.getText();
Stringsex=tsex.getSelectedItem();
Stringbz=tbz.getSelectedItem();
Stringphone=tphone.getText();
Stringaddress=taddress.getText();
Stringah=tah.getSelectedItem();
try
{
Connection
con=DriverManager.getConnection("jdbc:
odbc:
student");
Statementsta=con.createStatement();
if(e.getSource()==jadd)//添加
{
Stringsql="insertintostudent(学号,姓名,性别,爱好,
联系电话,家庭地址,备注)values
('"+number+"','"+name+"','"+sex+"','"+ah+"','"+phone+"','"+
address+"','"+bz+"')";
if(number.length()==0||name.length()==0||ah.length()==0||bz.length()==0||sex.length()==0
||phone.length()==0||address.length()==0){
JOptionPane.showMessageDialog(null,"输入信息不能为空~");}
else
{
StringsqlString="selectcount(*)fromstudent
where学号="+number;
introwCount=0;
try{
ResultSetrs=sta.executeQuery(sqlString);
if(rs.next()){
System.out.println(rowCount=rs.getInt
(1));
}
if(rowCount>0){
JOptionPane.showMessageDialog(null,
"Sorry!
这个编号的学生已经存在,请检查是否输入有误~");
}
else{
sta.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"添加成功~");
}
}
catch(Exceptione3)
{System.out.print("helloworld");}
}
}
if(e.getActionCommand()=="查看全部")//查询全部
{
ls1.clear();
ResultSetrs;
Stringsql0String="selectcount(*)fromstudent";
intrc=0;
rs=sta.executeQuery(sql0String);
if(rs.next()){
System.out.println(rc=rs.getInt
(1));
}
if(rc==0){
JOptionPane.showMessageDialog(null,"对不起,通讯录此时无任何联系人~");
}
rs=sta.executeQuery("select*fromstudent");
while(rs.next()){
Stringstr;
str=rs.getString("学号")+""+rs.getString("
姓名")+""+rs.getString("性别")+""+rs.getString("联系电话")+""+rs.getString("家庭地址")+""+rs.getString("爱好")+""+rs.getString("备注");
ls1.add(str);
}
}
if(e.getActionCommand()=="查询个人")//查询个人
{
ls1.clear();
ResultSetrs;
Stringxh=JOptionPane.showInputDialog(this,"请输
入要查询的学号","查询",JOptionPane.OK_CANCEL_OPTION);
if(xh.length()==0){
JOptionPane.showMessageDialog(null,"请输入条
件~");}
try{
Stringsql0String="selectcount(*)from
studentwhere学号="+xh;
intrc=0;
rs=sta.executeQuery(sql0String);
if(rs.next()){
System.out.println(rc=rs.getInt
(1));
}
if(rc==0){
JOptionPane.showMessageDialog(null,"Sorry!
这个编号的人不存在~");
}
else{rs=sta.executeQuery("select*fromstudent
WHERE学号="+xh);
if(rs.next()){
Stringstr;
str="学号:
\n"+rs.getString("学号")+"
"+rs.getString("姓名")+""+rs.getString("性别")+"
"+rs.getString("联系电话")+""+rs.getString("家庭地址")+"
"+rs.getString("爱好")+""+rs.getString("备注");
ls1.add(str);
}
}
}
catch(Exceptione3)
{
}
}
if(e.getSource()==jupd)//修改
{
number=tid.getText();
ResultSetrs;
if(number.length()==0){
JOptionPane.showMessageDialog(null,"Sorry!
你没有输入学号~");
}
Stringsql0String="selectcount(*)fromstudent
where学号="+number;
intrc=0;
rs=sta.executeQuery(sql0String);
if(rs.next()){
System.out.println(rc=rs.getInt
(1));
}
if(rc==0){
JOptionPane.showMessageDialog(null,"Sorry!
这个编号的人不存在~");
}
StringsqlString="DELETEFROMstudentWHERE学号="+number;
sta.executeUpdate(sqlString);//执行语句,结果放在rs
Stringsql="insertintostudent(学号,姓名,性别,爱好,
联系电话,家庭地址,备注)values
('"+number+"','"+name+"','"+sex+"','"+ah+"','"+phone+"','"+
address+"','"+bz+"')";
sta.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"修改成功~");
}
if(e.getSource()==jcls)//清除
{
ls1.clear();
StringsqlString="DELETEFROMstudent";
try{
sta.executeUpdate(sqlString);
JOptionPane.showMessageDialog(null,"清除成功~");
}
catch(SQLExceptione1){
e1.printStackTrace();
}
}
if(e.getSource()==jdel){//删除
Stringxh=JOptionPane.showInputDialog(this,"请输入要修改的学号","查询",JOptionPane.OK_CANCEL_OPTION);
if(xh.length()==0){
JOptionPane.showMessageDialog(null,"请输入条件~");
}
try{
Stringsql0String="selectcount(*)from
studentwhere学号="+xh;
intrc=0;
ResultSetrs=
sta.executeQuery(sql0String);
if(rs.next()){
System.out.println(rc=rs.getInt
(1));
}
if(rc==0){
JOptionPane.showMessageDialog(null,
"Sorry!
这个编号的学生不存在,请检查是否输入有误~");
}
else{
sta.executeUpdate("DELETEFROMstudent
WHERE学号="+xh);
sta.close();
con.close();
JOptionPane.showMessageDialog(this,"删除成功","成功",JOptionPane.PLAIN_MESSAGE);
}
}catch(SQLExceptione2){
System.out.println(e2);
}
}
if(e.getActionCommand()=="退出")
System.exit(0);
}
catch(Exceptione1)
{
}
tid.setText("");
tname.setText("");
tphone.setText("");
taddress.setText("");
}
}
publicclassTest4
{
publicstaticvoidmain(Stringargs[])
{
newMyJFrame("非常非常非常简单的通讯录");//创建窗体对
象;
}
}