📄 logindao.java
字号:
package shop;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
public class loginDAO {
DBTools dao = new DBTools();
public boolean checkLogin(loginBean lBean)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt=con.prepareStatement(
"select name,pass from login where name=? and pass=?");
pstmt.setString(1,lBean.getName());
pstmt.setString(2,lBean.getPass());
rs = pstmt.executeQuery();
if(rs.next())
{
flag=true;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally
{
dao.close(con,pstmt,rs);
}return flag;
}
public boolean checkUser(String name)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt=con.prepareStatement(
"select name from login where name=?");
pstmt.setString(1,name);
rs = pstmt.executeQuery();
if(rs.next())
{
flag=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
dao.close(con,pstmt,rs);
}return flag;
}
/**
* 添加用户
* @param lBean
* @return
*/
public boolean addUser(loginBean lBean)
{
Connection con = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt = con.prepareStatement("insert into login (name,pass,dengji) values (?,?,?)");
pstmt.setString(1,lBean.getName());
pstmt.setString(2,lBean.getPass());
pstmt.setInt(3,lBean.getDengji());
int num = pstmt.executeUpdate();
if(num >0 )
{
flag= true;
}
else
{
flag = false;
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
dao.close(con,pstmt);
}
return flag;
}
/**
* 根据名字查等级
* @param name
* @return
*/
public loginBean checkdengji(String name)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
loginBean lBean = new loginBean();
try {
con = dao.getConnection();
pstmt=con.prepareStatement(
"select dengji from login where name=?");
pstmt.setString(1,name);
rs = pstmt.executeQuery();
if(rs.next())
{
lBean.setDengji(rs.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
dao.close(con,pstmt,rs);
}return lBean;
}
/**
* 查询所有用户信息
* @return
*/
public Collection upyonghu( )
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Collection <loginBean> co = new ArrayList<loginBean>();
try {
con = dao.getConnection();
pstmt=con.prepareStatement(
"select name,pass,dengji from login ");
rs = pstmt.executeQuery();
while(rs.next())
{
loginBean lBean = new loginBean();
lBean.setName(rs.getString(1));
lBean.setPass(rs.getString(2));
lBean.setDengji(rs.getInt(3));
co.add(lBean);
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
dao.close(con,pstmt,rs);
}return co;
}
/**
* 根据loginBean更新用户
* @param l
* @return
*/
public boolean upaddyonghu(loginBean l)
{
Connection con = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"update login set name=?,pass = ?,dengji = ? where name = ?");
pstmt.setString(1,l.getName());
pstmt.setString(2,l.getPass());
pstmt.setInt(3,l.getDengji());
pstmt.setString(4,l.getName());
int num = pstmt.executeUpdate();
if(num >0 )
{
flag= true;
}
else
{
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
}finally
{
dao.close(con,pstmt);
}
return flag;
}
/**
* 根据姓名查询用户信息
* @return
*/
public loginBean seletyonghu( String name)
{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
loginBean lBean = new loginBean();
try {
con = dao.getConnection();
pstmt=con.prepareStatement(
"select name,pass,dengji from login where name=?");
pstmt.setString(1,name);
rs = pstmt.executeQuery();
if(rs.next())
{
lBean.setName(rs.getString(1));
lBean.setPass(rs.getString(2));
lBean.setDengji(rs.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
}finally
{
dao.close(con,pstmt,rs);
}return lBean;
}
/**
* 根据loginBean更新用户等级
* @param l
* @return
*/
public boolean upayonghu(loginBean l)
{
Connection con = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
con = dao.getConnection();
pstmt = con.prepareStatement(
"update login set dengji = ? where name = ?");
pstmt.setInt(1,l.getDengji());
pstmt.setString(2,l.getName());
int num = pstmt.executeUpdate();
if(num >0 )
{
flag= true;
}
else
{
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
}finally
{
dao.close(con,pstmt);
}
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -