package dao;
/**
* @author 作者: guya
* description:
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.Hero;
public class HeroDAO {
public int getTotal() {
Connection c=null;
Statement s=null;
ResultSet rs=null;
int total = 0;
try {
c=DbUtil.getConnection();
s= c.createStatement();
String sql = "select count(*) from hero";
rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.close(c, s, rs);
}
return total;
}
public void add(Hero hero) {
Connection c=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
c=DbUtil.getConnection();
String sql = "insert into hero values(null,?,?,?)";
ps = c.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, hero.name);
ps.setFloat(2, hero.hp);
ps.setInt(3, hero.damage);
ps.execute();
rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
hero.id = id;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.close(c, ps, rs);
}
}
public void update(Hero hero) {
Connection c=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
c=DbUtil.getConnection();
String sql = "update hero set name= ?, hp = ? , damage = ? where id = ?";
ps = c.prepareStatement(sql);
ps.setString(1, hero.name);
ps.setFloat(2, hero.hp);
ps.setInt(3, hero.damage);
ps.setInt(4, hero.id);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.close(c, ps);
}
}
public void delete(int id) {
Connection c=null;
Statement s=null;
ResultSet rs=null;
try {
c=DbUtil.getConnection();
s = c.createStatement();
String sql = "delete from hero where id = " + id;
s.execute(sql);
}catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.close(c, s);
}
}
public Hero get(int id) {
Connection c=null;
Statement s=null;
ResultSet rs=null;
Hero hero = null;
try {
c=DbUtil.getConnection();
s = c.createStatement();
String sql = "select * from hero where id = " + id;
rs = s.executeQuery(sql);
if (rs.next()) {
hero = new Hero();
hero.setName(rs.getString(2));
hero.setHp(rs.getFloat("hp"));
hero.setDamage(rs.getInt(4));
hero.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.close(c, s, rs);
}
return hero;
}
public List<Hero> list() {
return list(0, Short.MAX_VALUE);
}
public List<Hero> list(int start, int count) {
Connection c=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<Hero> heros = new ArrayList<Hero>();
try {
c=DbUtil.getConnection();
String sql = "select * from hero order by id desc limit ?,? ";
ps = c.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, count);
rs = ps.executeQuery();
while (rs.next()) {
Hero hero = new Hero();
hero = new Hero();
hero.setName(rs.getString(2));
hero.setHp(rs.getFloat("hp"));
hero.setDamage(rs.getInt(4));
hero.setId(rs.getInt(1));
heros.add(hero);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DbUtil.close(c, ps, rs);
}
return heros;
}
}
最近下载更多
liuliuyl LV3
2023年6月17日
小顾顾顾顾顾 LV4
2023年6月7日
ZDM133 LV2
2023年5月22日
人生三重奏 LV1
2022年12月21日
无名氏111 LV33
2022年12月16日
1379585889 LV11
2022年12月14日
bigstone1 LV1
2022年11月9日
微信网友_6208827097387008 LV1
2022年11月7日
sdkjfhg LV1
2022年3月6日
guanxiaopeng LV1
2021年12月21日

最近浏览