Java运用JDBC实现一个注册、登录系统的编写

数据库的建立

首先,建立一个数据库,存储注册成功的账户信息。

其SQL的DDL语句如下:

[sql][/sql] view plaincopy

  1. CREATE TABLE `jdbctest` (
  2.   `id` int(10) NOT NULL auto_increment,
  3.   `username` varchar(20) default NULL,
  4.   `password` varchar(20) default NULL,
  5.   `realName` varchar(10) default NULL,
  6.   `sex` char(1) default NULL,
  7.   `phone` varchar(20) default NULL,
  8.   `email` varchar(30) default NULL,
  9.   `vocation` varchar(10) default NULL,
  10.   `city` varchar(10) default NULL,
  11.   PRIMARY KEY  (`id`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

示意图

处理SQL代码的方法

以下方法存储在BaseDAO.java文件中。

变量声明

[java][/java] view plaincopy

  1. Connection connection;
  2. Statement statement;
  3. PreparedStatement pStatement;
  4. ResultSet rSet;

处理增删改的SQL语句

[java][/java] view plaincopy

  1. /** PreparedStatement 预编译之查询拓展版 */
  2. public ArrayList<HashMap<Object, Object>> Query(String sql,Object[] s) {
  3.     ArrayList<HashMap<Object, Object>> list = null;
  4.     getConnection();
  5.     try {
  6.         pStatement = connection.prepareStatement(sql);
  7.         for (int i = 0; i < s.length; i++) {
  8.             pStatement.setObject(i+1, s[i]);
  9.         }
  10.         rSet = pStatement.executeQuery();
  11.         list = ResultSetToList(rSet);
  12.     } catch (SQLException e) {
  13.         // TODO Auto-generated catch block
  14.         e.printStackTrace();
  15.     }
  16.     return list;
  17. }

处理查询类的SQL语句

[java][/java] view plaincopy

  1. /** 查询并返回记录集 */
  2. public ResultSet getResultSet(String sql, Object[] objArr){
  3.     getConnection();
  4.     try {
  5.         pStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  6.         if(objArr!=null && objArr.length>0) {
  7.             for (int i = 0; i < objArr.length; i++) {
  8.                 pStatement.setObject(i+1, objArr[i]);
  9.             }
  10.         }
  11.         rSet = pStatement.executeQuery();
  12.         //list = resultSetToList(rs);
  13.     } catch (SQLException e) {
  14.         e.printStackTrace();
  15.     } finally {
  16.         //close();
  17.     }
  18.     return rSet;
  19. }

关闭并释放资源

[java][/java] view plaincopy

  1. /** 关闭并释放资源 */
  2. public void close(){
  3.     try {
  4.         if(statement!=null){
  5.             statement.close();
  6.             statement = null;
  7.         }
  8.         if(pStatement!=null){
  9.             pStatement.close();
  10.             pStatement = null;
  11.         }
  12.         if(connection!=null){
  13.             connection.close();
  14.             connection = null;
  15.         }
  16.     } catch (Exception e) {
  17.         System.out.println(“数据库close异常”);
  18.     }
  19. }

处理用户信息的方法

以下方法存储在UserDAO.java中,类UserDAO继承自类BaseDAO。

注册

[java][/java] view plaincopy

  1. public boolean Reg(String[] arr){
  2.     BaseDAO bDao = new BaseDAO();
  3.     String sql =”INSERT INTO jdbctest(username,password,realName,sex,phone,email,vocation,city) values(?,?,?,?,?,?,?,?);” ;
  4.     bDao.Update(sql, arr);
  5.     return true;
  6. }

验证用户名唯一性

[java][/java] view plaincopy

  1. public boolean Unique(String nameString) {
  2.     BaseDAO bDao = new BaseDAO();
  3.     String[] s = {nameString};
  4.     ArrayList<HashMap<Object, Object>> list = bDao.Query(“SELECT * FROM jdbctest WHERE username=?;”,s);
  5.     if (list.isEmpty()) {
  6.         return true;
  7.     }
  8.     return false;
  9. }

判断是否成功登录

[java][/java] view plaincopy

  1. public boolean login(String username, String password){
  2.     BaseDAO bDao = new BaseDAO();
  3.     String[] s = {username ,password};
  4.     ArrayList<HashMap<Object, Object>> list = bDao.Query(“SELECT * FROM jdbctest WHERE username=? and password=?;”,s);
  5.     if (list.isEmpty()) {
  6.         return false;
  7.     }
  8.     return true;
  9. }

注册窗口

Java代码

[java][/java] view plaincopy

  1. /*
  2.  * RegisteFrame.java
  3.  *
  4.  * Created on __DATE__, __TIME__
  5.  */
  6. package com.ui;
  7. import javax.swing.JOptionPane;
  8. import com.dao.UserDAO;
  9. /**
  10.  *
  11.  * @author  __USER__
  12.  */
  13. public class RegisteFrame extends javax.swing.JFrame {
  14.     /** Creates new form RegisteFrame */
  15.     public RegisteFrame() {
  16.         initComponents();
  17.         setResizable(false);
  18.         setLocation(400, 160);
  19.     }
  20.     public RegisteFrame(String s) {
  21.         super(s);
  22.         initComponents();
  23.         setResizable(false);
  24.         setLocation(400, 150);
  25.     }
  26.     /** This method is called from within the constructor to
  27.      * initialize the form.
  28.      * WARNING: Do NOT modify this code. The content of this method is
  29.      * always regenerated by the Form Editor.
  30.      */
  31.     //GEN-BEGIN:initComponents
  32.     // <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
  33.     private void initComponents() {
  34.         buttonGroup1 = new javax.swing.ButtonGroup();
  35.         buttonGroup2 = new javax.swing.ButtonGroup();
  36.         jLabel1 = new javax.swing.JLabel();
  37.         jLabel2 = new javax.swing.JLabel();
  38.         jTextField1 = new javax.swing.JTextField();
  39.         jLabel3 = new javax.swing.JLabel();
  40.         jLabel4 = new javax.swing.JLabel();
  41.         jPasswordField1 = new javax.swing.JPasswordField();
  42.         jPasswordField2 = new javax.swing.JPasswordField();
  43.         jLabel5 = new javax.swing.JLabel();
  44.         jTextField2 = new javax.swing.JTextField();
  45.         jLabel6 = new javax.swing.JLabel();
  46.         jTextField3 = new javax.swing.JTextField();
  47.         jLabel7 = new javax.swing.JLabel();
  48.         jRadioButton1 = new javax.swing.JRadioButton();
  49.         jRadioButton2 = new javax.swing.JRadioButton();
  50.         jLabel8 = new javax.swing.JLabel();
  51.         jTextField4 = new javax.swing.JTextField();
  52.         jLabel9 = new javax.swing.JLabel();
  53.         jTextField5 = new javax.swing.JTextField();
  54.         jLabel10 = new javax.swing.JLabel();
  55.         jTextField6 = new javax.swing.JTextField();
  56.         jToggleButton1 = new javax.swing.JToggleButton();
  57.         jToggleButton2 = new javax.swing.JToggleButton();
  58.         jButton1 = new javax.swing.JButton();
  59.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  60.         jLabel1.setFont(new java.awt.Font(“Segoe UI”, 0, 24));
  61.         jLabel1.setText(“Membership Registration”);
  62.         jLabel2.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  63.         jLabel2.setText(“UserName (*)”);
  64.         jTextField1.addActionListener(new java.awt.event.ActionListener() {
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  66.                 jTextField1ActionPerformed(evt);
  67.             }
  68.         });
  69.         jLabel3.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  70.         jLabel3.setText(“PassWord (*)”);
  71.         jLabel4.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  72.         jLabel4.setText(“PassWord Verify (*)”);
  73.         jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
  74.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  75.                 jPasswordField1ActionPerformed(evt);
  76.             }
  77.         });
  78.         jPasswordField2.addActionListener(new java.awt.event.ActionListener() {
  79.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  80.                 jPasswordField2ActionPerformed(evt);
  81.             }
  82.         });
  83.         jLabel5.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  84.         jLabel5.setText(“Your RealName”);
  85.         jTextField2.addActionListener(new java.awt.event.ActionListener() {
  86.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  87.                 jTextField2ActionPerformed(evt);
  88.             }
  89.         });
  90.         jLabel6.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  91.         jLabel6.setText(“Your E-mail”);
  92.         jTextField3.addActionListener(new java.awt.event.ActionListener() {
  93.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  94.                 jTextField3ActionPerformed(evt);
  95.             }
  96.         });
  97.         jLabel7.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  98.         jLabel7.setText(“Your Sex”);
  99.         jRadioButton1.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  100.         jRadioButton1.setText(“Man”);
  101.         jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
  102.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  103.                 jRadioButton1ActionPerformed(evt);
  104.             }
  105.         });
  106.         jRadioButton2.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  107.         jRadioButton2.setText(“WoMan”);
  108.         jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
  109.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  110.                 jRadioButton2ActionPerformed(evt);
  111.             }
  112.         });
  113.         jLabel8.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  114.         jLabel8.setText(“Your Vocation”);
  115.         jTextField4.addActionListener(new java.awt.event.ActionListener() {
  116.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  117.                 jTextField4ActionPerformed(evt);
  118.             }
  119.         });
  120.         jLabel9.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  121.         jLabel9.setText(“Your City”);
  122.         jTextField5.addActionListener(new java.awt.event.ActionListener() {
  123.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  124.                 jTextField5ActionPerformed(evt);
  125.             }
  126.         });
  127.         jLabel10.setFont(new java.awt.Font(“Segoe UI”, 0, 14));
  128.         jLabel10.setText(“Your Phone”);
  129.         jTextField6.addActionListener(new java.awt.event.ActionListener() {
  130.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  131.                 jTextField6ActionPerformed(evt);
  132.             }
  133.         });
  134.         jToggleButton1.setFont(new java.awt.Font(“Segoe UI”, 0, 18));
  135.         jToggleButton1.setText(“registe”);
  136.         jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
  137.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  138.                 jToggleButton1ActionPerformed(evt);
  139.             }
  140.         });
  141.         jToggleButton2.setFont(new java.awt.Font(“Segoe UI”, 0, 18));
  142.         jToggleButton2.setText(“clean”);
  143.         jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
  144.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  145.                 jToggleButton2ActionPerformed(evt);
  146.             }
  147.         });
  148.         jButton1.setFont(new java.awt.Font(“Segoe UI”, 0, 18));
  149.         jButton1.setText(“login”);
  150.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  151.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  152.                 jButton1ActionPerformed(evt);
  153.             }
  154.         });
  155.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
  156.                 getContentPane());
  157.         getContentPane().setLayout(layout);
  158.         layout.setHorizontalGroup(layout
  159.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  160.                 .addGroup(
  161.                         layout.createSequentialGroup()
  162.                                 .addGroup(
  163.                                         layout.createParallelGroup(
  164.                                                 javax.swing.GroupLayout.Alignment.TRAILING)
  165.                                                 .addGroup(
  166.                                                         javax.swing.GroupLayout.Alignment.LEADING,
  167.                                                         layout.createSequentialGroup()
  168.                                                                 .addContainerGap(
  169.                                                                         59,
  170.                                                                         Short.MAX_VALUE)
  171.                                                                 .addGroup(
  172.                                                                         layout.createParallelGroup(
  173.                                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
  174.                                                                                 .addComponent(
  175.                                                                                         jLabel1)
  176.                                                                                 .addGroup(
  177.                                                                                         layout.createSequentialGroup()
  178.                                                                                                 .addGroup(
  179.                                                                                                         layout.createParallelGroup(
  180.                                                                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
  181.                                                                                                                 .addComponent(
  182.                                                                                                                         jLabel6)
  183.                                                                                                                 .addComponent(
  184.                                                                                                                         jLabel7)
  185.                                                                                                                 .addComponent(
  186.                                                                                                                         jLabel4)
  187.                                                                                                                 .addComponent(
  188.                                                                                                                         jLabel9)
  189.                                                                                                                 .addComponent(
  190.                                                                                                                         jLabel10)
  191.                                                                                                                 .addComponent(
  192.                                                                                                                         jLabel8)
  193.                                                                                                                 .addComponent(
  194.                                                                                                                         jLabel5)
  195.                                                                                                                 .addComponent(
  196.                                                                                                                         jLabel3)
  197.                                                                                                                 .addComponent(
  198.                                                                                                                         jLabel2))
  199.                                                                                                 .addGap(43,
  200.                                                                                                         43,
  201.                                                                                                         43)
  202.                                                                                                 .addGroup(
  203.                                                                                                         layout.createParallelGroup(
  204.                                                                                                                 javax.swing.GroupLayout.Alignment.LEADING,
  205.                                                                                                                 false)
  206.                                                                                                                 .addComponent(
  207.                                                                                                                         jTextField6)
  208.                                                                                                                 .addComponent(
  209.                                                                                                                         jTextField5)
  210.                                                                                                                 .addComponent(
  211.                                                                                                                         jTextField4)
  212.                                                                                                                 .addComponent(
  213.                                                                                                                         jTextField3)
  214.                                                                                                                 .addComponent(
  215.                                                                                                                         jTextField2)
  216.                                                                                                                 .addComponent(
  217.                                                                                                                         jPasswordField2,
  218.                                                                                                                         javax.swing.GroupLayout.Alignment.TRAILING)
  219.                                                                                                                 .addComponent(
  220.                                                                                                                         jPasswordField1)
  221.                                                                                                                 .addComponent(
  222.                                                                                                                         jTextField1,
  223.                                                                                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  224.                                                                                                                         138,
  225.                                                                                                                         Short.MAX_VALUE)
  226.                                                                                                                 .addGroup(
  227.                                                                                                                         layout.createSequentialGroup()
  228.                                                                                                                                 .addComponent(
  229.                                                                                                                                         jRadioButton1)
  230.                                                                                                                                 .addPreferredGap(
  231.                                                                                                                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED,
  232.                                                                                                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  233.                                                                                                                                         Short.MAX_VALUE)
  234.                                                                                                                                 .addComponent(
  235.                                                                                                                                         jRadioButton2))))))
  236.                                                 .addGroup(
  237.                                                         javax.swing.GroupLayout.Alignment.LEADING,
  238.                                                         layout.createSequentialGroup()
  239.                                                                 .addGap(73, 73,
  240.                                                                         73)
  241.                                                                 .addComponent(
  242.                                                                         jButton1)
  243.                                                                 .addGap(18, 18,
  244.                                                                         18)
  245.                                                                 .addComponent(
  246.                                                                         jToggleButton1)
  247.                                                                 .addGap(18, 18,
  248.                                                                         18)
  249.                                                                 .addComponent(
  250.                                                                         jToggleButton2)))
  251.                                 .addGap(87, 87, 87)));
  252.         layout.setVerticalGroup(layout
  253.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  254.                 .addGroup(
  255.                         layout.createSequentialGroup()
  256.                                 .addContainerGap()
  257.                                 .addComponent(jLabel1)
  258.                                 .addGap(13, 13, 13)
  259.                                 .addGroup(
  260.                                         layout.createParallelGroup(
  261.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  262.                                                 .addComponent(
  263.                                                         jTextField1,
  264.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  265.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  266.                                                         javax.swing.GroupLayout.PREFERRED_SIZE)
  267.                                                 .addComponent(jLabel2))
  268.                                 .addPreferredGap(
  269.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  270.                                 .addGroup(
  271.                                         layout.createParallelGroup(
  272.                                                 javax.swing.GroupLayout.Alignment.LEADING)
  273.                                                 .addComponent(
  274.                                                         jPasswordField1,
  275.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  276.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  277.                                                         javax.swing.GroupLayout.PREFERRED_SIZE)
  278.                                                 .addComponent(jLabel3))
  279.                                 .addGap(11, 11, 11)
  280.                                 .addGroup(
  281.                                         layout.createParallelGroup(
  282.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  283.                                                 .addComponent(
  284.                                                         jPasswordField2,
  285.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  286.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  287.                                                         javax.swing.GroupLayout.PREFERRED_SIZE)
  288.                                                 .addComponent(jLabel4))
  289.                                 .addPreferredGap(
  290.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  291.                                 .addGroup(
  292.                                         layout.createParallelGroup(
  293.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  294.                                                 .addComponent(jLabel5)
  295.                                                 .addComponent(
  296.                                                         jTextField2,
  297.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  298.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  299.                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  300.                                 .addPreferredGap(
  301.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  302.                                 .addGroup(
  303.                                         layout.createParallelGroup(
  304.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  305.                                                 .addComponent(jLabel6)
  306.                                                 .addComponent(
  307.                                                         jTextField3,
  308.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  309.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  310.                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  311.                                 .addPreferredGap(
  312.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  313.                                 .addGroup(
  314.                                         layout.createParallelGroup(
  315.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  316.                                                 .addComponent(jLabel7)
  317.                                                 .addComponent(jRadioButton1)
  318.                                                 .addComponent(jRadioButton2))
  319.                                 .addPreferredGap(
  320.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  321.                                 .addGroup(
  322.                                         layout.createParallelGroup(
  323.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  324.                                                 .addComponent(jLabel8)
  325.                                                 .addComponent(
  326.                                                         jTextField4,
  327.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  328.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  329.                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  330.                                 .addPreferredGap(
  331.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  332.                                 .addGroup(
  333.                                         layout.createParallelGroup(
  334.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  335.                                                 .addComponent(jLabel9)
  336.                                                 .addComponent(
  337.                                                         jTextField5,
  338.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  339.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  340.                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  341.                                 .addPreferredGap(
  342.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  343.                                 .addGroup(
  344.                                         layout.createParallelGroup(
  345.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  346.                                                 .addComponent(jLabel10)
  347.                                                 .addComponent(
  348.                                                         jTextField6,
  349.                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  350.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  351.                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  352.                                 .addGap(18, 18, 18)
  353.                                 .addGroup(
  354.                                         layout.createParallelGroup(
  355.                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  356.                                                 .addComponent(jButton1)
  357.                                                 .addComponent(jToggleButton1)
  358.                                                 .addComponent(jToggleButton2))
  359.                                 .addContainerGap(26, Short.MAX_VALUE)));
  360.         pack();
  361.     }// </editor-fold>
  362.     //GEN-END:initComponents
  363.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  364.         // TODO add your handling code here:登录
  365.         LoginFrame lFrame = new LoginFrame(“用户登录”);
  366.         lFrame.setVisible(true);
  367.         this.setVisible(false);
  368.     }
  369.     private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  370.         // TODO add your handling code here: 清空
  371.         jTextField1.setText(null);
  372.         jTextField2.setText(null);
  373.         jTextField3.setText(null);
  374.         jTextField4.setText(null);
  375.         jTextField5.setText(null);
  376.         jTextField6.setText(null);
  377.         jPasswordField1.setText(null);
  378.         jPasswordField2.setText(null);
  379.         jRadioButton1.setSelected(false);
  380.         jRadioButton2.setSelected(false);
  381.     }
  382.     boolean registe = false;
  383.     public boolean Regist() {
  384.         return registe;
  385.     }
  386.     private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  387.         // TODO add your handling code here: 登录
  388.         UserDAO uDao = new UserDAO();
  389.         if (jTextField1.getText().isEmpty()) {
  390.             JOptionPane.showMessageDialog(null, “用户名不能为空!”, “注册失败”,
  391.                     JOptionPane.ERROR_MESSAGE);
  392.         } else if (jPasswordField1.getText().isEmpty()
  393.                 || jPasswordField1.getText().isEmpty()) {
  394.             JOptionPane.showMessageDialog(null, “密码栏不能为空!”, “注册失败”,
  395.                     JOptionPane.ERROR_MESSAGE);
  396.         } else if (!jPasswordField1.getText().equals(jPasswordField2.getText())) {
  397.             JOptionPane.showMessageDialog(null, “两次输入的密码不一致!”, “注册失败”,
  398.                     JOptionPane.ERROR_MESSAGE);
  399.             jPasswordField1.setText(null);
  400.             jPasswordField2.setText(null);
  401.         } else if (! uDao.Unique(jTextField1.getText())) {
  402.             JOptionPane.showMessageDialog(null, “该用户名已被注册,请重新输入!”, “注册失败”,
  403.                     JOptionPane.ERROR_MESSAGE);
  404.             jTextField1.setText(null);
  405.             jPasswordField1.setText(null);
  406.             jPasswordField2.setText(null);
  407.         }else {
  408.             JOptionPane.showMessageDialog(null, “账号已注册成功!”, “注册成功”,
  409.                     JOptionPane.INFORMATION_MESSAGE);
  410.             uDao.Reg(UserInformation());
  411.             registe = true;
  412.         }
  413.     }
  414.     public String[] UserInformation() {
  415.         String[] infor = { null, null, null, null, null, null, null, null };
  416.         infor[0] = jTextField1.getText();
  417.         infor[1] = jPasswordField1.getText();
  418.         infor[2] = jTextField2.getText(); //真实姓名
  419.         infor[3] = (!jRadioButton1.isSelected() && !jRadioButton2.isSelected()) ? “”
  420.                 : (jRadioButton1.isSelected()) ? “男” : “女”; //性别
  421.         infor[4] = jTextField6.getText(); //电话
  422.         infor[5] = jTextField3.getText(); //邮箱
  423.         infor[6] = jTextField4.getText(); //职业
  424.         infor[7] = jTextField5.getText(); //城市
  425.         return infor;
  426.     }
  427.     private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {
  428.         // TODO add your handling code here: 电话
  429.     }
  430.     private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
  431.         // TODO add your handling code here: 城市
  432.     }
  433.     private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
  434.         // TODO add your handling code here:  职业
  435.     }
  436.     private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  437.         // TODO add your handling code here: 性别 女
  438.         if (jRadioButton1.isSelected()) {
  439.             jRadioButton1.setSelected(false);
  440.         }
  441.     }
  442.     private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  443.         // TODO add your handling code here: 性别 男
  444.         if (jRadioButton2.isSelected()) {
  445.             jRadioButton2.setSelected(false);
  446.         }
  447.     }
  448.     private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
  449.         // TODO add your handling code here:  邮箱
  450.     }
  451.     private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
  452.         // TODO add your handling code here: 真实姓名
  453.     }
  454.     private void jPasswordField2ActionPerformed(java.awt.event.ActionEvent evt) {
  455.         // TODO add your handling code here: 确认密码
  456.     }
  457.     private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
  458.         // TODO add your handling code here: 密码
  459.     }
  460.     private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
  461.         // TODO add your handling code here:  UserName
  462.     }
  463.     /**
  464.      * @param args the command line arguments
  465.      */
  466.     public static void main(String args[]) {
  467.         java.awt.EventQueue.invokeLater(new Runnable() {
  468.             public void run() {
  469.                 RegisteFrame rFrame = new RegisteFrame(“用户注册”);
  470.                 rFrame.setVisible(true);
  471.             }
  472.         });
  473.     }
  474.     //GEN-BEGIN:variables
  475.     // Variables declaration – do not modify
  476.     private javax.swing.ButtonGroup buttonGroup1;
  477.     private javax.swing.ButtonGroup buttonGroup2;
  478.     private javax.swing.JButton jButton1;
  479.     private javax.swing.JLabel jLabel1;
  480.     private javax.swing.JLabel jLabel10;
  481.     private javax.swing.JLabel jLabel2;
  482.     private javax.swing.JLabel jLabel3;
  483.     private javax.swing.JLabel jLabel4;
  484.     private javax.swing.JLabel jLabel5;
  485.     private javax.swing.JLabel jLabel6;
  486.     private javax.swing.JLabel jLabel7;
  487.     private javax.swing.JLabel jLabel8;
  488.     private javax.swing.JLabel jLabel9;
  489.     private javax.swing.JPasswordField jPasswordField1;
  490.     private javax.swing.JPasswordField jPasswordField2;
  491.     private javax.swing.JRadioButton jRadioButton1;
  492.     private javax.swing.JRadioButton jRadioButton2;
  493.     private javax.swing.JTextField jTextField1;
  494.     private javax.swing.JTextField jTextField2;
  495.     private javax.swing.JTextField jTextField3;
  496.     private javax.swing.JTextField jTextField4;
  497.     private javax.swing.JTextField jTextField5;
  498.     private javax.swing.JTextField jTextField6;
  499.     private javax.swing.JToggleButton jToggleButton1;
  500.     private javax.swing.JToggleButton jToggleButton2;
  501.     // End of variables declaration//GEN-END:variables
  502. }

效果图

底部的三个按钮分别用于 进入登录界面、注册、清除注册信息。

登录界面

Java代码

[java][/java] view plaincopy

  1. package com.ui;
  2. import java.io.IOException;
  3. import java.net.URL;
  4. import java.net.URLConnection;
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7. import com.dao.UserDAO;
  8. /**
  9.  *
  10.  * @author  __USER__
  11.  */
  12. public class LoginFrame extends javax.swing.JFrame {
  13.     /** Creates new form Test4QQ */
  14.     public LoginFrame() {
  15.         initComponents();
  16.         setResizable(false);
  17.         setBounds(450,230,380,310);
  18.     }
  19.     public LoginFrame(String string) {
  20.         super(string);
  21.         initComponents();
  22.         setResizable(false);
  23.         setBounds(450,230,380,310);
  24.     }
  25.     //GEN-BEGIN:initComponents
  26.     // <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
  27.     private void initComponents() {
  28.         jColorChooser1 = new javax.swing.JColorChooser();
  29.         jColorChooser2 = new javax.swing.JColorChooser();
  30.         jLabel1 = new javax.swing.JLabel();
  31.         jLabel2 = new javax.swing.JLabel();
  32.         jTextField1 = new javax.swing.JTextField();
  33.         jButton1 = new javax.swing.JButton();
  34.         jButton2 = new javax.swing.JButton();
  35.         jCheckBox1 = new javax.swing.JCheckBox();
  36.         jCheckBox2 = new javax.swing.JCheckBox();
  37.         jButton3 = new javax.swing.JButton();
  38.         jPasswordField1 = new javax.swing.JPasswordField();
  39.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  40.         jLabel1.setIcon(new javax.swing.ImageIcon(
  41.                 “qq1.png”)); // NOI18N
  42.         jLabel2.setIcon(new javax.swing.ImageIcon(
  43.                 “qq.png”)); // NOI18N
  44.         jTextField1.addActionListener(new java.awt.event.ActionListener() {
  45.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  46.                 jTextField1ActionPerformed(evt);
  47.             }
  48.         });
  49.         jButton1.setText(“\u6ce8\u518c\u8d26\u53f7”);
  50.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  51.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  52.                 jButton1ActionPerformed(evt);
  53.             }
  54.         });
  55.         jButton2.setText(“\u627e\u56de\u5bc6\u7801”);
  56.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  57.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  58.                 jButton2ActionPerformed(evt);
  59.             }
  60.         });
  61.         jCheckBox1.setText(“\u8bb0\u4f4f\u5bc6\u7801”);
  62.         jCheckBox2.setText(“\u81ea\u52a8\u767b\u5f55”);
  63.         jButton3.setFont(new java.awt.Font(“锟斤拷锟斤拷”, 0, 18));
  64.         jButton3.setText(“\u767b\u5f55”);
  65.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  66.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  67.                 jButton3ActionPerformed(evt);
  68.             }
  69.         });
  70.         jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
  71.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  72.                 jPasswordField1ActionPerformed(evt);
  73.             }
  74.         });
  75.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
  76.                 getContentPane());
  77.         getContentPane().setLayout(layout);
  78.         layout.setHorizontalGroup(layout
  79.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  80.                 .addComponent(jLabel1)
  81.                 .addGroup(
  82.                         layout.createSequentialGroup()
  83.                                 .addContainerGap()
  84.                                 .addComponent(jLabel2)
  85.                                 .addGroup(
  86.                                         layout.createParallelGroup(
  87.                                                 javax.swing.GroupLayout.Alignment.LEADING)
  88.                                                 .addGroup(
  89.                                                         layout.createSequentialGroup()
  90.                                                                 .addGap(18, 18,
  91.                                                                         18)
  92.                                                                 .addGroup(
  93.                                                                         layout.createParallelGroup(
  94.                                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
  95.                                                                                 false)
  96.                                                                                 .addComponent(
  97.                                                                                         jPasswordField1)
  98.                                                                                 .addComponent(
  99.                                                                                         jTextField1,
  100.                                                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  101.                                                                                         130,
  102.                                                                                         Short.MAX_VALUE))
  103.                                                                 .addPreferredGap(
  104.                                                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED,
  105.                                                                         25,
  106.                                                                         Short.MAX_VALUE)
  107.                                                                 .addGroup(
  108.                                                                         layout.createParallelGroup(
  109.                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
  110.                                                                                 .addComponent(
  111.                                                                                         jButton1)
  112.                                                                                 .addComponent(
  113.                                                                                         jButton2))
  114.                                                                 .addGap(26, 26,
  115.                                                                         26))
  116.                                                 .addGroup(
  117.                                                         layout.createSequentialGroup()
  118.                                                                 .addPreferredGap(
  119.                                                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  120.                                                                 .addComponent(
  121.                                                                         jCheckBox1)
  122.                                                                 .addGap(44, 44,
  123.                                                                         44)
  124.                                                                 .addComponent(
  125.                                                                         jCheckBox2))))
  126.                 .addGroup(
  127.                         layout.createSequentialGroup()
  128.                                 .addGap(113, 113, 113)
  129.                                 .addComponent(jButton3,
  130.                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  131.                                         138,
  132.                                         javax.swing.GroupLayout.PREFERRED_SIZE)
  133.                                 .addContainerGap(123, Short.MAX_VALUE)));
  134.         layout.setVerticalGroup(layout
  135.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136.                 .addGroup(
  137.                         layout.createSequentialGroup()
  138.                                 .addComponent(jLabel1)
  139.                                 .addGap(10, 10, 10)
  140.                                 .addGroup(
  141.                                         layout.createParallelGroup(
  142.                                                 javax.swing.GroupLayout.Alignment.LEADING)
  143.                                                 .addGroup(
  144.                                                         layout.createSequentialGroup()
  145.                                                                 .addPreferredGap(
  146.                                                                         javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  147.                                                                 .addComponent(
  148.                                                                         jLabel2))
  149.                                                 .addGroup(
  150.                                                         layout.createSequentialGroup()
  151.                                                                 .addGroup(
  152.                                                                         layout.createParallelGroup(
  153.                                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  154.                                                                                 .addComponent(
  155.                                                                                         jButton1)
  156.                                                                                 .addComponent(
  157.                                                                                         jTextField1,
  158.                                                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  159.                                                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  160.                                                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  161.                                                                 .addPreferredGap(
  162.                                                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  163.                                                                 .addGroup(
  164.                                                                         layout.createParallelGroup(
  165.                                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  166.                                                                                 .addComponent(
  167.                                                                                         jButton2)
  168.                                                                                 .addComponent(
  169.                                                                                         jPasswordField1,
  170.                                                                                         javax.swing.GroupLayout.PREFERRED_SIZE,
  171.                                                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  172.                                                                                         javax.swing.GroupLayout.PREFERRED_SIZE))
  173.                                                                 .addPreferredGap(
  174.                                                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  175.                                                                 .addGroup(
  176.                                                                         layout.createParallelGroup(
  177.                                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
  178.                                                                                 .addComponent(
  179.                                                                                         jCheckBox1)
  180.                                                                                 .addComponent(
  181.                                                                                         jCheckBox2))))
  182.                                 .addPreferredGap(
  183.                                         javax.swing.LayoutStyle.ComponentPlacement.RELATED,
  184.                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  185.                                         Short.MAX_VALUE).addComponent(jButton3)
  186.                                 .addContainerGap()));
  187.         pack();
  188.     }// </editor-fold>
  189.     //GEN-END:initComponents
  190.     private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
  191.         // TODO add your handling code here:
  192.     }
  193.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  194.         // TODO add your handling code here:
  195.         RegisteFrame rFrame = new RegisteFrame();
  196.         rFrame.setVisible(true);
  197.         this.setVisible(false);
  198.     }
  199.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  200. //      MemberFrame mFrame = new MemberFrame();
  201. //      mFrame.setVisible(true);
  202. //      this.setVisible(false);
  203.     }
  204.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  205.         // TODO add your handling code here:
  206.         UserDAO uDao = new UserDAO();
  207.         if (uDao.login(jTextField1.getText(), jPasswordField1.getText())) {
  208. //          JOptionPane.showMessageDialog(null, “您登录成功啦!”, “登录成功”,
  209. //                  JOptionPane.INFORMATION_MESSAGE);
  210.             String string = “欢迎用户: “+jTextField1.getText()+” 登录!”;
  211.             MemberFrame mFrame = new MemberFrame(string);
  212.             mFrame.setVisible(true);
  213.             this.setVisible(false);
  214.         } else  {
  215.             JOptionPane.showMessageDialog(null, “用户名或密码错误,请重新输入!”, “登录失败”,
  216.                     JOptionPane.ERROR_MESSAGE);
  217.             jPasswordField1.setText(null);
  218.         }
  219.     }
  220.     private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
  221.         // TODO add your handling code here:
  222.     }
  223.     /**
  224.      * @param args the command line arguments
  225.      */
  226.     public static void main(String args[]) {
  227.         java.awt.EventQueue.invokeLater(new Runnable() {
  228.             public void run() {
  229.                 LoginFrame lFrame = new LoginFrame();
  230.                 lFrame.setVisible(true);
  231.                 lFrame.setTitle(“用户登录”);
  232.             }
  233.         });
  234.     }
  235.     //GEN-BEGIN:variables
  236.     // Variables declaration – do not modify
  237.     private javax.swing.JButton jButton1;
  238.     private javax.swing.JButton jButton2;
  239.     private javax.swing.JButton jButton3;
  240.     private javax.swing.JCheckBox jCheckBox1;
  241.     private javax.swing.JCheckBox jCheckBox2;
  242.     private javax.swing.JColorChooser jColorChooser1;
  243.     private javax.swing.JColorChooser jColorChooser2;
  244.     private javax.swing.JLabel jLabel1;
  245.     private javax.swing.JLabel jLabel2;
  246.     private javax.swing.JPasswordField jPasswordField1;
  247.     private javax.swing.JTextField jTextField1;
  248.     // End of variables declaration//GEN-END:variables
  249. }

效果图

源码下载

http://download.csdn.net/detail/oyuntaolianwu/5882099

标签