package com.mybean;
import java.sql.SQLException;
import java.util.ArrayList;
public class user {
private String loginname;
private String name;
private String password;
private String sex;
private String birthday;
private String email;
private int roleid;
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRoleid() {
return roleid;
}
public void setRoleid(int roleid) {
this.roleid = roleid;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean insertuser()
{
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
String sql="insert into users values(?,?,?,?,?,?,default)";
try {
dbc.presta=dbc.con.prepareStatement(sql);
dbc.presta.setString(1, this.loginname);
dbc.presta.setString(2, this.name);
dbc.presta.setString(3,this.password);
dbc.presta.setString(4,this.sex);
dbc.presta.setString(5, this.birthday);
dbc.presta.setString(6,this.email);
//dbc.presta.setInt(6, this.roleid);
dbc.presta.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
public ArrayList selectAllUser()
{
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
ArrayList list=new ArrayList();
user us=null;
String sql="select * from users";
try {
dbc.rs=dbc.sta.executeQuery(sql);
while(dbc.rs.next())
{
us=new user();
us.setLoginname(dbc.rs.getString(1));
us.setName(dbc.rs.getString(2));
us.setPassword(dbc.rs.getString(3));
us.setSex(dbc.rs.getString(4));
us.setBirthday(dbc.rs.getString(5));
us.setEmail(dbc.rs.getString(6));
list.add(us);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
dbc.close();
return list;
}
public user selectUser(String loginname)
{
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
user us=new user();
String sql="select * from users where loginname='"+loginname+"'";
try {
dbc.rs=dbc.sta.executeQuery(sql);
while(dbc.rs.next())
{
us.setLoginname(dbc.rs.getString(1));
us.setName(dbc.rs.getString(2));
us.setPassword(dbc.rs.getString(3));
us.setSex(dbc.rs.getString(4));
us.setBirthday(dbc.rs.getString(5));
us.setEmail(dbc.rs.getString(6));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
dbc.close();
return us;
}
//判断是否有此用户
public boolean findUser()
{
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
String str="select * from users where loginname='"+this.loginname+"'";
try {
dbc.rs=dbc.sta.executeQuery(str);
while(dbc.rs.next())
{
return true;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}
return false;
}
public boolean selectUser()
{
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
String str="select * from users where loginname='"+this.loginname+"'and password='"+this.password+"' and roleid="+this.roleid+"";
try {
dbc.rs=dbc.sta.executeQuery(str);
while(dbc.rs.next())
{
return true;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
}
return false;
}
public String getuserPassword()
{
String password;
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
String sql="select password from users where loginname='"+this.loginname+"' and email='"+this.email+"'";
try {
dbc.rs=dbc.sta.executeQuery(sql);
while(dbc.rs.next())
{
password=dbc.rs.getString(1);
return password;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return null;
}
public boolean updateUser()
{
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
String sql="update users set name='"+this.name+"',password='"+this.password+"',sex='"+this.sex+"',birthday='"+this.birthday+"',email='"+this.email+"'where loginname='"+this.loginname+"'";
try {
dbc.sta.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
public boolean deleteUser()
{
DBConnection dbc=new DBConnection();
if(dbc.getCon()==null)
{
dbc.getCon();
}
String sql="delete from users where loginname='"+this.loginname+"'";
try {
dbc.sta.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
}