1、java通讯录连接mysql数据库package jdbc;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.*;public class Denglu extends
2、WindowAdapter implements ActionListener JFrame f=new JFrame(登录); Container c = f.getContentPane(); JLabel I=new JLabel(账号:,JLabel.CENTER); JLabel P=new JLabel(密码:,JLabel.CENTER); JTextField tI=new JTextField(11); JPasswordField tP=new JPasswordField(11); JButton confirm=new JButton(确定); JButton canc
3、el=new JButton(取消); JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER,7,3); JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER,7,3); JPanel p3=new JPanel(new FlowLayout(FlowLayout.CENTER,7,0); String ID=; String PW=; Object option = new Object查询,新建; public Denglu() tP.setEchoChar(*); c.setLay
4、out(new GridLayout(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
5、_ON_CLOSE); public static void main(String args) new Denglu(); public void windowClosing(WindowEvent e) System.exit(0); public void actionPerformed(ActionEvent e) if (e.getSource()=confirm) if (ID.equals(tI.getText()&PW.equals(new String(tP.getPassword() f.dispose(); JOptionPane.showMessageDialog(nu
6、ll, 登录成功, 提示,JOptionPane.INFORMATION_MESSAGE); new ChioceDialog(); else JOptionPane.showMessageDialog(null, 用户名或密码错误,请重新登录, 提示,JOptionPane.ERROR_MESSAGE); else if (e.getSource()=cancel) System.exit(1); class ChioceDialog extends WindowAdapter implements ActionListener int newx = 0; New newone = null
7、; Reference reference =null; JDialog jDialog = new JDialog(); JButton referButton = new JButton(查询); JButton newButton = new JButton(新建); String name; public ChioceDialog() Container container = jDialog.getContentPane(); jDialog.setBounds(300,120,240,60); jDialog.setTitle(通讯录); container.setLayout(n
8、ew GridLayout(1,2,20,10); container.add(referButton); container.add(newButton); jDialog.setVisible(true); jDialog.validate(); newButton.addActionListener(this); referButton.addActionListener(this); jDialog.addWindowListener(this); public void windowClosing (WindowEvent e) if (newx = 1) reference.clo
9、se(); else if (newx = 2) newone.close(); System.exit(1); public void actionPerformed(ActionEvent d) if (d.getSource()=referButton) try reference = new Reference(); newx = 1; catch (Exception e) / TODO Auto-generated catch block e.printStackTrace(); else if(d.getSource()=newButton) try newone = new N
10、ew(); newx = 2; catch (Exception e) / TODO Auto-generated catch block e.printStackTrace(); package jdbc;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.sql.Connection;import jav
11、a.sql.DatabaseMetaData;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class AddressBook extends WindowAdapter implements ActionListener Statement st = null; Connection conn; ResultSet rsTables; ResultSet rs; Frame f=new Frame(通讯录); Label title=new Label(个人基本
12、信息表,Label.CENTER); Label qqLabel=new Label( QQ: ,Label.CENTER); Label nameLabel=new Label(姓名:,Label.CENTER); Label sexLabel=new Label(性别:,Label.CENTER); Label telLabel=new Label( Tel: ,Label.CENTER); Label emaiLabel=new Label(Email:,Label.CENTER); Label hobbyLabel=new Label(兴趣:,Label.CENTER); Label
13、addtext = new Label(备注); TextField nameTextField=new TextField(17); TextField qqTextField=new TextField(17); TextField telTextField=new TextField(17); TextField emailTextField=new TextField(17); TextArea text = new TextArea(4,3); Button confirm=new Button(确定); Button submit =new Button(重置); Button k
14、nowButton=new Button(确定); Panel p1 = new Panel(new FlowLayout(FlowLayout.CENTER,10,3); Panel p7 = new Panel(new FlowLayout(FlowLayout.CENTER,10,3); Panel p8 = new Panel(new GridLayout(2,1); Panel p3 = new Panel(new BorderLayout(3,3); Panel p2 = new Panel(new FlowLayout(FlowLayout.CENTER,66,1); Panel
15、 p4 = new Panel(new BorderLayout(); Panel p5 = new Panel(new FlowLayout(FlowLayout.LEFT,15,1); Panel p6 = new Panel(new FlowLayout(FlowLayout.RIGHT,10,1); static CheckboxGroup csex = new CheckboxGroup(); static Checkbox male =new Checkbox(男, true, csex); static Checkbox female =new Checkbox(女,false,
16、 csex); static Checkbox read=new Checkbox(阅读,false); static Checkbox basketball=new Checkbox(篮球); static Checkbox run=new Checkbox(跑步); static String result; static String result2; public void close() System.exit(0); public AddressBook() throws Exception Class.forName(com.mysql.jdbc.Driver); /2建立连接
17、String url = jdbc:mysql:/localhost:3306/test; String user = root; String password = 1993; conn = DriverManager.getConnection(url, user, password); DatabaseMetaData meta = conn.getMetaData(); rsTables = meta.getTables(null , null, AddressBook, null); st = conn.createStatement(); void gui() p1.add(nam
18、eLabel); 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(ho
19、bbyLabel); 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); public static String checkString() if (male.getState() result=male.getLabel(); else result=female.getLab
20、el(); return result; public static String checkString2() if (read.getState() result2=read.getLabel(); if (basketball.getState() result2+= +basketball.getLabel(); if (run.getState() result2+= +run.getLabel(); return result2; public void windowClosing(WindowEvent e) f.dispose(); Override public void a
21、ctionPerformed(ActionEvent arg0) / TODO Auto-generated method stub package jdbc;import java.awt.Color;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.sql.SQLException;import javax.swing.JOptionPane;public class New extends AddressBook public New() throws Exception super(); gu
22、i(); p2.add(confirm); p2.add(submit); confirm.setPreferredSize(new Dimension(70,25); submit.setPreferredSize(new Dimension(70,25); confirm.setBackground(Color.cyan); submit.setBackground(Color.cyan); confirm.addActionListener(this); submit.addActionListener(this); f.setVisible(true); f.pack(); publi
23、c void actionPerformed(ActionEvent e) if(e.getSource()=confirm) if (nameTextField.getText().equals() JOptionPane.showMessageDialog(null, 姓名不能为空); else String str = CREATE TABLE AddressBook(name VARCHAR(20),qq VARCHAR(17), + tel VARCHAR(20),email VARCHAR(30),sex CHAR(2),hobby VARCHAR(14),addtext TEXT
24、); String s1 = +nameTextField.getText()+,+qqTextField.getText()+; String s2=+telTextField.getText()+,+emailTextField.getText()+,+AddressBook.checkString()+; String s3=+AddressBook.checkString2()+,+text.getText()+; String tem = insert into addressbook values(+s1+,+s2+,+s3+); try st.executeUpdate(str)
25、; st.executeUpdate(tem); st.close(); conn.close(); f.dispose(); JOptionPane.showMessageDialog(null, 添加成功!); catch (SQLException e1) e1.printStackTrace(); if(e.getSource() = submit) nameTextField.setText(); qqTextField.setText(); telTextField.setText(); emailTextField.setText(); male.setState(true);
26、read.setState(false); basketball.setState(false); run.setState(false); text.setText(); package jdbc;import java.awt.Color;import java.awt.Dimension;import java.awt.event.ActionEvent;import javax.swing.JOptionPane;import java.sql.ResultSet;import java.sql.SQLException;public class Reference extends A
27、ddressBook String name; int num=0; public Reference() throws Exception 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)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1