java通讯录连接mysql数据库.docx
《java通讯录连接mysql数据库.docx》由会员分享,可在线阅读,更多相关《java通讯录连接mysql数据库.docx(21页珍藏版)》请在冰豆网上搜索。
![java通讯录连接mysql数据库.docx](https://file1.bdocx.com/fileroot1/2023-2/4/faffe593-216d-40f1-a85a-2257504542c3/faffe593-216d-40f1-a85a-2257504542c31.gif)
java通讯录连接mysql数据库
packagejdbc;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.*;
publicclassDengluextendsWindowAdapterimplementsActionListener{
JFramef=newJFrame("登录");
Containerc=f.getContentPane();
JLabelI=newJLabel("账号:
",JLabel.CENTER);
JLabelP=newJLabel("密码:
",JLabel.CENTER);
JTextFieldtI=newJTextField(11);
JPasswordFieldtP=newJPasswordField(11);
JButtonconfirm=newJButton("确定");
JButtoncancel=newJButton("取消");
JPanelp1=newJPanel(newFlowLayout(FlowLayout.CENTER,7,3));
JPanelp2=newJPanel(newFlowLayout(FlowLayout.CENTER,7,3));
JPanelp3=newJPanel(newFlowLayout(FlowLayout.CENTER,7,0));
StringID="";
StringPW="";
Object[]option=newObject[]{"查询","新建"};
publicDenglu(){
tP.setEchoChar('*');
c.setLayout(newGridLayout(3,1));
p1.add(I);
p1.add(tI);
p2.add(P);
p2.add(tP);
p3.add(confirm);
p3.add(cancel);
f.add(p1);
f.add(p2);
f.add(p3);
f.setBounds(300,200,70,50);
f.setVisible(true);
f.pack();
confirm.addActionListener(this);
cancel.addActionListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
newDenglu();
}
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==confirm){
if(ID.equals(tI.getText())&&PW.equals(newString(tP.getPassword()))){
f.dispose();
JOptionPane.showMessageDialog(null,"登录成功","提示",JOptionPane.INFORMATION_MESSAGE);
newChioceDialog();
}else{
JOptionPane.showMessageDialog(null,"用户名或密码错误,请重新登录","提示",JOptionPane.ERROR_MESSAGE);
}
}elseif(e.getSource()==cancel){
System.exit
(1);
}
}
}
classChioceDialogextendsWindowAdapterimplementsActionListener{
intnewx=0;
Newnewone=null;
Referencereference=null;
JDialogjDialog=newJDialog();
JButtonreferButton=newJButton("查询");
JButtonnewButton=newJButton("新建");
Stringname;
publicChioceDialog(){
Containercontainer=jDialog.getContentPane();
jDialog.setBounds(300,120,240,60);
jDialog.setTitle("通讯录");
container.setLayout(newGridLayout(1,2,20,10));
container.add(referButton);
container.add(newButton);
jDialog.setVisible(true);
jDialog.validate();
newButton.addActionListener(this);
referButton.addActionListener(this);
jDialog.addWindowListener(this);
}
publicvoidwindowClosing(WindowEvente){
if(newx==1){
reference.close();
}elseif(newx==2){
newone.close();
}
System.exit
(1);
}
publicvoidactionPerformed(ActionEventd){
if(d.getSource()==referButton){
try{
reference=newReference();
newx=1;
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}elseif(d.getSource()==newButton){
try{
newone=newNew();
newx=2;
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}
packagejdbc;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.sql.Connection;
importjava.sql.DatabaseMetaData;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.Statement;
publicclassAddressBookextendsWindowAdapterimplementsActionListener{
Statementst=null;
Connectionconn;
ResultSetrsTables;
ResultSetrs;
Framef=newFrame("通讯录");
Labeltitle=newLabel("个人基本信息表",Label.CENTER);
LabelqqLabel=newLabel("QQ:
",Label.CENTER);
LabelnameLabel=newLabel("姓名:
",Label.CENTER);
LabelsexLabel=newLabel("性别:
",Label.CENTER);
LabeltelLabel=newLabel("Tel:
",Label.CENTER);
LabelemaiLabel=newLabel("Email:
",Label.CENTER);
LabelhobbyLabel=newLabel("兴趣:
",Label.CENTER);
Labeladdtext=newLabel("备注");
TextFieldnameTextField=newTextField(17);
TextFieldqqTextField=newTextField(17);
TextFieldtelTextField=newTextField(17);
TextFieldemailTextField=newTextField(17);
TextAreatext=newTextArea(4,3);
Buttonconfirm=newButton("确定");
Buttonsubmit=newButton("重置");
ButtonknowButton=newButton("确定");
Panelp1=newPanel(newFlowLayout(FlowLayout.CENTER,10,3));
Panelp7=newPanel(newFlowLayout(FlowLayout.CENTER,10,3));
Panelp8=newPanel(newGridLayout(2,1));
Panelp3=newPanel(newBorderLayout(3,3));
Panelp2=newPanel(newFlowLayout(FlowLayout.CENTER,66,1));
Panelp4=newPanel(newBorderLayout());
Panelp5=newPanel(newFlowLayout(FlowLayout.LEFT,15,1));
Panelp6=newPanel(newFlowLayout(FlowLayout.RIGHT,10,1));
staticCheckboxGroupcsex=newCheckboxGroup();
staticCheckboxmale=newCheckbox("男",true,csex);
staticCheckboxfemale=newCheckbox("女",false,csex);
staticCheckboxread=newCheckbox("阅读",false);
staticCheckboxbasketball=newCheckbox("篮球");
staticCheckboxrun=newCheckbox("跑步");
staticStringresult;
staticStringresult2;
publicvoidclose(){
System.exit(0);
}
publicAddressBook()throwsException{
Class.forName("com.mysql.jdbc.Driver");
//2建立连接
Stringurl="jdbc:
mysql:
//localhost:
3306/test";
Stringuser="root";
Stringpassword="1993";
conn=DriverManager.getConnection(url,user,password);
DatabaseMetaDatameta=conn.getMetaData();
rsTables=meta.getTables(null,null,"AddressBook",null);
st=conn.createStatement();
}
voidgui(){
p1.add(nameLabel);
p1.add(nameTextField);
p1.add(qqLabel);
p1.add(qqTextField);
p7.add(telLabel);
p7.add(telTextField);
p7.add(emaiLabel);
p7.add(emailTextField);
p8.add(p1);
p8.add(p7);
f.setBounds(300,200,0,0);
f.add(title,"North");
p4.add(p8,"North");
p5.add(sexLabel);
p5.add(male);
p5.add(female);
p6.add(hobbyLabel);
p6.add(read);
p6.add(basketball);
p6.add(run);
p4.add(p5);
p4.add(p6,"East");
p4.add(p3,"South");
f.add(p4);
p3.add(addtext,"West");
p3.add(text);
f.add(p2,"South");
f.addWindowListener(this);
}
publicstaticStringcheckString(){
if(male.getState()){
result=male.getLabel();
}else{
result=female.getLabel();
}
returnresult;
}
publicstaticStringcheckString2(){
if(read.getState()){
result2=read.getLabel();
}
if(basketball.getState()){
result2+=""+basketball.getLabel();
}
if(run.getState()){
result2+=""+run.getLabel();
}
returnresult2;
}
publicvoidwindowClosing(WindowEvente){
f.dispose();
}
@Override
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
}
}
packagejdbc;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.event.ActionEvent;
importjava.sql.SQLException;
importjavax.swing.JOptionPane;
publicclassNewextendsAddressBook{
publicNew()throwsException{
super();
gui();
p2.add(confirm);
p2.add(submit);
confirm.setPreferredSize(newDimension(70,25));
submit.setPreferredSize(newDimension(70,25));
confirm.setBackground(Color.cyan);
submit.setBackground(Color.cyan);
confirm.addActionListener(this);
submit.addActionListener(this);
f.setVisible(true);
f.pack();
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==confirm){
if(nameTextField.getText().equals("")){
JOptionPane.showMessageDialog(null,"姓名不能为空");
}else{
Stringstr="CREATETABLEAddressBook(nameVARCHAR(20),qqVARCHAR(17),"+
"telVARCHAR(20),emailVARCHAR(30),sexCHAR
(2),hobbyVARCHAR(14),addtextTEXT)";
Strings1="'"+nameTextField.getText()+"','"+qqTextField.getText()+"'";
Strings2="'"+telTextField.getText()+"','"+emailTextField.getText()+"','"+AddressBook.checkString()+"'";
Strings3="'"+AddressBook.checkString2()+"','"+text.getText()+"'";
Stringtem="insertintoaddressbookvalues("+s1+","+s2+","+s3+")";
try{
st.executeUpdate(str);
st.executeUpdate(tem);
st.close();
conn.close();
f.dispose();
JOptionPane.showMessageDialog(null,"添加成功!
");
}catch(SQLExceptione1){
e1.printStackTrace();
}
}
}
if(e.getSource()==submit){
nameTextField.setText("");
qqTextField.setText("");
telTextField.setText("");
emailTextField.setText("");
male.setState(true);
read.setState(false);
basketball.setState(false);
run.setState(false);
text.setText("");
}
}
}
packagejdbc;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.event.ActionEvent;
importjavax.swing.JOptionPane;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassReferenceextendsAddressBook{
Stringname;
intnum=0;
publicReference()throwsException{
super();
if(rsTables.next()==false){
JOptionPane.showMessageDialog(null,"无数据,请添加");
}else{
do{
name=JOptionPane.showInputDialog(null,"请输入你要查询的姓名:
","查询",JOptionPane.INFORMATION_MESSAGE);
if(name.equals("")){
JOptionPane.showMessageDialog(null,"请输入姓名!
",null,JOptionPane.WARNING_MESSAGE);
}
}while(name.equals(""));
if(name!
=null){