基于JDBC的增删改查

来源:互联网 发布:好看的中国网络剧推荐 编辑:程序博客网 时间:2024/06/01 09:27

学习记录

jdbc基本概念:

1、java中数据库存取技术主要有:

  • jdbc直接访问数据库
  • JDO技术(?);
  • 第三方工具,Hibernate,ibatis等。

2、jdbc接口(API)包括两个层次:

  1. Java API,面向应用,供开发人员(连接数据库,执行sql语句,获得结果);
  2. Java Driver API,面向数据库,供数据库开发商。

3、Driver接口:

  • Java.sql.Driver是所有JDBC驱动程序需要实现的接口。提供给数据库厂商使用。
  • 程序中不需要直接访问Driver的实现类,由java.sql.DriverManager去调用Driver实现。

Driver接口导入

  • Mysql Driver下载路径:https://dev.mysql.com/downloads/connector/j/
  • 版本:mysql-connector-java-5.1.42

具体步骤:

  1. 先导入jar包:mysql-connector-java-5.1.42-bin.jar;
  2. 工程目录下新建lib文件夹;
  3. 选中jar包,右键Build Path,执行Add to Build Path,加到类路径下。

首先,获取数据库连接:

private static Connection getConn() {    String driver = "com.mysql.jdbc.Driver";    String url = "jdbc:mysql://localhost:3306/samp_db";    String username = "root";    String password = "";    Connection conn = null;    try {        //将驱动类的class文件装载到内存中,并且形成一个描述此驱动类结构的Class类实例,并且初始化此驱动类,这样jvm就可以使用它了,这就是Class.forName()方法的含义。                Class.forName(driver); //classLoader,加载对应驱动        conn = (Connection) DriverManager.getConnection(url, username, password);    } catch (ClassNotFoundException e) {        e.printStackTrace();    } catch (SQLException e) {        e.printStackTrace();    }    return conn;}

增加操作:

private static int insert(Student student) {    Connection conn = getConn();    int i = 0;    String sql = "insert into students (Name,Sex,Age) values(?,?,?)";    PreparedStatement pstmt;    try {        pstmt = (PreparedStatement) conn.prepareStatement(sql);        //123对应sql语句中的?        pstmt.setString(1, student.getName());        pstmt.setString(2, student.getSex());        pstmt.setString(3, student.getAge());        i = pstmt.executeUpdate();        pstmt.close();        conn.close();    } catch (SQLException e) {        e.printStackTrace();    }    return i;}

删除操作:

private static int delete(String name) {    Connection conn = getConn();    int i = 0;    String sql = "delete from students where Name='" + name + "'";    PreparedStatement pstmt;    try {        pstmt = (PreparedStatement) conn.prepareStatement(sql);        i = pstmt.executeUpdate();        System.out.println("resutl: " + i);        pstmt.close();        conn.close();    } catch (SQLException e) {        e.printStackTrace();    }    return i;}

修改操作:

private static int update(Student student) {    Connection conn = getConn();    int i = 0;    String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";    PreparedStatement pstmt;    try {        pstmt = (PreparedStatement) conn.prepareStatement(sql);        i = pstmt.executeUpdate();        System.out.println("resutl: " + i);        pstmt.close();        conn.close();    } catch (SQLException e) {        e.printStackTrace();    }    return i;}

查询操作:

private static Integer getAll() {    Connection conn = getConn();    String sql = "select * from students";    PreparedStatement pstmt;    try {        pstmt = (PreparedStatement)conn.prepareStatement(sql);        ResultSet rs = pstmt.executeQuery();        //得到返回数据的列数        int col = rs.getMetaData().getColumnCount();        System.out.println("============================");        while (rs.next()) {            for (int i = 1; i <= col; i++) {                System.out.print(rs.getString(i) + "\t");             }            System.out.println("");        }            System.out.println("============================");    } catch (SQLException e) {        e.printStackTrace();    }    return null;}

总结

通用流程:

在上述对数据库进行增删改查的过程中,可以发现其共性部分,即通用的流程:
  (1)创建Connection对象、SQL查询命令字符串;
  (2)对Connection对象传入SQL查询命令,获得PreparedStatement对象;
  (3)对PreparedStatement对象执行executeUpdate()executeQurey()获得结果;
  (4)先后关闭PreparedStatement对象和Connection对象。
  可见,使用JDBC时,最常打交道的是Connection、PreparedStatement这两个类,以及select中的ResultSet类。

关于url的写法:
这里写图片描述

以上

原创粉丝点击