jdbc03.使用Statement 接口实现增/删/改操作

快捷键:Ctrl+Shift+f,将长句自动分段
- Statement 接口
作用:用于执行静态SQL 语句并返回它所生成结果的对象。
int executeUpdate(String sql) 执行给定SQL 语句,该语句可能为INSERT、UPDATE 或DELETE 语句,或者不返回任何内容的SQL 语句(如SQL DDL 语句)。
void close() 立即释放此Statement 对象的数据库和JDBC 资源,而不是等待该对象自动关闭时发生此操作
实例化Statement 接口 使用接口 Statement页面的另外参见Connection.createStatement(), 如下图jdbc03.使用Statement 接口实现增/删/改操作_第1张图片

public class Demo1 {

    public static void main(String[] args) throws Exception {
        String sql = "insert into t_student values(null, 'bob', '23', '男','六年级')";//此处用''t_student 为db_book里的表,null表示插在后面

        DbUtil dbUtil = new DbUtil();
        Connection con = dbUtil.getCon();//获取数据库连接
        Statement stat = con.createStatement();
        int result = stat.executeUpdate(sql);//返回更新的数据条数
        stat.close();//关闭Statement
        con.close();//关闭连接//先打开的后关闭
        System.out.println("操作了:"+ result +"条数据");


    }
}


  • 添加数据
/**
 * 用于封装数据库连接的一些方法
 * @author Gavin
 *
 */
public class DbUtil {

    private static String jdbcName = "com.mysql.jdbc.Driver";//驱动名字,不能随便改
    private static String dbUrl = "jdbc:mysql://localhost:3306/db_book";//mysql的数据库地址
    public static String dbUserName = "root";//数据库用户名
    public static String dbPassWord = "123456";//密码
    /**
     * 获取数据库连接
     * @return
     * @throws Exception
     */
    public Connection getCon() throws Exception {
        Class.forName(jdbcName);
        Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassWord);
        return con;
    }
    /**
     * 关闭连接
     * @param con
     * @throws Exception
     */
    public void closeCon(Statement stat,Connection con) throws Exception {
        if(stat!=null){
            stat.close();
            if (con!=null){
                con.close();
            }
        }   

    }
}

输出:操作了1条数据
数据库如下图jdbc03.使用Statement 接口实现增/删/改操作_第2张图片
数据类型如下
jdbc03.使用Statement 接口实现增/删/改操作_第3张图片


封装上面添加数据的方法,功能一样

/**
 * 用来将student对象封装
 * @author Gavin
 *
 */
public class Student {

    //alt+S生成构造函数
    public Student(String stuName, double age, String sex, String gradeName) {
        super();
        this.stuName = stuName;
        this.age = age;
        this.sex = sex;
        this.gradeName = gradeName;
    }
    private  String stuName; 
    private double age;
    private String sex;
    private String gradeName;
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getGradeName() {
        return gradeName;
    }
    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }   
}

    public static DbUtil dbUtil = new DbUtil();

    private static int addStudent2(Student student) throws Exception {
        String sql = "insert into t_student values(null, '"+student.getStuName()+"', '"+student.getAge()+"', '"+student.getSex()+"', '"+student.getGradeName()+"')"; 
        Connection con = dbUtil.getCon();
        Statement stat = con.createStatement();
        int result = stat.executeUpdate(sql);
        dbUtil.closeCon(stat, con);
        if (result == 1){
            System.out.println("添加成功");
        }
        return result;

    }

    public static void main(String[] args) throws Exception {
        /*addStudent("张三",   12, "男", "大二");*/
        Student student = new Student("张三",  12, "男", "大san");
        addStudent2(student);
    }
}


  • 更新数据
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.jdbc03.model.Student;
import com.jdbc03.util.DbUtil;

public class Demo3 {

    private static DbUtil dbUtil = new DbUtil();

    private static int updateStudent(Student student) throws Exception {
        String sql = "update t_student set stuName = '" + student.getStuName()
                + "', age = " + student.getAge() + ", sex = '"
                + student.getSex() + "', gradeName = '"
                + student.getGradeName() + "' where id = " + student.getId();
        //注意此处写法stuName,age,gradeName等为数据库表中的项目,Ctrl+Shift+F:将长句子分段
        Connection con = dbUtil.getCon();
        Statement stat = con.createStatement();
        int result = stat.executeUpdate(sql);
        dbUtil.closeCon(stat, con);

        return result;
    }

    public static void main(String[] args) throws Exception {

        Student student = new Student(6, "we", 22, "女", "233");
        int result = updateStudent(student);
        if(result == 1){
            System.out.println("成功");
        }

    }
}
/**
 * 用来将student对象封装
 * @author Gavin
 *
 */
public class Student {
    private int id;
    private  String stuName; 
    private double age;
    private String sex;
    private String gradeName;
    //alt+S生成构造函数
    public Student(String stuName, double age, String sex, String gradeName) {
        super();
        this.stuName = stuName;
        this.age = age;
        this.sex = sex;
        this.gradeName = gradeName;
    }

//由于更改需要用到id,所以需要重载构造函数 alt+S
    public Student(int id, String stuName, double age, String sex,
            String gradeName) {
        super();
        this.id = id;
        this.stuName = stuName;
        this.age = age;
        this.sex = sex;
        this.gradeName = gradeName;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getGradeName() {
        return gradeName;
    }
    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }

}


  • 删除
import java.sql.Connection;
import java.sql.Statement;

import com.jdbc03.model.Student;
import com.jdbc03.util.DbUtil;

public class Demo4 {

    private static DbUtil dbUtil = new DbUtil();    
    private static int delectStu(int id) throws Exception {
        Connection con = dbUtil.getCon();
        String sql = "delete from t_student where id = " +id;//此处用''
        Statement stat = con.createStatement();//创建Statement
        int result = stat.executeUpdate(sql);
        dbUtil.closeCon(stat, con);
        return result;
    }

    public static void main(String[] args) throws Exception {
        int result = delectStu(3);
        if(result == 1){
            System.out.println("删除成功");
        }       
    }
}

你可能感兴趣的:(JAVA)