Dao层实现
public class GoodsDao {
private static Connection conn=null;//Connection接口,是特定的数据库的连接。通过DriverManager类的静态方法的getConnection()方法来获取。
public static void initConnection() throws Exception{//建立连接
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8","root","root");
}
public static List<Goods> getSelect(String goodscon) {
List<Goods> list1 = new ArrayList<Goods>();
try {
Daoconn.init();
ResultSet rs= Daoconn.selectSql(" SELECT * FROM goods where goodscon LIKE '%"+goodscon+"%' ");
while (rs.next()) {
Goods goods = new Goods();
goods.setGoodsid(rs.getString("goodsid"));
goods.setType(rs.getString("type"));
goods.setGoodsimg(rs.getString("goodsimg"));
goods.setGoodscon(rs.getString("goodscon"));
list1.add(goods);
}
Daoconn.closeConn();
return list1;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void closeConnection() throws Exception{//断开连接
conn.close();
}
}