package cn.edu.mju.project1.entity;
/**
* 用户实体类
* CREATE TABLE `band` (
* `id` int(11) NOT NULL,
* `name` varchar(40) NOT NULL,
* `remark` varchar(32) NOT NULL,
* PRIMARY KEY (`id`),
* UNIQUE KEY `name_UNIQUE` (`name`)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
public class Band {
private Integer id;
private String name;
private String remark;
public Band() {
}
public Band(Integer id, String name, String remark) {
this.id = id;
this.name = name;
this.remark = remark;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
该类的代码请参考我的这篇文章:JDBC数据库连接池
package cn.edu.mju.project1.persiste;
import cn.edu.mju.project1.entity.Band;
import cn.edu.mju.project1.util.Pager;
import java.util.List;
public interface IBandDao {
/**
* 添加数据到数据库band表中
* @param band
* @return
*/
public boolean insert(Band band);
/**
* 根据主键修改数据库表中的相应数据
* @param band
* @return
*/
public boolean update(Band band);
/**
* 根据主键删除记录
* @param id
* @return
*/
public boolean delete(Integer id);
/**
* 通过主键来查找数据
* @param id 主键编号
* @return
*/
public Band findById(Integer id);
/**
* 查找所有的数据
* @return
*/
public List<Band> findAll();
/**
* 根据条件查询相关数据
* @param band
* @return
*/
public List<Band> find(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.util.Pager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* 数据库连接与操作类用于增删改查数据并返回给servlet使用
*/
public class BandDao implements IBandDao {
/**
* 实现添加数据到数据库band表中
* @param band
* @return
*/
@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);//PreparedStatement 获得SQL语句
pstmt.setString(1,band.getName());
pstmt.setString(2, band.getRemark());
int result = pstmt.executeUpdate();//executeUpdate()的返回值是更新的条数(更新的记录数量)
if(result > 0){
ret = true;
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* 实现根据主键修改数据库表中的相应数据
* @param band
* @return
*/
@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);
pstmt.setString(1,band.getName());
pstmt.setString(2, band.getRemark());
pstmt.setInt(3, band.getId());
int result = pstmt.executeUpdate();
if(result > 0){
ret = true;
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* 实现根据主键删除记录
* @param id
* @return
*/
@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);
pstmt.setInt(1, id);
int result = pstmt.executeUpdate();
if(result > 0){
ret = true;
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* 实现通过主键来查找数据
* @param id 主键编号
* @return
*/
@Override
public Band findById(Integer id) {
Band band = null;
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "SELECT * FROM band WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,id);
ResultSet rs = pstmt.executeQuery();//executeQuery()方法会把数据库响应的查询结果存放在ResultSet类对象中供使用。
/**
* 1、rs = stmt.exeQuery(sql)后,rs肯定不会为null,注意,是rs对象不会为null,而不是其包含的result
* 2、既然rs不会为null,所以在rs = stmt.exeQuery(sql)后,使用while(rs.next())或者if(rs.next())时就不用判断rs != null;
* 3、rs.next时,游标最先是指向第一条记录前的位置,所以第一次rs.next后,游标指向的正好是第一条记录(如果有的话)
* 4、要判断rs的结果是否为空,只要使用一次rs.next方法就行了,如果它返回为false,则证明rs的结果为null
*/
if(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;
}
/**
* 实现查找所有的数据
* @return
*/
@Override
public List<Band> findAll() {
List<Band> bands = new ArrayList<>();
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "SELECT * FROM band";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Band band = new Band();
band.setId(rs.getInt("id"));
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;
}
/**
* 实现根据条件查询相关数据
* @param band
* @return
*/
@Override
public List<Band> find(Band band) {
List<Band> bands = new ArrayList<>();
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 ? ";
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);
for(int i=0; i< params.size(); i++){
//问号是从1开始的
pstmt.setObject(i+1, params.get(i));
}
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"));
bands.add(band);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return bands;
}
import cn.edu.mju.project1.persiste.impl.MySqlDbUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TestDbUtil {
@Test
public void testInsert(){
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "INSERT INTO band(name,remark) VALUES(?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"软工1班");
pstmt.setString(2, "备注信息");
int result = pstmt.executeUpdate();
if(result > 0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testUpdate(){
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "UPDATE band SET name=?,remark=? WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"软工2班");
pstmt.setString(2, "备注信息2");
pstmt.setInt(3, 4);
int result = pstmt.executeUpdate();
if(result > 0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testDelete(){
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "DELETE FROM band WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 5);
int result = pstmt.executeUpdate();
if(result > 0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testFindAll(){
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "SELECT * FROM band";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String remark = rs.getString("remark");
System.out.println(id + ", " + name + ", " + remark);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testFindById(){
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "SELECT * FROM band WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,6);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String remark = rs.getString("remark");
System.out.println(id + ", " + name + ", " + remark);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testFindByName(){
try {
Connection conn = MySqlDbUtil.getConnection();
String sql = "SELECT * FROM band WHERE name LIKE ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%1%");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String remark = rs.getString("remark");
System.out.println(id + ", " + name + ", " + remark);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}