package jdbctest;
import java.util.List;
public interface DAO {
public void add(Hero hero);
public void update(Hero hero);
public void delete(int id);
public Hero get(int id);
public List<Hero> list(int start, int count);
}
package jdbctest;
import java.sql.DriverManager;
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 java.sql.Connection;
public class HeroDAO implements DAO{
public HeroDAO(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnections() throws SQLException{
return DriverManager.getConnection("jdbc:mysql://192.168.88.120:3306:3306/db_hero?characterEncoding=UTF-8","root","123456");
}
public int getTotal(){
int total = 0;
try(Connection c = getConnections();Statement s = c.createStatement();){
String sql = "select count(*) from hero";
ResultSet rs = s.executeQuery(sql);
while(rs.next()){
total = rs.getInt(1);
}
System.out.println("total:" + total);
}catch(SQLException e){
e.printStackTrace();
}
return total;
}
public void add(Hero hero){
String sql = "insert into hero values(null,?,?,?)";
try (Connection c = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, hero.name);
ps.setFloat(2, hero.hp);
ps.setInt(3, hero.damage);
ps.execute();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
hero.id = id;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(Hero hero){
String sql = "update hero set name = ?, hp = ?, damage = ? where id = ?";
try (Connection c = getConnections(); PreparedStatement 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();
}
}
public void delete(int id){
String sql = "delete from hero where id = ?";
try (Connection c = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1,id);
ps.execute();
}catch(SQLException e){
e.printStackTrace();
}
}
@Override
public List<Hero> list(int start, int count) {
ArrayList<Hero> heros = new ArrayList();
String sql = "select *from hero limit ?,?";
try (Connection c = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1,start);
ps.setInt(2,count);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Hero hero = new Hero();
hero.id = rs.getInt(1);
hero.name = rs.getString(2);
hero.hp = rs.getFloat(3);
hero.damage = rs.getInt(4);
heros.add(hero);
}
} catch (SQLException e) {
e.printStackTrace();
}
return heros;
}
@Override
public Hero get(int id) {
Hero hero = null;
String sql = "select *from hero where id = ?";
try (Connection c = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();
if(rs.next()){
hero = new Hero();
hero.id = rs.getInt(1);
hero.name = rs.getString(2);
hero.hp = rs.getFloat(3);
hero.damage = rs.getInt(4);
}
} catch (SQLException e) {
e.printStackTrace();
}
return hero;
}
}