java基础——数据库ORM操作原型

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);*/
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值