数据库的建立
首先,建立一个数据库,存储注册成功的账户信息。
其SQL的DDL语句如下:
CREATE TABLE `jdbctest` ( `id` int(10) NOT NULL auto_increment, `username` varchar(20) default NULL, `password` varchar(20) default NULL, `realName` varchar(10) default NULL, `sex` char(1) default NULL, `phone` varchar(20) default NULL, `email` varchar(30) default NULL, `vocation` varchar(10) default NULL, `city` varchar(10) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
示意图
处理SQL代码的方法
以下方法存储在BaseDAO.java文件中。
变量声明
Connection connection; Statement statement; PreparedStatement pStatement; ResultSet rSet; |
处理增删改的SQL语句
/** PreparedStatement 预编译之查询拓展版 */ public ArrayList<HashMap<Object, Object>> Query(String sql,Object[] s) { ArrayList<HashMap<Object, Object>> list = null; getConnection(); try { pStatement = connection.prepareStatement(sql); for (int i = 0; i < s.length; i++) { pStatement.setObject(i+1, s[i]); } rSet = pStatement.executeQuery(); list = ResultSetToList(rSet); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } |
处理查询类的SQL语句
/** 查询并返回记录集 */ public ResultSet getResultSet(String sql, Object[] objArr){ getConnection(); try { pStatement = connection.prepareStatement(sql, ResultSet.
TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); if(objArr!=null && objArr.length>0) { for (int i = 0; i < objArr.length; i++) { pStatement.setObject(i+1, objArr[i]); } } rSet = pStatement.executeQuery(); //list = resultSetToList(rs); } catch (SQLException e) { e.printStackTrace(); } finally { //close(); } return rSet; } |
关闭并释放资源
/** 关闭并释放资源 */ public void close(){ try { if(statement!=null){ statement.close(); statement = null; } if(pStatement!=null){ pStatement.close(); pStatement = null; } if(connection!=null){ connection.close(); connection = null; } } catch (Exception e) { System.out.println("数据库close异常"); } } |
处理用户信息的方法
以下方法存储在UserDAO.java中,类UserDAO继承自类BaseDAO。
注册
public boolean Reg(String[] arr){ BaseDAO bDao = new BaseDAO(); String sql ="INSERT INTO jdbctest(username,password,
realName,sex,phone,email,vocation,city) values(?,?,?,?,?,?,?,?);" ; bDao.Update(sql, arr); return true; } |
验证用户名唯一性
public boolean Unique(String nameString) { BaseDAO bDao = new BaseDAO(); String[] s = {nameString}; ArrayList<HashMap<Object, Object>> list = bDao.Query
("SELECT * FROM jdbctest WHERE username=?;",s); if (list.isEmpty()) { return true; } return false; } |
判断是否成功登录
public boolean login(String username, String password){ BaseDAO bDao = new BaseDAO(); String[] s = {username ,password}; ArrayList<HashMap<Object, Object>> list = bDao.Query
("SELECT * FROM jdbctest WHERE username=? and password=?;",s); if (list.isEmpty()) { return false; } return true; } |
注册窗口
Java代码
package com.ui;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import com.dao.UserDAO;
/**
*
* @author __USER__
*/
public class LoginFrame extends javax.swing.JFrame {
/** Creates new form Test4QQ */
public LoginFrame() {
initComponents();
setResizable(false);
setBounds(450,230,380,310);
}
public LoginFrame(String string) {
super(string);
initComponents();
setResizable(false);
setBounds(450,230,380,310);
}
//GEN-BEGIN:initComponents
//
private void initComponents() {
jColorChooser1 = new javax.swing.JColorChooser();
jColorChooser2 = new javax.swing.JColorChooser();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jCheckBox1 = new javax.swing.JCheckBox();
jCheckBox2 = new javax.swing.JCheckBox();
jButton3 = new javax.swing.JButton();
jPasswordField1 = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setIcon(new javax.swing.ImageIcon(
"qq1.png")); // NOI18N
jLabel2.setIcon(new javax.swing.ImageIcon(
"qq.png")); // NOI18N
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jButton1.setText("\u6ce8\u518c\u8d26\u53f7");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("\u627e\u56de\u5bc6\u7801");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jCheckBox1.setText("\u8bb0\u4f4f\u5bc6\u7801");
jCheckBox2.setText("\u81ea\u52a8\u767b\u5f55");
jButton3.setFont(new java.awt.Font("锟斤拷锟斤拷", 0, 18));
jButton3.setText("\u767b\u5f55");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(18, 18,18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING,false)
.addComponent(jPasswordField1)
.addComponent(jTextField1,javax.swing.GroupLayout.DEFAULT_SIZE,130,Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,25,Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(26, 26,26))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jCheckBox1)
.addGap(44, 44,44)
.addComponent(jCheckBox2))))
.addGroup(layout.createSequentialGroup()
.addGap(113, 113, 113)
.addComponent(jButton3,javax.swing.GroupLayout.PREFERRED_SIZE,138,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(123, Short.MAX_VALUE)));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(10, 10, 10)
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jPasswordField1,javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jCheckBox1)
.addComponent(jCheckBox2))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addComponent(jButton3)
.addContainerGap()));
pack();
}//
//GEN-END:initComponents
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
RegisteFrame rFrame = new RegisteFrame();
rFrame.setVisible(true);
this.setVisible(false);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// MemberFrame mFrame = new MemberFrame();
// mFrame.setVisible(true);
// this.setVisible(false);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
UserDAO uDao = new UserDAO();
if (uDao.login(jTextField1.getText(), jPasswordField1.getText())) {
// JOptionPane.showMessageDialog(null, "您登录成功啦!", "登录成功",
// JOptionPane.INFORMATION_MESSAGE);
String string = "欢迎用户: "+jTextField1.getText()+" 登录!";
MemberFrame mFrame = new MemberFrame(string);
mFrame.setVisible(true);
this.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入!", "登录失败",
JOptionPane.ERROR_MESSAGE);
jPasswordField1.setText(null);
}
}
private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
LoginFrame lFrame = new LoginFrame();
lFrame.setVisible(true);
lFrame.setTitle("用户登录");
}
});
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JColorChooser jColorChooser1;
private javax.swing.JColorChooser jColorChooser2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
} |
效果图
底部的三个按钮分别用于 进入登录界面、注册、清除注册信息。
登录界面
Java代码
package com.ui; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import javax.swing.JFrame; import javax.swing.JOptionPane; import com.dao.UserDAO; /** * * @author __USER__ */ public class LoginFrame extends javax.swing.JFrame { /** Creates new form Test4QQ */ public LoginFrame() { initComponents(); setResizable(false); setBounds(450,230,380,310); } public LoginFrame(String string) { super(string); initComponents(); setResizable(false); setBounds(450,230,380,310); } //GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jColorChooser1 = new javax.swing.JColorChooser(); jColorChooser2 = new javax.swing.JColorChooser(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jCheckBox1 = new javax.swing.JCheckBox(); jCheckBox2 = new javax.swing.JCheckBox(); jButton3 = new javax.swing.JButton(); jPasswordField1 = new javax.swing.JPasswordField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setIcon(new javax.swing.ImageIcon( "qq1.png")); // NOI18N jLabel2.setIcon(new javax.swing.ImageIcon( "qq.png")); // NOI18N jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jButton1.setText("\u6ce8\u518c\u8d26\u53f7"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("\u627e\u56de\u5bc6\u7801"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jCheckBox1.setText("\u8bb0\u4f4f\u5bc6\u7801"); jCheckBox2.setText("\u81ea\u52a8\u767b\u5f55"); jButton3.setFont(new java.awt.Font("锟斤拷锟斤拷", 0, 18)); jButton3.setText("\u767b\u5f55"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jPasswordField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jPasswordField1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout( getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup( layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel2) .addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(18, 18,18) addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING,false) .addComponent(jPasswordField1) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE,130,Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,25, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addComponent(jButton2)) .addGap(26, 26,26)) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jCheckBox1) .addGap(44, 44,44) addComponent(jCheckBox2)))) .addGroup(layout.createSequentialGroup() .addGap(113, 113, 113) .addComponent(jButton3,javax.swing.GroupLayout.PREFERRED_SIZE,138, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(123, Short.MAX_VALUE))); layout.setVerticalGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout.createSequentialGroup() .addComponent(jLabel1) .addGap(10, 10, 10) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel2)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) addComponent(jButton1) .addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton2) .addComponent(jPasswordField1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jCheckBox1) .addComponent(jCheckBox2)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
.addComponent(jButton3) .addContainerGap())); pack(); }// </editor-fold> //GEN-END:initComponents private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: RegisteFrame rFrame = new RegisteFrame(); rFrame.setVisible(true); this.setVisible(false); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // MemberFrame mFrame = new MemberFrame(); // mFrame.setVisible(true); // this.setVisible(false); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: UserDAO uDao = new UserDAO(); if (uDao.login(jTextField1.getText(), jPasswordField1.getText())) { // JOptionPane.showMessageDialog(null, "您登录成功啦!", "登录成功", // JOptionPane.INFORMATION_MESSAGE); String string = "欢迎用户: "+jTextField1.getText()+" 登录!"; MemberFrame mFrame = new MemberFrame(string); mFrame.setVisible(true); this.setVisible(false); } else { JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入!", "登录失败", JOptionPane.ERROR_MESSAGE); jPasswordField1.setText(null); } } private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { LoginFrame lFrame = new LoginFrame(); lFrame.setVisible(true); lFrame.setTitle("用户登录"); } }); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JCheckBox jCheckBox2; private javax.swing.JColorChooser jColorChooser1; private javax.swing.JColorChooser jColorChooser2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPasswordField jPasswordField1; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables } |
效果图
|