什么是JDBC:java连接数据库的规范(标准),可以使用java语言连接数据库完成CRUD操作
Class.forName("com.mysql.jdbc.Driver");
Conncetion conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8","root","1234");
Statement statement = conn.createStatement();
//DML语句返回int型 String sql = "delete ....."; int result = statement.executeUpdate(sql); ----------------------------------------------------------- //DQL语句返回ResultSet结果集 ResultSet rs = statement.executeQuery(sql);
//DML语句 if(result == 1 ){ System.out.println("Success!!") } //DQL语句 while(rs.next() ){ String job_id = resultSet.getString( "列名" 或者 列的序号 1); int min_salary = resultSet.getInt( "列名" 或者 列的序号 2); ... }
rs.close();//处理结果集时 statement.close(); conn.close();
PreparedStatement pstmt = conn.prepareStatement( "select * from user where username =? and password =?" ); pstmt.setString(1,username); pstmt.setString(2,password); int result = pstmt.executeUpdate();//DML语句 ResultSet rs = pstmt.executeQuery();//DQL语句 while(rs.next()){ int pid = rs.getInt("列名"); String name = rs.getString("name"); ... }
/** * 日期转换 * 字符串转UtilDate * UtilDate转SqlDate * UtilDate转成字符串 */ public class DateUtils { private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //字符串转Util public static java.util.Date strToUtilDate(String str){ try { return simpleDateFormat.parse(str); } catch (ParseException e) { e.printStackTrace(); } return null; } //Util转sql public static java.sql.Date utilToSql(java.util.Date date){ return new java.sql.Date(date.getTime()); } //Util转字符串 public static String toStr(java.util.Date bornDate){ return simpleDateFormat.format(bornDate); } }
#连接设置 driverClassName=com.mysql.jdbc.Driver url=jdbc:mydql://localhost:3306/school username=root password=root #初始化连接 initialSize=10 #最大连接数量 maxActive=50 #最小空闲连接 minIdle=5 #超时等待时间以毫秒为单位 maxWait=5000
在JDBC中,获得Connection对象开始事物–提交或回滚–关闭连接。其事物策略是
- conn.setAutoCommit(false);
- conn.commit();
- conn.rollback();
public class DbUtils { private static DruidDataSource ds; ThreadLocal<Connection> t1 = new ThreadLocal<Connection>(); static{ Properties properties=new Properties(); try { properties.load(DbUtils.class.getResourceAsStream("database.properties")); ds = (DruidDataSource)DruidDataSourceFactory.createDataSource(properties); } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection(){ Connection conn=t1.get(); if (conn == null){ conn = ds.getConnection(); t1.set(conn); } return conn; } public static void closeAll(ResultSet rs, Statement st,Connection conn){ try{ if (rs != null){ rs.close(); } if (st != null){ st.close(); } if (conn != null){ conn.close(); t1.remove(); } }catch (SQLException e){ e.printStackTrace(); } } //开启事务 public static void begin(){ Connection conn=getConnection(); try{ conn.setAutoCommit(false); }catch (SQLException e){ e.printStackTrace(); } } //提交事务 public static void commit(){ Connection conn=getConnection(); try{ conn.commit(); }catch (SQLException e){ e.printStackTrace(); }finally { DbUtils.closeAll(null,null,conn); } } //回滚事务 public static void rollback(){ Connection conn=getConnection(); try { conn.rollback(); } catch (SQLException e) { e.printStackTrace(); }finally { DbUtils.closeAll(null,null,conn); } } }
public class DaoUtils { public int commonsUpdate(String sql,Object...args){ Connection conn=null; PreparedStatement pstmt=null; try { conn=DbUtils.getConnection(); pstmt=conn.prepareStatement(sql); for (int i=0;i< args.length;i++){ pstmt.setObject(i+1,args[i]); } return pstmt.executeUpdate(); }catch (SQLException e){ e.printStackTrace(); }finally { DbUtils.closeAll(null,pstmt,conn); } return 0; } public List<T> commonsSelect(String sql, RowMapper<t> rowMapper,Object...args){ List<T> elements=new ArrayList<T>(); Connection conn=null; PreparedStatement pstmt =null; ResultSet rs=null; try { conn=DbUtils.getConnection(); pstmt = conn.prepareStatement(sql); if (args != null){ for (int i=0;i< args.length;i++){ pstmt.setObject(i+1,args[i]); } } rs=pstmt.executeQuery(); while (rs.next()){ T t=rowMapper.getRow(rs); elements.add(t); } } catch (SQLException e) { e.printStackTrace(); }finally { DbUtils.closeAll(rs,pstmt,conn); } return elements; } }