博客
关于我
Day112.JDBC技术复习 -JDBC技术
阅读量:328 次
发布时间:2019-03-04

本文共 2936 字,大约阅读时间需要 9 分钟。

JDBC??

JDBC??

??????

????????????????????????????????????????

JDBC???

JDBC?SUN??????????????Java????????API???????????????????????????????SQL???

JDBC???

  • ??????????????JDBC???????????????
  • ?????????????????????JDBC???????????
  • ?????

    ???????

    ??JDBCUtils???????????????????????????????????

    public class JDBCUtil {    public static Connection getConncetion() throws Exception {        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");        Properties pros = new Properties();        pros.load(is);        String user = pros.getProperty("user");        String password = pros.getProperty("password");        String url = pros.getProperty("url");        String driver = pros.getProperty("driver");        Class.forName(driver);        Connection conn = DriverManager.getConnection(url, user, password);        return conn;    }}

    JDBCUtils???

    ????

    ??????????????????????????????????????

    public class JDBCUtil {    public static void closeResource(Connection conn, PreparedStatement ps) {        try {            if (conn != null) {                conn.close();            }        } catch (SQLException e) {            e.printStackTrace();        }        try {            if (ps != null) {                ps.close();            }        } catch (SQLException e) {            e.printStackTrace();        }    }    public static void closeResource(Connection conn, PreparedStatement ps, ResultSet resultSet) {        try {            if (conn != null) {                conn.close();            }        } catch (SQLException e) {            e.printStackTrace();        }        try {            if (ps != null) {                ps.close();            }        } catch (SQLException e) {            e.printStackTrace();        }        try {            if (resultSet != null) {                resultSet.close();            }        } catch (SQLException e) {            e.printStackTrace();        }    }}

    Statement??

    CRUD??

    Statement????????????????????SQL?????

    PreparedStatement??Statement

    PreparedStatement???SQL????????SQL??????????

    ?????

    ????

    ???????????????????????????????????????????

    ????

    ????conn.setAutoCommit(false)?????????DML??????????????

    ??????

    ??????

    ???????????????????????????????

    ?????

    ?????????C3P0?DBCP?Druid????????????

    DBUtils???

    ????

    ??DBUtils???QueryRunner??CRUD???????????

    @Testpublic void testInsert() {    Connection conn = null;    try {        QueryRunner runner = new QueryRunner();        conn = JDBCUtil.getConnection();        String sql = "insert into customers(name,email,birth)values('??', 'achang@qq.com', '1999-03-09')";        int insertCount = runner.update(conn, sql);        System.out.println("??? " + insertCount + " ???");    } catch (Exception e) {        e.printStackTrace();    } finally {        JDBCUtil.closeResource(conn, null);    }}

    ??

    JDBC??????????????SQL????????????PreparedStatement???Statement?????????????????????????????DBUtils??????????????

    转载地址:http://rboq.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现二进制补码算法(附完整源码)
    查看>>
    Objective-C实现交易密码算法(附完整源码)
    查看>>
    Objective-C实现低通滤波器(附完整源码)
    查看>>
    Objective-C实现使用管道重定向进程输入输出(附完整源码)
    查看>>
    Objective-C实现关系矩阵A和B的乘积(附完整源码)
    查看>>
    Objective-C实现内存映射文件(附完整源码)
    查看>>
    Objective-C实现内存泄露检查(附完整源码)
    查看>>
    Objective-C实现内格尔·施雷肯伯格算法(附完整源码)
    查看>>
    Objective-C实现分块查找算法(附完整源码)
    查看>>
    Objective-C实现分水岭算法(附完整源码)
    查看>>
    Objective-C实现分解质因数(附完整源码)
    查看>>
    Objective-C实现切换数字的符号switchSign算法(附完整源码)
    查看>>
    Objective-C实现创建多级目录(附完整源码)
    查看>>
    Objective-C实现删除重复的字母字符算法(附完整源码)
    查看>>
    Objective-C实现判断32位的数字是否为正数isPositive算法(附完整源码)
    查看>>
    Objective-C实现十进制转N进制算法(附完整源码)
    查看>>
    Objective-C实现华氏温度转摄氏温度(附完整源码)
    查看>>
    Objective-C实现单例模式(附完整源码)
    查看>>
    Objective-C实现单向链表的反转(附完整源码)
    查看>>
    Objective-C实现单字母密码算法(附完整源码)
    查看>>