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 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 cancel=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.setLayout(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_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(null,
" 登 录 成 功 ",
" 提 示
",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;
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(new 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.close();
}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 New();
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 java.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;
QQ:
",Label.CENTER);
Frame f=new Frame("通讯录");
Label title=new Label("个人基本信息表",Label.CENTER);
Label qqLabel=new Label("
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 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 knowButton=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 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, 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 建立连接
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(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);
}
public static String checkString(){
if (male.getState()) {
result=male.getLabel();
}else {
result=female.getLabel();
}
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 actionPerformed(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();
gui();
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();
}
public void actionPerformed(ActionEvent e) {