package jdbc;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TestJDBC {
public static Hero get(int id) {
Hero hero = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8" +
"&useSSL=false&serverTimezone=UTC",
"root", "123");
Statement s = c.createStatement();
) {
String sql = "select * from hero where id = "+id;
ResultSet rs = s.executeQuery(sql);
if(rs.next()){
hero = new Hero();
String name = rs.getString(2);
float hp = rs.getFloat("hp");
int damage = rs.getInt(4);
hero.name = name;
hero.hp = hp;
hero.damage = damage;
hero.id = id;
}
} catch (SQLException e) {
e.printStackTrace();
}
return hero;
}
public static void add(Hero h) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String sql = "insert into hero values(null,?,?,?)";
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8" +
"&useSSL=false&serverTimezone=UTC",
"root", "123");
PreparedStatement ps = c.prepareStatement(sql);
) {
ps.setString(1,h.name);
ps.setFloat(2,h.hp);
ps.setInt(3,h.damage);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void delete(Hero h) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String sql = "delete from hero where id = ?";
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8" +
"&useSSL=false&serverTimezone=UTC",
"root", "123");
PreparedStatement ps = c.prepareStatement(sql);
) {
ps.setInt(1,h.id);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void update(Hero h) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
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=UTF-8" +
"&useSSL=false&serverTimezone=UTC",
"root", "123");
PreparedStatement ps = c.prepareStatement(sql);
) {
ps.setString(1,h.name);
ps.setFloat(2,h.hp);
ps.setInt(3,h.damage);
ps.setInt(4,h.id);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static List<Hero> list() {
List<Hero> heros = new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8" +
"&useSSL=false&serverTimezone=UTC",
"root", "123");
Statement s = c.createStatement();
) {
String sql = "select * from hero";
ResultSet rs = s.executeQuery(sql);
while(rs.next()){
Hero hero = new Hero();
int id = rs.getInt(1);
String name = rs.getString(2);
float hp = rs.getFloat("hp");
int damage = rs.getInt(4);
hero.name = name;
hero.hp = hp;
hero.damage = damage;
hero.id = id;
heros.add(hero);
}
} catch (SQLException e) {
e.printStackTrace();
}
return heros;
}
public static void main(String[] args){
List<Hero> heros = list();
System.out.println("数据库多少条数据:"+heros.size());
/*Hero h = get(27);
System.out.println(h.name);*/
/*Hero h1 = new Hero();
h1.name = "test hero";
h1.hp = 3123f;
h1.damage = 233;
add(h1);*/
//delete(h);
/*h.name = "new name";
update(h);*/
}
}
java基础——数据库ORM操作原型
最新推荐文章于 2024-07-02 19:45:24 发布