数据库连接与用户登录
来源:互联网 发布:淘宝如何避免盗图投诉 编辑:程序博客网 时间:2024/06/06 19:14
1.数据库连接
利用myEclipse与mysql进行连接
从官网上下载jar包,导入到myEclipse中,接下来书写相关的连接代码:
代码如下:
package com.iotek.dao.impl;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.spi.DirStateFactory.Result;
public class BassDao {
//private static final String DRIVER="com.mysql.jdbc";
public Connection getConn(){
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/data","root","123456");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 释放相应的资源
* @param rs
* @param pstmt
* @param conn
*/
public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){
try{
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
/**
* 此方法可以完成增删改所有操作
* @param sql
* @param params
* @return true 或 false
*/
public boolean operUpdate(String sql,List<Object>params){
int res=0;//影响的行数
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn=getConn();//建立数据库连接
pstmt=conn.prepareStatement(sql);//装载数据库语句
if(params!=null){
//假如有?,在执行前把?占位符替换掉
for(int i=0;i<params.size();i++){
pstmt.setObject(i+1, params.get(i));
}
}
res=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(rs,pstmt,conn);
}
return res>0? true : false;
}
/**
* 使用泛型方法和反射机制进行封装,数据的查询
* @param sql
* @param params
* @param cls
* @return
*/
public <T> List<T> operQuery(String sql,List<Object>params,Class<T> cls)throws Exception{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<T> data=new ArrayList<T>();
try {
conn=getConn();//建立数据库连接
pstmt=conn.prepareStatement(sql);//装载sql语句
if(params!=null){
//假如有?,在执行前把?占位符替换掉
for(int i=0;i<params.size();i++){
pstmt.setObject(i+1, params.get(i));
}
}
rs=pstmt.executeQuery();
//把查询出来的记录封装成对应的实体类对象
ResultSetMetaData rsd=rs.getMetaData();
while(rs.next()){
T m =cls.newInstance();
for(int i=0;i<rsd.getColumnCount();i++){
String col_name=rsd.getColumnName(i+1);//获得列名
Object value=rs.getObject(col_name);//获得列所对应的值
Field field=cls.getDeclaredField(col_name);
field.setAccessible(true);//给私有属性设置可访问权限
field.set(m, value);//给对象的私有属性赋值
}
data.add(m);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(rs,pstmt,conn);
}
return data;
}
}
具体作用代码中有详解。
2.数据库设计:
建立3张表
1).users表,列为:id(用户编号);uname(用户名);upass(用户密码);type(用户类型)。
2).dvds表,列为:ID(dvd编号);dname(dvd名字);dcount(借出次数);status(dvd状态)。
3).records表,列为:ID(记录编号);uid(借dvd人的编号);did(所借dvd的编号);lendtime(接出时间);returntime(归还时间)。
3.用户登录
先写关于users表的相关操作:
private int id;
private String uname;
private String upass;
private int type;
public Users(){
}
public Users(String uname, String upass, int type) {
super();
this.uname = uname;
this.upass = upass;
this.type = type;
}
public Users(int id, String uname, String upass, int type) {
super();
this.id = id;
this.uname = uname;
this.upass = upass;
this.type = type;
}
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
package com.iotek.dao;
import com.iotek.entity.*;
public interface UserDao {
public boolean saveUser(Users user);
public boolean delUser(int id);
public boolean updateUser(Users user);
public Users queryUser(Users user);
}
import java.util.List;
import com.iotek.entity.Users;
public boolean saveUser(Users user) {
String sql="insert into users(uname,upass,type) values(?,?,?)";
List<Object> params=new ArrayList<Object>();
params.add(user.getUname());
params.add(user.getUpass());
params.add(user.getType());
return this.operUpdate(sql, params);
}
public boolean delUser(int id) {
String sql="delete from users where id=?";
List<Object> params=new ArrayList<Object>();
params.add(id);
return this.operUpdate(sql, params);
}
public boolean updateUser(Users user) {
String sql="update users set uname=?,upass=?,type=? where id=?";
List<Object> params=new ArrayList<Object>();
params.add(user.getUname());
params.add(user.getUpass());
params.add(user.getType());
params.add(user.getId());
return this.operUpdate(sql, params);
}
public Users queryUser(Users user) {
List<Users> uList=null;
String sql="select id,uname,upass,type from users where uname=? and upass=? and type=?";
List<Object> params=new ArrayList<Object>();
params.add(user.getUname());
params.add(user.getUpass());
params.add(user.getType());
try {
uList=this.operQuery(sql, params,Users.class);
} catch (Exception e) {
e.printStackTrace();
}
if(uList.size()>0){
return uList.get(0);
}
return null;
}
接下来进行用户登录的书写:
代码如下:
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.iotek.biz.impl.UserBizImpl;
import com.iotek.entity.Users;
private JPanel panel_main= null;
private JPanel panel_left=null;
private JPanel panel_right=null;
private JLabel lb_uname=null;
private JLabel lb_upass=null;
private JLabel lb_type=null;
private JTextField tf_uname=null;
private JPasswordField pf_pass=null;
private JLabel lb_img=null;
private JButton btn_login=null;
private JButton btn_register=null;
private JComboBox cb_type=null;
private UserBiz userBiz=null;
public LoginView(){
userBiz=new UserBizImpl();
init();
registerListener();
}
private void init(){
this.setSize(320, 220);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setTitle("登录窗口");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel_main=new JPanel(new GridLayout(1, 2));
panel_left=new JPanel();
panel_right=new JPanel(new GridLayout(4, 2,0,10));
tf_uname=new JTextField(8);
pf_pass=new JPasswordField(8);
cb_type=new JComboBox();
cb_type.addItem("普通用户");
cb_type.addItem("管理员");
btn_login=new JButton("登录");
btn_register=new JButton("注册") ;
lb_uname=new JLabel("用 户名:",JLabel.CENTER);
lb_upass=new JLabel("密 码:",JLabel.CENTER);
lb_type=new JLabel("类 型:",JLabel.CENTER);
lb_img=new JLabel(new ImageIcon(ClassLoader.getSystemResource("1.jpg")));
panel_left.add(lb_img);
panel_right.add(lb_uname);
panel_right.add(tf_uname);
panel_right.add(lb_upass);
panel_right.add(pf_pass);
panel_right.add(lb_type);
panel_right.add(cb_type);
panel_right.add(btn_login);
panel_right.add(btn_register);
panel_main.add(panel_left);
panel_main.add(panel_right);
this.getContentPane().add(panel_main);
this.pack();
this.setVisible(true);
}
private void registerListener(){
btn_register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new UserRegisterView();
}
});
btn_login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String sname=tf_uname.getText().trim();
String spass=new String(pf_pass.getPassword());
int type=cb_type.getSelectedIndex()+1;
if(sname.equals("")){
JOptionPane.showMessageDialog(LoginView.this, "用户名不能为空");
return;
}else if(spass.equals("")){
JOptionPane.showMessageDialog(LoginView.this, "密码不能为空");
return;
}
Users user=new Users(sname,spass, type);
user=userBiz.login(user);
if(user!=null){
if(user.getType()==1){
new UserMainView(user);
}else{
new AdminMainView(user);
}
LoginView.this.dispose();
}
else{
JOptionPane.showMessageDialog(LoginView.this, "用户名或密码出错");
return;
}
}
});
}
}
- 数据库连接与用户登录
- java 与数据库连接 实现用户的注册和登录
- java 与数据库连接 实现用户的注册和登录
- javaWeb 简单注册登录(含数据库连接) -- (四) 用户登录请求与响应
- asp.net入门---数据库连接和登录用户
- 登录界面与SQL数据库连接
- mfc登录界面与数据库连接
- 关于数据库连接和在WINFROM下做用户登录
- 登录之记住用户与自动登录
- 登录之记住用户与自动登录
- 登录之记住用户与自动登录
- PHP 用户注册与登录
- Cookie与session 用户登录
- 用户登录与帐号管理
- 登录时session与用户
- Linux-用户帐号与登录
- Velocity与Struts验证用户登录
- SQL Server 关联用户与登录名
- 2.1版本修改.git status,git diff
- 树莓派手动指定静态IP和DNS 终极解决大法
- faster rcnn源码解析(持续更新中)
- 【1701H1】【穆晨】【171119】连续第四十天总结
- iOS C语言~bzero函数、memset函数
- 数据库连接与用户登录
- windows下模块分析软件——Dependency Walker
- 文章标题
- 块调整方案K2-Photogrammetry.PATB.v3.6.278最高规格
- 死锁的检测和解除
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink高级特性与高级应用009-Slot和Parallelism的深入分析004
- 很low的小东西(即时通讯)
- C++求最大值编程
- JS中的prototype