package dbcon;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class DBdao {
//查询商品类别
public ArrayList getbooktype()
{
ArrayList list=new ArrayList();
String sql="select * from booktype_tb";
Connection conn=null;
try {
conn=DBcon.getconn();
Statement sta=conn.createStatement();
ResultSet res=sta.executeQuery(sql);
while(res!=null&&res.next())
{
booktype_tb type=new booktype_tb();
type.setType_id(res.getInt("type_id"));
type.setType_name(res.getString("type_name"));
type.setType_info(res.getString("type_info"));
list.add(type);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//查询销量前十的商品
public ArrayList getbook()
{
ArrayList list=new ArrayList();
String sql="select top 10 * from book_tb b ,(select book_id, sum(subscibe_num) num from userorderinfo_tb group by book_id) s where b.bookid = s.book_id order by s.num desc";
System.out.println(sql);
Connection conn=DBcon.getconn();
try {
Statement sta=conn.createStatement();
ResultSet res= sta.executeQuery(sql);
while(res!=null&&res.next())
{
book_tb book=new book_tb();
book.setBookid(res.getInt("bookid"));
book.setBookname(res.getString("bookname"));
book.setAutor(res.getString("autor"));
book.setBook_concern(res.getString("book_concern"));
book.setSynopsis(res.getString("synopsis"));
book.setList(res.getString("list"));
book.setImage(res.getString("image"));
book.setMarket_price(res.getFloat("market_price"));
book.setPreferential_price(res.getFloat("preferential_price"));
book.setType_id(res.getInt("type_id"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//根据图书类别查询图书
public ArrayList getbookbytype(String type_name)
{
ArrayList list=new ArrayList();
String sql="select * from book_tb where type_id=(select type_id from booktype_tb where type_name='"+type_name+"')";
Connection conn=DBcon.getconn();
try {
Statement sta=conn.createStatement();
ResultSet res= sta.executeQuery(sql);
while(res!=null&&res.next())
{
book_tb book=new book_tb();
book.setBookid(res.getInt("bookid"));
book.setBookname(res.getString("bookname"));
book.setAutor(res.getString("autor"));
book.setBook_concern(res.getString("book_concern"));
book.setSynopsis(res.getString("synopsis"));
book.setList(res.getString("list"));
book.setImage(res.getString("image"));
book.setMarket_price(res.getFloat("market_price"));
book.setPreferential_price(res.getFloat("preferential_price"));
book.setType_id(res.getInt("type_id"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//新书上架 查询最进刚出来的书 根据id 自增
public ArrayList getnewbook()
{
ArrayList list=new ArrayList();
String sql="select top 10 * from book_tb order by bookid desc";
Connection conn=DBcon.getconn();
try {
Statement sta=conn.createStatement();
ResultSet res= sta.executeQuery(sql);
while(res!=null&&res.next())
{
book_tb book=new book_tb();
book.setBookid(res.getInt("bookid"));
book.setBookname(res.getString("bookname"));
book.setAutor(res.getString("autor"));
book.setBook_concern(res.getString("book_concern"));
book.setSynopsis(res.getString("synopsis"));
book.setList(res.getString("list"));
book.setImage(res.getString("image"));
book.setMarket_price(res.getFloat("market_price"));
book.setPreferential_price(res.getFloat("preferential_price"));
book.setType_id(res.getInt("type_id"));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//根据图书id 查询该图书所有信息
public book_tb selectbyid(int bookid)
{
book_tb book=new book_tb();
String sql="select * from book_tb where bookid="+bookid;
Connection conn=DBcon.getconn();
try {
Statement sta=conn.createStatement();
ResultSet res= sta.executeQuery(sql);
while(res!=null&&res.next())
{
book.setBookid(res.getInt("bookid"));
book.setBookname(res.getString("bookname"));
book.setAutor(res.getString("autor"));
book.setBook_concern(res.getString("book_concern"));
book.setSynopsis(res.getString("synopsis"));
book.setList(res.getString("list"));
book.setImage(res.getString("image"));
book.setMarket_price(res.getFloat("market_price"));
book.setPreferential_price(res.getFloat("preferential_price"));
book.setType_id(res.getInt("type_id"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return book;
}
/*
* 新用户注册
*/
public boolean adduser(user_tb user)
{
String sql="insert into user_tb values (?,?,?,?,?,?,?,?)";
PreparedStatement pps=null;
Connection conn=DBcon.getconn();
try {
pps= conn.prepareStatement(sql);
pps.setString(1, user.getUser_name());
pps.setString(2, user.getUser_psw());
pps.setString(3, user.getUser_truename());
pps.setString(4, user.getUser_email());
pps.setString(5, user.getUser_tel());
pps.setString(6, user.getUser_address());
pps.setString(7, user.getUser_type());
pps.setString(8, user.getUser_state());
int i=pps.executeUpdate();
if(i==1)
{
System.out.println("添加成功!");
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
if(pps!=null)
{
try {
pps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
}
/*
* 用户登陆查询
*/
public user_tb selectuser(String username,String psw)
{
String sql="select * from user_tb where user_psw='"+psw+"' and user_name='"+username+"'";
Connection conn=DBcon.getconn();
ResultSet res=null;
Statement stm=null;
try {
stm=conn.createStatement();
res=stm.executeQuery(sql);
if(res!=null&&res.next())
{
user_tb user=new user_tb();
user.setUser_id(res.getInt("user_id"));
user.setUser_name(res.getString("user_name"));
user.setUser_psw(res.getString("user_psw"));
user.setUser_address(res.getString("user_address"));
user.setUser_email(res.getString("user_email"));
user.setUser_tel(res.getString("user_tel"));
user.setUser_truename(res.getString("user_truename"));
user.setUser_type(res.getString("user_type"));
System.out.println("登陆成功");
return user;
}
else
{
System.out.println("用户名或密码输入错误请重新登陆");
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
finally
{
if(res!=null)
{
try {
res.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(stm!=null)
{
try {
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
* 用户登陆后修改个人信息
*/
public boolean updateuser(user_tb user,int userid)
{
Stri