IBandDao接口代码如下:
package cn.edu.mju.project1.persiste;
import cn.edu.mju.project1.entity.Band;
import java.util.List;
public interface IBandDao {
public boolean insert(Band band);//只需将表的对象传过来,添加数据到数据库band表中
public boolean update(Band band);//根据主键修改数据库表中相应的数据
public boolean delete(Integer id);//通过主键删除记录的话只需将主键传过来
public Band findById(Integer id);//通过主键查找单个ID数据,将实体类对象返回
public List<Band> findALL();//查找所有数据,用列表的形式将一个个对象返回回去
public List<Band> find(Band band);//按照查询查找条件,将结果放入列表中
public List<Band> pagenate(int page,int pageSize,Band band);//分页查询第几页,一页由几条记录,查询条件?
}
写完接口中抽象方法后,再来写对应实现类:
package cn.edu.mju.project1.persiste.impl;
import cn.edu.mju.project1.entity.Band;
import cn.edu.mju.project1.persiste.IBandDao;
import cn.edu.mju.project1.service.impl.MySqlDbUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class BandDao implements IBandDao {
@Override
public boolean insert(Band band) {
boolean ret = false;
try {
Connection conn =MySqlDbUtil.getConnection();
String sql ="INSERT INTO band(name,remark) VALUES(?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);//将sql语句传递给该对象,并将其放入变量中
pstmt.setString(1,band.getName());//获取姓名添加到band表中
pstmt.setString(2,band.getRemark());
int result = pstmt.executeUpdate();//执行pstmt对象,对其返回的boolean值进行判断
System.out.println(result);
if(result > 0){
ret = true;
}
pstmt.close();
conn.close();//使用完,将其关闭,避免浪费资源
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean update(Band band) {
boolean ret = false;
try {
Connection conn =MySqlDbUtil.getConnection();
String sql ="UPDATE band SET name=?,remark=? where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);//将sql语句传递给该对象,并将其放入变量中
pstmt.setString(1,band.getName());
pstmt.setString(2,band.getRemark());
pstmt.setInt(3,band.getId());
int result = pstmt.executeUpdate();//执行pstmt对象,对其返回的boolean值进行判断
System.out.println(result);
if(result > 0){
ret = true;
}
pstmt.close();
conn.close();//使用完,将其关闭,避免浪费资源
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean delete(Integer id) {
boolean ret = false;
try {
Connection conn = MySqlDbUtil.getConnection();
String sql ="DELETE FROM band where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);//将sql语句传递给该对象,并将其放入变量中
pstmt.setInt(1,id);
int result = pstmt.executeUpdate();//执行pstmt对象,对其返回的boolean值进行判断
System.out.println(result);
if(result > 0){
ret= true;
}
pstmt.close();
conn.close();//使用完,将其关闭,避免浪费资源
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
@Override
public Band findById(Integer id) {
Band band = null;
try {
Connection conn =MySqlDbUtil.getConnection();
String sql ="SELECT * FROM band where name LIKE ?";
PreparedStatement pstmt = conn.prepareStatement(sql);//将sql语句传递给该对象,并将其放入变量中
pstmt.setInt(1,id);
ResultSet rs = pstmt.executeQuery();//返回结果集
while (rs.next()){
band = new Band();
band.setId(rs.getInt("id"));
band.setName(rs.getString("name"));
band.setRemark(rs.getString("remark"));
}
rs.close();
pstmt.close();
conn.close();//使用完,将其关闭,避免浪费资源
} catch (Exception e) {
e.printStackTrace();
}
return band;
}
@Override
public List<Band> findALL() {
List<Band> bands= new ArrayList<>();//建立List对象,并实例化
try {
Connection conn =MySqlDbUtil.getConnection();
String sql ="SELECT * FROM band ";
PreparedStatement pstmt = conn.prepareStatement(sql);//将sql语句传递给该对象,并将其放入变量中
ResultSet rs = pstmt.executeQuery();//返回结果集
while (rs.next()){
Band band = new Band();
band.setId(rs.getInt("id"));//取出ID的值将其放在Band对象中去
band.setName(rs.getString("name"));
band.setRemark(rs.getString("remark"));
bands.add(band);//将对象放入列表对象中
}
rs.close();
pstmt.close();
conn.close();//使用完,将其关闭,避免浪费资源
} catch (Exception e) {
e.printStackTrace();
}
return bands;
}
@Override
public List<Band> find(Band band) {
List<Band> bands= new ArrayList<>();//建立List对象,并实例化
List<Object> params =new ArrayList<>();
try {
Connection conn =MySqlDbUtil.getConnection();
String sql ="SELECT * FROM band WHERE 1=1 ";//分语句查找
if(band !=null){
if(band.getName() !=null && !"".equals(band.getName())){
sql = sql +"AND name LIKE ?";//添加mysql语句
params.add("%"+ band.getName() + "%");
}
if(band.getRemark() !=null && !"".equals(band.getRemark())){
sql = sql +"AND remark LIKE ?";
params.add("%"+ band.getRemark() + "%");
}
}
PreparedStatement pstmt = conn.prepareStatement(sql);//将sql语句传递给该对象,并将其放入变量中
for(int i=0;i<params.size();i++){
pstmt.setObject(i+1,params.get(i));
}
ResultSet rs = pstmt.executeQuery();//返回结果集
while (rs.next()){
band = new Band();
band.setId(rs.getInt("id"));//取出ID的值将其放在Band对象中去
band.setName(rs.getString("name"));
band.setRemark(rs.getString("remark"));
bands.add(band);//将对象放入列表对象中
}
rs.close();
pstmt.close();
conn.close();//使用完,将其关闭,避免浪费资源
} catch (Exception e) {
e.printStackTrace();
}
return bands;
}
@Override
public List<Band> pagenate(int page, int pageSize, Band band) {
return null;
}
}
本文中只写了增删改 查询全部数据,通过输入ID来查询数据,通过输入名称和备注信息来进行查找。