package database;
import java.sql.*;
import java.util.ArrayList;
import beans.UserInf;
public class DBConn {
private Connection conn;// 数据库链接对象
private PreparedStatement stmt;// 预编译对象
private CallableStatement sc;
private ResultSet rs;// 结果集对象
public DBConn()
{
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/tong?user=root&password=1234&useUnicode=true&characterEncoding=gb2312");
} catch (Exception e) {
}
}
public boolean check(String name)
{
try {
stmt=conn.prepareStatement("select * from userinf where name=?");
stmt.setString(1, name);
rs = stmt.executeQuery();
if(rs.next())
{
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void reg(UserInf u)
{
try {
stmt=conn.prepareStatement("insert into userinf(name,pwd,addr,tel,truename,email) values(?,?,?,?,?,?)");
stmt.setString(1, u.getName());
stmt.setString(2, u.getPwd());
stmt.setString(3, u.getAddr());
stmt.setString(4, u.getTel());
stmt.setString(5, u.getTruename());
stmt.setString(6, u.getEmail());
stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public UserInf login(String name,String pwd)
{
try {
stmt=conn.prepareStatement("select * from userinf where name=? and pwd = ?");
stmt.setString(1, name);
stmt.setString(2, pwd);
rs = stmt.executeQuery();
if(rs.next())
{
UserInf u = new UserInf();
u.setId(rs.getInt("id"));
u.setAddr(rs.getString("addr"));
u.setEmail(rs.getString("email"));
u.setName(rs.getString("name"));
u.setPwd(rs.getString("pwd"));
u.setQuanxian(rs.getString("quanxian"));
u.setTel(rs.getString("tel"));
u.setTruename(rs.getString("truename"));
return u;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public ArrayList search(String search)
{
ArrayList<UserInf> a = new ArrayList<UserInf>();
try {
stmt=conn.prepareStatement("select * from userinf where name like ?");
stmt.setString(1, "%"+search+"%");
rs = stmt.executeQuery();
while(rs.next())
{
UserInf u = new UserInf();
u.setId(rs.getInt("id"));
u.setAddr(rs.getString("addr"));
u.setEmail(rs.getString("email"));
u.setName(rs.getString("name"));
u.setPwd(rs.getString("pwd"));
u.setQuanxian(rs.getString("quanxian"));
u.setTel(rs.getString("tel"));
u.setTruename(rs.getString("truename"));
a.add(u);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public ArrayList searchAll()
{
ArrayList<UserInf> a = new ArrayList<UserInf>();
try {
stmt=conn.prepareStatement("select * from userinf");
rs = stmt.executeQuery();
while(rs.next())
{
UserInf u = new UserInf();
u.setId(rs.getInt("id"));
u.setAddr(rs.getString("addr"));
u.setEmail(rs.getString("email"));
u.setName(rs.getString("name"));
u.setPwd(rs.getString("pwd"));
u.setQuanxian(rs.getString("quanxian"));
u.setTel(rs.getString("tel"));
u.setTruename(rs.getString("truename"));
a.add(u);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public boolean del(int id)
{
boolean a = false;
try {
stmt=conn.prepareStatement("delete from userinf where id = ?");
stmt.setInt(1, id);
stmt.execute();
a = true;
return a;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public UserInf searchID(int id)
{
UserInf u = new UserInf();
try {
stmt=conn.prepareStatement("select * from userinf where id = ?");
stmt.setInt(1, id);
rs = stmt.executeQuery();
if(rs.next())
{
u.setId(rs.getInt("id"));
u.setAddr(rs.getString("addr"));
u.setEmail(rs.getString("email"));
u.setName(rs.getString("name"));
u.setPwd(rs.getString("pwd"));
u.setQuanxian(rs.getString("quanxian"));
u.setTel(rs.getString("tel"));
u.setTruename(rs.getString("truename"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return u;
}
public int change(UserInf u)
{
int i = 0;
try {
stmt=conn.prepareStatement("update userinf set name = ? , pwd = ? , addr= ? , email=? , tel=? , truename=? where id = ?");
stmt.setString(1, u.getName());
stmt.setString(2, u.getPwd());
stmt.setString(3, u.getAddr());
stmt.setString(4, u.getEmail());
stmt.setString(5, u.getTel());
stmt.setString(6, u.getTruename());
stmt.setInt(7, u.getId());
stmt.execute();
i = 1;
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
}