JDBC连接数据库,完成注册和登录
2016-03-05 00:35
447 查看
第一:建立user表
第二:完成登录和注册界面
其中注册界面代码
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class ResigerUI extends JDialog implements ActionListener {
private UserService service = new UserServiceImpl();
private int width = 600;
private int height = 500;
private JLabel titlelb;
/** 用户名标签 */
private JLabel nameLb;
/** 密码标签 */
private JLabel passwordLb;
/** 性别标签 */
private JLabel sexLb;
/** 邮箱标签 */
private JLabel emailLb;
/** 联系电话标签 */
private JLabel phoneLb;
/** 用户名文本框 */
private JTextField nameTf;
/** 密码文本框 */
private JPasswordField passwordTf;
/** 女性单选按钮 */
private JRadioButton maleRbtn;
/** 男性单选按钮 */
private JRadioButton femaleRbtn;
/** 联系电话文本框 */
private JTextField phoneTf;
/** 邮箱文本框 */
private JTextField emailTf;
/**确定按钮*/
private JButton okBtn;
/**重置*/
private JButton resetBtn;
/** 构造方法 */
public ResigerUI() {
initComponents();
layoutComponents();
addListeners();
setSize(516, 483);// 设置大小
setModal(true);//设置为模态窗口
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);// 居屏幕中间显示
}
/**
* 初始化组件
*/
private void initComponents() {
titlelb= new JLabel("注 册");
nameLb = new JLabel("用户名");
nameLb.setFont(new Font("宋体", Font.PLAIN, 15));
passwordLb = new JLabel("\u5BC6 \u7801");
passwordLb.setFont(new Font("宋体", Font.PLAIN, 15));
sexLb = new JLabel("\u6027 \u522B");
sexLb.setFont(new Font("宋体", Font.PLAIN, 15));
phoneLb = new JLabel("联系电话");
phoneLb.setFont(new Font("宋体", Font.PLAIN, 15));
emailLb = new JLabel("\u90AE \u7BB1");
emailLb.setFont(new Font("宋体", Font.PLAIN, 15));
nameTf=new JTextField();
passwordTf=new JPasswordField();
maleRbtn=new JRadioButton("男");
maleRbtn.setFont(new Font("宋体", Font.PLAIN, 15));
femaleRbtn=new JRadioButton("女");
femaleRbtn.setFont(new Font("宋体", Font.PLAIN, 15));
phoneTf=new JTextField();
emailTf=new JTextField();
okBtn = new JButton("确定");
okBtn.setFont(new Font("宋体", Font.PLAIN, 15));
resetBtn = new JButton("重置");
resetBtn.setFont(new Font("宋体", Font.PLAIN, 15));
}
/**
* 布局组件
*/
private void layoutComponents() {
getContentPane().setLayout(null);// 去除默认的边界布局
titlelb.setBounds(182, 20, 80, 30);
titlelb.setFont(new Font("", Font.BOLD, 26));
nameLb.setBounds(100, 85, 80, 30);
nameTf.setBounds(200, 86, 250, 30);
passwordLb.setBounds(100, 143, 80, 30);
passwordTf.setBounds(200, 144, 250, 30);
sexLb.setBounds(100, 194, 80, 30);
maleRbtn.setBounds(200, 194, 50, 30);
femaleRbtn.setBounds(262, 194, 50, 30);
ButtonGroup btnGrounp = new ButtonGroup();
btnGrounp.add(maleRbtn);
btnGrounp.add(femaleRbtn);
maleRbtn.setSelected(true);
emailLb.setBounds(100,251,80,30);
emailTf.setBounds(200, 252, 250, 30);
phoneLb.setBounds(100,319,80,30);
phoneTf.setBounds(200, 320, 250, 30);
okBtn.setBounds(151, 386, 80, 30);
resetBtn.setBounds(289, 386, 80, 30);
getContentPane().add(titlelb);
getContentPane().add(nameLb);
getContentPane().add(nameTf);
getContentPane().add(passwordLb);
getContentPane().add(passwordTf);
getContentPane().add(sexLb);
getContentPane().add(maleRbtn);
getContentPane().add(femaleRbtn);
getContentPane().add(emailLb);
getContentPane().add(emailTf);
getContentPane().add(phoneLb);
getContentPane().add(phoneTf);
getContentPane().add(okBtn);
getContentPane().add(resetBtn);
}
/**
* 添加监听器
*/
private void addListeners() {
okBtn.addActionListener(this);
resetBtn.addActionListener(this);
}
/**
* 点击按钮触发的操作都写在这里
*/
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == okBtn){//点击确定按钮
resiger();//注册
}else if(e.getSource()==resetBtn){//点击重置按钮
reset();//重置
}
}
//注册
private void resiger() {
String name = nameTf.getText();
String password = new String(passwordTf.getPassword());
String sex = "男";
if(femaleRbtn.isSelected()){
sex = "女";
}
String phonestr = phoneTf.getText();
Integer phone = new Integer(phonestr);
String email = emailTf.getText();
User user = new User();
user.setName(name);
user.setPassword(password);
user.setSex(sex);
user.setPhone(phone);
user.setEmail(email);
try {
service.resiger(user);
JOptionPane.showMessageDialog(this, "注册成功");
dispose();
new LoginFrame().setVisible(true); //切换界面
} catch (ServiceException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
/**重置*/
private void reset
a728
() {
nameTf.setText("");
passwordTf.setText("");
maleRbtn.setSelected(true);
femaleRbtn.setSelected(false);
phoneTf.setText("");
emailTf.setText("");
nameTf.requestFocus();//获得焦点
}
}
登录代码略写,效果如下图
查看数据库,已成功导入数据
登录成功
关键代码(包括上一篇的JDBC工具类)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import day2.rowmapper.UserRowmapper;
import day2.util.JDBCUtils;
import day2.util.RowMapper;
import day3.dao.UserDao;
import day3.entity.User;
public class UserDaoImpl implements UserDao {
//根据用户名查询用户
public User findByName(String name) {
Connection conn = null;
List<User> list = new ArrayList<User>();
try {
conn = JDBCUtils.getConnection();
String sql = "select * from user where name= ? ";
Object[] params = {name };
RowMapper rm = new UserRowmapper();
list = JDBCUtils.executeQuery(conn, sql, params, rm);
if (null != list && list.size() > 0) {
return list.get(0);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn);
}
return null;
}
//用户注册
public User resiger(User user) {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
String sql = "insert into user (name,password,sex,phone,email) "
+ "values (?,?,?,?,?)";
Object[] params = { user.getName(),user.getPassword(),user.getSex(),user.getPhone(),user.getEmail() };
JDBCUtils.executeUpdate(conn, sql, params);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn);
}
return null;
}
//用户登录
public String login(String loginname, String loginpsd) {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
String sql="select * from user where name=? and password=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setObject(1, loginname);
pst.setObject(2, loginpsd);
ResultSet rs = pst.executeQuery();
if(rs.next()){
return null;
}
return "登录失败,用户名或密码错误";
}catch (SQLException e) {
return "登录异常";
} finally {
JDBCUtils.close(conn);
}
}
}
先简单到这,有不明白或者需要源码指导的初学的童鞋,可以私信我!!
第二:完成登录和注册界面
其中注册界面代码
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class ResigerUI extends JDialog implements ActionListener {
private UserService service = new UserServiceImpl();
private int width = 600;
private int height = 500;
private JLabel titlelb;
/** 用户名标签 */
private JLabel nameLb;
/** 密码标签 */
private JLabel passwordLb;
/** 性别标签 */
private JLabel sexLb;
/** 邮箱标签 */
private JLabel emailLb;
/** 联系电话标签 */
private JLabel phoneLb;
/** 用户名文本框 */
private JTextField nameTf;
/** 密码文本框 */
private JPasswordField passwordTf;
/** 女性单选按钮 */
private JRadioButton maleRbtn;
/** 男性单选按钮 */
private JRadioButton femaleRbtn;
/** 联系电话文本框 */
private JTextField phoneTf;
/** 邮箱文本框 */
private JTextField emailTf;
/**确定按钮*/
private JButton okBtn;
/**重置*/
private JButton resetBtn;
/** 构造方法 */
public ResigerUI() {
initComponents();
layoutComponents();
addListeners();
setSize(516, 483);// 设置大小
setModal(true);//设置为模态窗口
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);// 居屏幕中间显示
}
/**
* 初始化组件
*/
private void initComponents() {
titlelb= new JLabel("注 册");
nameLb = new JLabel("用户名");
nameLb.setFont(new Font("宋体", Font.PLAIN, 15));
passwordLb = new JLabel("\u5BC6 \u7801");
passwordLb.setFont(new Font("宋体", Font.PLAIN, 15));
sexLb = new JLabel("\u6027 \u522B");
sexLb.setFont(new Font("宋体", Font.PLAIN, 15));
phoneLb = new JLabel("联系电话");
phoneLb.setFont(new Font("宋体", Font.PLAIN, 15));
emailLb = new JLabel("\u90AE \u7BB1");
emailLb.setFont(new Font("宋体", Font.PLAIN, 15));
nameTf=new JTextField();
passwordTf=new JPasswordField();
maleRbtn=new JRadioButton("男");
maleRbtn.setFont(new Font("宋体", Font.PLAIN, 15));
femaleRbtn=new JRadioButton("女");
femaleRbtn.setFont(new Font("宋体", Font.PLAIN, 15));
phoneTf=new JTextField();
emailTf=new JTextField();
okBtn = new JButton("确定");
okBtn.setFont(new Font("宋体", Font.PLAIN, 15));
resetBtn = new JButton("重置");
resetBtn.setFont(new Font("宋体", Font.PLAIN, 15));
}
/**
* 布局组件
*/
private void layoutComponents() {
getContentPane().setLayout(null);// 去除默认的边界布局
titlelb.setBounds(182, 20, 80, 30);
titlelb.setFont(new Font("", Font.BOLD, 26));
nameLb.setBounds(100, 85, 80, 30);
nameTf.setBounds(200, 86, 250, 30);
passwordLb.setBounds(100, 143, 80, 30);
passwordTf.setBounds(200, 144, 250, 30);
sexLb.setBounds(100, 194, 80, 30);
maleRbtn.setBounds(200, 194, 50, 30);
femaleRbtn.setBounds(262, 194, 50, 30);
ButtonGroup btnGrounp = new ButtonGroup();
btnGrounp.add(maleRbtn);
btnGrounp.add(femaleRbtn);
maleRbtn.setSelected(true);
emailLb.setBounds(100,251,80,30);
emailTf.setBounds(200, 252, 250, 30);
phoneLb.setBounds(100,319,80,30);
phoneTf.setBounds(200, 320, 250, 30);
okBtn.setBounds(151, 386, 80, 30);
resetBtn.setBounds(289, 386, 80, 30);
getContentPane().add(titlelb);
getContentPane().add(nameLb);
getContentPane().add(nameTf);
getContentPane().add(passwordLb);
getContentPane().add(passwordTf);
getContentPane().add(sexLb);
getContentPane().add(maleRbtn);
getContentPane().add(femaleRbtn);
getContentPane().add(emailLb);
getContentPane().add(emailTf);
getContentPane().add(phoneLb);
getContentPane().add(phoneTf);
getContentPane().add(okBtn);
getContentPane().add(resetBtn);
}
/**
* 添加监听器
*/
private void addListeners() {
okBtn.addActionListener(this);
resetBtn.addActionListener(this);
}
/**
* 点击按钮触发的操作都写在这里
*/
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == okBtn){//点击确定按钮
resiger();//注册
}else if(e.getSource()==resetBtn){//点击重置按钮
reset();//重置
}
}
//注册
private void resiger() {
String name = nameTf.getText();
String password = new String(passwordTf.getPassword());
String sex = "男";
if(femaleRbtn.isSelected()){
sex = "女";
}
String phonestr = phoneTf.getText();
Integer phone = new Integer(phonestr);
String email = emailTf.getText();
User user = new User();
user.setName(name);
user.setPassword(password);
user.setSex(sex);
user.setPhone(phone);
user.setEmail(email);
try {
service.resiger(user);
JOptionPane.showMessageDialog(this, "注册成功");
dispose();
new LoginFrame().setVisible(true); //切换界面
} catch (ServiceException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
/**重置*/
private void reset
a728
() {
nameTf.setText("");
passwordTf.setText("");
maleRbtn.setSelected(true);
femaleRbtn.setSelected(false);
phoneTf.setText("");
emailTf.setText("");
nameTf.requestFocus();//获得焦点
}
}
登录代码略写,效果如下图
查看数据库,已成功导入数据
登录成功
关键代码(包括上一篇的JDBC工具类)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import day2.rowmapper.UserRowmapper;
import day2.util.JDBCUtils;
import day2.util.RowMapper;
import day3.dao.UserDao;
import day3.entity.User;
public class UserDaoImpl implements UserDao {
//根据用户名查询用户
public User findByName(String name) {
Connection conn = null;
List<User> list = new ArrayList<User>();
try {
conn = JDBCUtils.getConnection();
String sql = "select * from user where name= ? ";
Object[] params = {name };
RowMapper rm = new UserRowmapper();
list = JDBCUtils.executeQuery(conn, sql, params, rm);
if (null != list && list.size() > 0) {
return list.get(0);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn);
}
return null;
}
//用户注册
public User resiger(User user) {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
String sql = "insert into user (name,password,sex,phone,email) "
+ "values (?,?,?,?,?)";
Object[] params = { user.getName(),user.getPassword(),user.getSex(),user.getPhone(),user.getEmail() };
JDBCUtils.executeUpdate(conn, sql, params);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(conn);
}
return null;
}
//用户登录
public String login(String loginname, String loginpsd) {
Connection conn = null;
try {
conn = JDBCUtils.getConnection();
String sql="select * from user where name=? and password=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setObject(1, loginname);
pst.setObject(2, loginpsd);
ResultSet rs = pst.executeQuery();
if(rs.next()){
return null;
}
return "登录失败,用户名或密码错误";
}catch (SQLException e) {
return "登录异常";
} finally {
JDBCUtils.close(conn);
}
}
}
先简单到这,有不明白或者需要源码指导的初学的童鞋,可以私信我!!
相关文章推荐
- Oracle order by case when 多条件排序
- 在Windows7中搭建Mysql Cluster
- 将.sql文件中的数据库结构和数据存储入数据库中
- 修改mysql数据库表的engine类型
- day05 mysql基础
- Oracle命令(一):Oracle登录命令
- SQL Server中获取指定时间段内的所有月份
- mongoDB复制集(Replica Set)
- 分布式mongodb集群---sharding(分片)+复制集
- HTML5+Activex+Singr+ABP+MongoDB
- sql语句大全
- JDBC连接数据库(查询)的步骤
- Xampp mysql无法启动的解决方案(转)
- 2003 - Can't connect to MySQL server on 'x.x.x.x' (10038)
- memcached分布式集群算法(一致性哈希实现)
- oracle体系结构 非常详细
- 全国身份证身份数据库sql(2017最新)
- mysql命令
- mysql innodb_flush_log_at_trx_commit
- mysql undo redo log在事务中起的作用