本文共 2936 字,大约阅读时间需要 9 分钟。
????????????????????????????????????????
JDBC?SUN??????????????Java????????API???????????????????????????????SQL???
??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; }} ??????????????????????????????????????
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????????????????????SQL?????
PreparedStatement???SQL????????SQL??????????
???????????????????????????????????????????
????conn.setAutoCommit(false)?????????DML??????????????
???????????????????????????????
?????????C3P0?DBCP?Druid????????????
??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/