求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   Code  
会员   
 
  
 
 
     
   
分享到
运用JDBC实现一个注册、登录系统的编写
 
火龙果软件    发布于 2013-9-18
 

数据库的建立

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

其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
}

效果图

相关文章

Java微服务新生代之Nacos
深入理解Java中的容器
Java容器详解
Java代码质量检查工具及使用案例
相关文档

Java性能优化
Spring框架
SSM框架简单简绍
从零开始学java编程经典
相关课程

高性能Java编程与系统性能优化
JavaEE架构、 设计模式及性能调优
Java编程基础到应用开发
JAVA虚拟机原理剖析
 
分享到
 
 


Java 中的中文编码问题
Java基础知识的三十个经典问答
玩转 Java Web 应用开发
使用Spring更好地处理Struts
用Eclipse开发iPhone Web应用
插件系统框架分析
更多...   


Struts+Spring+Hibernate
基于J2EE的Web 2.0应用开发
J2EE设计模式和性能调优
Java EE 5企业级架构设计
Java单元测试方法与技术
Java编程方法与技术


Struts+Spring+Hibernate/EJB+性能优化
华夏基金 ActiveMQ 原理与管理
某民航公司 Java基础编程到应用开发
某风电公司 Java 应用开发平台与迁移
日照港 J2EE应用开发技术框架与实践
某跨国公司 工作流管理JBPM
东方航空公司 高级J2EE及其前沿技术
更多...