MySql+Java中JDBC增删改查

在characher包下,创建Hero类

package characher;
public class Hero {
    public int id;
    public String name;
    public float hp;
    public int damage;
}

在jdbcr包下,创建TestJDBC类

package jdbc;
import characher.Hero;
import java.sql.*;
public class TestJDBC {
    //mysql驱动包名
    private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
    //数据库连接地址
    // ?useSSL=false不校验ssl证书
    // jdbc:mysql://这是协议,localhost:3306这是地址和端口,test这是数据库
    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false";
    //用户名,更换成你自己的用户名,此处为root用户
    private static final String USER_NAME = "root";
    //密码,更换成你自己设定的密码,此处为:123456
    private static final String PASSWORD = "123456";
    //查询
    public static Hero getHero(int id) {
        Hero hero = null;
        try {
            //初始化驱动
            Class.forName(DRIVER_NAME);
            Connection con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            //禁止自动提交sql
            con.setAutoCommit(false);
            String sql = "select * from hero where id = ?";
            PreparedStatement gather = con.prepareStatement(sql);
            gather.setInt(1, id);
            ResultSet res = gather.executeQuery();
            while (res.next()) {
                hero = new Hero();
                String name = res.getString(2);
                float hp = res.getFloat(3);
                int damage = res.getInt(4);
                hero.name = name;
                hero.hp = hp;
                hero.damage = damage;
            }
            //主动动提交sql
            con.commit();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return hero;
    }
    //添加
    public static void addHero(int id,String name,float hp, int damage){
        Hero hero = null;
        try{
            hero = new Hero();
            hero.id = id;
            hero.name = name;
            hero.hp = hp;
            hero.damage = damage;
            Class.forName(DRIVER_NAME);
            Connection con = DriverManager.getConnection(URL,USER_NAME,PASSWORD);
            con.setAutoCommit(false);
            String sql="insert into hero values (?, ?, ?, ?);";
            PreparedStatement res = con.prepareStatement(sql);
            res.setInt(1,hero.id);
            res.setString(2,hero.name);
            res.setFloat(3,hero.hp);
            res.setInt(4,hero.damage);
            res.execute();
            con.commit();

        }catch (Exception e){
            e.printStackTrace();
        }

    }
    //删除
    public static void delHero(int id){
        try{
            Class.forName(DRIVER_NAME);
            Connection con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            con.setAutoCommit(false);
            String sql = "delete from hero where id = ?;";
            PreparedStatement res = con.prepareStatement(sql);
            res.setInt(1,id);
            res.execute();
            con.commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //改
    public static void updataHero(int id,String name,float hp, int damage){
        try {
            Class.forName(DRIVER_NAME);
            Connection con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            con.setAutoCommit(false);
            String sql = "update hero set name = ? ,hp = ? ,damage = ? where id = ?;";
            PreparedStatement res = con.prepareStatement(sql);
            res.setString(1,name);
            res.setFloat(2,hp);
            res.setInt(3,damage);
            res.setInt(4,id);
            res.execute();
            con.commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
//        Hero h = getHero(23);
//        System.out.println(h.name + "," + h.hp + "," + h.damage);
//        添加100条
//        for(int i = 1;i<=100;i++){
//            addHero(i,"霖"+i,12.0f,111);
//        }
//        删除100条
//        for(int i = 1;i<=100;i++){
//            delHero(i);
//        };
        updataHero(1,"霖儿",123.0f,100);
    }
}

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页