package Goods.Dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import Goods.JavaBean.GoodsBean;
import Common.*;
public class IGoodsDao {
public GoodsBean getOneGoods(String sql)
{
try
{
Connection conn = getConnection();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
GoodsBean goodsBean = new GoodsBean();
if(rs.next())
{
goodsBean.setId(rs.getString("Id"));
goodsBean.setGname(rs.getString("Gname"));
goodsBean.setContent(rs.getString("Content"));
goodsBean.setPrice(rs.getString("Price"));
goodsBean.setPic_goods(rs.getString("Pic_goods"));
goodsBean.setSnum(rs.getString("Snum"));
}
return goodsBean;
}
catch(Exception e)
{
return null;
}
}
public ArrayList<GoodsBean> getMoreGoods(String sql)
{
ArrayList<GoodsBean> res = new ArrayList<GoodsBean>();
try
{
Connection conn = getConnection();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
GoodsBean goodsBean = new GoodsBean();
goodsBean.setId(rs.getString("Id"));
goodsBean.setGname(rs.getString("Gname"));
goodsBean.setContent(rs.getString("Content"));
goodsBean.setPrice(rs.getString("Price"));
goodsBean.setPic_goods(rs.getString("Pic_goods"));
goodsBean.setSnum(rs.getString("Snum"));
res.add(goodsBean);
}
rs.close();
statement.close();
conn.close();
return res;
}
catch(SQLException e)
{
e.printStackTrace();
}
return res;
}
public boolean saveGoods(GoodsBean goodsBean)
{
String sql = "insert into tbl_goods(Gname,Content,Price,Pic_goods,Snum)"+
"values('"+goodsBean.getGname()+"'," +
"'"+goodsBean.getContent()+"','"+goodsBean.getPrice()+"','"+goodsBean.getPic_goods()+"','"+goodsBean.getSnum()+"')";
Database database = new Database();
boolean res = database.execute(sql);
return res;
}
public boolean deleteGoods(GoodsBean goodsBean)
{
String sql = "delete from tbl_goods where Id = '"+goodsBean.getId()+"'";
Database database = new Database();
boolean res = database.execute(sql);
return res;
}
public Connection getConnection() {
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/petwebsite?useUnicode=true&characterEncoding=UTF-8","root","123456");
}
catch(Exception e)
{
conn = null;
}
return conn;
}
public boolean add(GoodsBean goodsBean) {
try{
Connection conn = getConnection();
Statement statement = conn.createStatement();
String sql = "insert into tbl_goods(Gname,Content,Price,Pic_goods,Snum) values('"+goodsBean.getGname()+"','"+goodsBean.getContent()+"','"+goodsBean.getPrice()+"','"+goodsBean.getPic_goods()+"','"+goodsBean.getSnum()+"')";
statement.execute(sql);
statement.close();
conn.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
public boolean delete(String sql){
try
{
Connection conn = getConnection();
Statement statement = conn.createStatement();
statement.execute(sql);
statement.close();
conn.close();
return true;
}
catch(Exception e)
{
return false;
}
}
public boolean change(GoodsBean goodsBean)
{
try{
Connection conn = getConnection();
Statement statement = conn.createStatement();
String sql = "update tbl_goods set Gname = '"+goodsBean.getGname()+"',Price = '"+goodsBean.getPrice()+"' ,Content = '"+goodsBean.getContent()+"',Pic_goods = '"+goodsBean.getPic_goods()+"',Snum ='"+goodsBean.getSnum()+"' where Id = '"+goodsBean.getId()+"'";
statement.execute(sql);
statement.close();
conn.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
}