package jdbc;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import file.Hero;
public class TestJDBC{
public static void main(String[] args){
TestJDBC a = new TestJDBC();
// System.out.println(a.getTotal());
Hero hero = new Hero();
hero.name = "A";
hero.hp = 20;
hero.damage = 15;
// a.add(hero);
// Hero hero = a.get(55);
// System.out.println(hero.damage);
a.update(hero,49);
// a.delete(43);
// System.out.println(a.get(44).name);
// List<Hero> he = new ArrayList<>();
// he = a.list(35,45);
// System.out.println(he);
}
public int getTotal(){
// 查询数据库数据量
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "select count(*) from hero";
int total = 0;
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
PreparedStatement ps = c.prepareStatement(sql) ){
ResultSet rs = ps.executeQuery();
if(rs.next()){
total = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return total;
}
public void add(Hero hero){
String name = hero.name;
float hp = hero.hp;
int damage = hero.damage;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "insert into hero values(null,?,?,?)";
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
PreparedStatement ps = c.prepareStatement(sql) ){
ps.setString(1, name);
ps.setFloat(2, hp);
ps.setInt(3, damage);
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(Hero h,int id){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "update hero set name=? , hp=? , damage=? where id=?";
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
PreparedStatement ps = c.prepareStatement(sql) ){
ps.setString(1, h.name);
ps.setFloat(2, h.hp);
ps.setInt(3, h.damage);
ps.setInt(4, id);
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete(int id){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "delete from hero where id=?";
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
PreparedStatement ps = c.prepareStatement(sql) ){
ps.setInt(1, id);
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Hero get(int id){
Hero hero = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "select * from hero where id=?";
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
PreparedStatement ps = c.prepareStatement(sql) ){
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next()){
hero = new Hero();
hero.name = rs.getString("name");
hero.hp = rs.getFloat("hp");
hero.damage = rs.getInt("damage");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hero;
}
public List<Hero> list(int start,int end){
List<Hero> heros = new ArrayList<>();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "select * from hero order by id desc limit ?,?";
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF8","root","admin");
PreparedStatement ps = c.prepareStatement(sql) ){
ps.setInt(1, start);
ps.setInt(2, end);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Hero hero = new Hero();
hero.name = rs.getString("name");
hero.hp = rs.getFloat("hp");
hero.damage = rs.getInt("damage");
heros.add(hero);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return heros;
}
}
Java 实现对象插入、删除、更新、集合返回、数据库中数据总数获取
最新推荐文章于 2022-06-10 15:09:59 发布