package edu.cqie.sofe.jiaju.week9.demo;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.*;
import java.util.Random;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class RegisterUI implements ActionListener
{
Random rand=new Random();
JFrame j;
int QQ;
JLabel jLabel1;
JTextField jTextField1;
JLabel jLabel2;
JTextField jTextField2;
JLabel jLabel3;
JPasswordField jPasswordField1;
JLabel jLabel4;
JTextField jTextField3;
JLabel jLabel5;
JTextField jTextField4;
JLabel jLabel6;
JComboBox jComboBox1;
JLabel jLabel7;
JComboBox jComboBox2;
JLabel jLabel8;
JComboBox jComboBox3;
JLabel jLabel9;
JComboBox jComboBox4;
JLabel jLabel11;
JTextArea jTextArea1;
JButton jButton1;
JButton jButton2;
Icon i;
public RegisterUI()
{
j=new JFrame("用户注册");
jButton2 = new JButton();
j.setLayout(null);
jLabel1=new JLabel();
jLabel1.setText("昵称:");
jLabel1.setBounds(36, 39, 91, 29);
jLabel1.setForeground(Color.GREEN);
jTextField2 =new JTextField();
jTextField2.setBounds(115, 127, 153, 27);
jLabel4=new JLabel();
jLabel4.setText("姓名:");
jLabel4.setBounds(36, 163, 53, 22);
jLabel4.setForeground(Color.GREEN);
jLabel3=new JLabel();
jLabel3.setText("住址:");
jLabel3.setBounds(36, 122, 62, 32);
jLabel3.setForeground(Color.GREEN);
jLabel2=new JLabel();
jPasswordField1=new JPasswordField();
jPasswordField1.setBounds(115, 86, 153, 25);
jLabel2.setText("密码:");
jLabel2.setBounds(36, 85, 91, 27);
jLabel2.setForeground(Color.GREEN);
String[] str={"男","女"};
jComboBox1 = new JComboBox(str);
String[] str1={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
jComboBox2 = new JComboBox(str1);
String[] str2={"不详","A","B","AB","O"};
jComboBox3 = new JComboBox(str2);
String[] str3={"不详","白羊","金牛","狮子","天秤","天蝎","水瓶","处女"};
jComboBox4 = new JComboBox(str3);
jButton1=new JButton();
jButton1.setBounds(69, 397, 60, 21);
jButton1.setText("注册");
jButton1.addActionListener(this);
jButton1.setActionCommand(("注册"));
jButton2.setBounds(252, 397, 65, 20);
jButton2.setText("取消");
jButton2.addActionListener(this);
jButton2.setActionCommand(("取消"));
jTextArea1 =new JTextArea();
jTextArea1.setBounds(115, 283, 208, 95);
j.add(jLabel1);
jLabel9=new JLabel();
jLabel9.setText("星座:");
jLabel9.setBounds(220, 239, 45, 20);
jLabel9.setForeground(Color.GREEN);
jComboBox3.setBounds(115, 239, 53, 19);
jLabel8=new JLabel();
jLabel8.setToolTipText("");
jLabel8.setText("血型:");
jLabel8.setBounds(36, 237, 53, 22);
jComboBox2.setBounds(276, 202, 54, 17);
jLabel8.setForeground(Color.GREEN);
jLabel7=new JLabel();
jLabel7.setText("生肖:");
jLabel7.setBounds(219, 204, 41, 20);
jLabel7.setForeground(Color.GREEN);
jComboBox1.setBounds(115, 202, 53, 17);
jLabel6=new JLabel();
jLabel6.setText("性别:");
jLabel6.setBounds(36, 201, 52, 21);
jLabel6.setForeground(Color.GREEN);
jTextField4 =new JTextField();
jTextField4.setBounds(276, 166, 79, 23);
jLabel5=new JLabel();
jLabel5.setText("年龄:");
jLabel5.setBounds(219, 166, 53, 22);
jLabel5.setForeground(Color.GREEN);
jTextField3=new JTextField();
jTextField3.setBounds(115, 166, 80, 20);
jLabel11=new JLabel();
jLabel11.setBounds(0, 0, 400, 434);
i=new ImageIcon("");
jLabel11.setIcon(i);
//jTextField1.setEditable(false);//设为不可写
jTextField1=new JTextField();
j.add(jTextField1);
j.add(jLabel4);
j.add(jLabel3);
j.add(jLabel2);
j.add(jPasswordField1);
j.add(jTextField2);
j.add(jTextField3);
j.add(jLabel6);
j.add(jLabel5);
j.add(jTextField4);
j.add(jLabel7);
j.add(jLabel8);
j.add(jComboBox3);
j.add(jLabel9);
j.add(jTextArea1);
j.add(jButton1);
j.add(jButton2);
j.add(jComboBox2);
j.add(jComboBox4);
j.add(jComboBox1);
jTextField1.setBounds(117, 40, 153, 26);
j.add(jLabel11);
j.setResizable(false);
j.setSize(400,465);
j.setLocation(600,100);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//public void itemStateChanged(ItemEvent e)
// {
// if(e.getStateChange()==ItemEvent.SELECTED)
// System.out.println(((JComboBox)e.getSource()).getSelectedItem().toString());
// }
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("取消"))
{
j.dispose();
LoginUI lo=new LoginUI();
}
else if(e.getActionCommand().equals("注册"))
{
if(jTextField1.getText().equals("")
|| jPasswordField1.getText().equals("")|| jTextField2.getText().equals("") ||
jTextField3.getText().equals("")||jTextField4.getText().equals("")||jTextArea1.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "不能为空", "错误",JOptionPane.ERROR_MESSAGE);
}
else
{
QQnumber();
search();
insert();
}
}
}
//随机数产生器
public int QQnumber()
{
int num=rand.nextInt()/1000000+1000000;
return num;
}
//查询数据库方法
private void search()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection co=DriverManager.getConnection("jdbc:odbc:lj","sa", "");
String str="select * from zhuce";
Statement sta=co.createStatement();
ResultSet re=sta.executeQuery(str);
if(re.next())
{
QQ=QQnumber();
JOptionPane.showMessageDialog(null,"记住新产生的QQ号:"+QQ,"成功获取",JOptionPane.INFORMATION_MESSAGE);///需改动
insert();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
//插入方法
public void insert()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection co = DriverManager.getConnection("jdbc:odbc:lj","sa", "");
String str="insert into zhuce(QQ,nicheng,mima,zhuzhi,xingming,nianling,xingbie,shengxiao,xuexing,xingzuo,shuoming) values(?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps=co.prepareStatement(str);
ps.setInt(1,QQ);
ps.setString(2,jTextField1.getText().trim());
ps.setString(3,jPasswordField1.getText().trim());
ps.setString(4,jTextField2.getText().trim());
ps.setString(5,jTextField3.getText().trim());
ps.setString(6,jTextField4.getText().trim());
ps.setString(7,jComboBox1.getSelectedItem().toString().trim());
ps.setString(8,jComboBox2.getSelectedItem().toString().trim());
ps.setString(9,jComboBox3.getSelectedItem().toString().trim());
ps.setString(10,jComboBox4.getSelectedItem().toString().trim());
ps.setString(11,jTextArea1.getText().trim());
int ret=ps.executeUpdate();
//判断执行结果
if(ret>0)
{
JOptionPane.showMessageDialog(null, "注册成功", "消息",JOptionPane.INFORMATION_MESSAGE);
j.dispose();
LoginUI lo=new LoginUI();
}
else
{
JOptionPane.showMessageDialog(null, "注册失败", "警告",JOptionPane.ERROR_MESSAGE);
}
ps.close();
co.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
public static void main(String a[])
{
RegisterUI r=new RegisterUI();
}
}
原文:http://www.cnblogs.com/chenyi1997/p/6755571.html