其他分享
首页 > 其他分享> > 简单jdbc实现

简单jdbc实现

作者:互联网

1、基本jdbc的代码实现

/*
   基本jdbc操作
 */

import java.sql.*;

public class JdbcDemo {
    public static final String URL = "jdbc:mysql://localhost:3306/test";
    public static final String USER = "root";
    public static final String PASSWORD = "root";
    public static final String SELECT_SQL = "SELECT * FROM t_order";
    public static final String UPDATE_SQL = "INSERT into t_role VALUES(3,'jiwei','kaishi','')";

    public static void main(String[] args) {
        // 1、导入MySQL或者其他数据库驱动包
        Connection con = null;
        ResultSet rs = null;
        Statement statement = null;

        try {
            // 2、加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            // 3、获取数据库连接
           con = DriverManager.getConnection(URL, USER, PASSWORD);
                //自动提交事务改为手动提交
                con.setAutoCommit(false);
            // 4、获取执行sql的小火车-statement对象
           statement = con.createStatement();
                // 预编译的小火车:提前装载sql
                //PreparedStatement pst = con.prepareStatement(SELECT_SQL);
                //pst.executeQuery();
            // 5、执行sql,拿到结果集
            rs = statement.executeQuery(SELECT_SQL);
            //int i = statement.executeUpdate(UPDATE_SQL);
            // 6、遍历结果集,获取数据
            while (rs.next()) {
                System.out.println(rs.getString(1));
                System.out.println(rs.getString(2));
            }
            //提交事务  没问题,提交吧
            con.commit();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            //数据回滚
            if (con !=null){
                try {
                    con.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }finally {
            // 7、 释放资源,后调用的先关闭;关闭之前先判断对象是否存在
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

2、内置Api一览

连接对象——Connection

客户端与数据库的操作都是基于Connection进行交互的。

// 创建运送sql的小火车~
Statement statement = Connection.createStatement();
PreparedStatement pst = con.prepareStatement(SELECT_SQL);

// 事务
con.setAutoCommit(false);
con.commit();// 提交
con.rollback();	// 回滚

小火车——Statement

将sql语句运输到数据库。

// 查询
statement.executeQuery(SELECT_SQL)
// 增删改
statement.executeUpdate(UPDATE_SQL)
// 执行
statement.execute(SQL)
// 批处理(导入)
statement.addBatch(); 
statement.executeBatch();
statement.clearBatch();

结果集——ResultSet

ResultSet维护了一个数据行的游标,调用ResultSet.next(),可以让游标指向具体行,获取数据。

(ps:工作时验证,调用ResultSet.next(),结果集中才有数据,而非执行sql时)

// 获取指定字段/行的数据
rs.getString(1)//第一个字段
rs.next() //对结果集滚动查看
// 遍历结果集
if 1条数据一下
while 循环遍历多条

3、JDBC的细节

PreparedStatement的优势:

​ 同样是小火车,比Statement的绿皮火车强大多了!

1、 Statement编译SQL时,比较古板,变量必须要用分隔符’++‘隔开,而PreparedStatement可以使用占位符,写起来简单多了;  最关键能够防止SQL注入问题!!!
2、 Statement需要频繁编译SQL,而PreparedStatement可以进行预编译,将SQL存储起来,能极大提升效率;

标签:jdbc,rs,实现,throwables,简单,statement,SQL,public,con
来源: https://blog.csdn.net/weixin_46512598/article/details/118633138