Created
July 3, 2017 09:58
-
-
Save hsipeng/d8ed0f6fd4df3be50933724b1631ee34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## DBUtil | |
| ``` | |
| package jdbc2; | |
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.IOException; | |
| import java.sql.Connection; | |
| import java.sql.DriverManager; | |
| import java.sql.SQLException; | |
| import java.util.Properties; | |
| import org.apache.commons.dbcp.BasicDataSource; | |
| /*** | |
| * 该类用于管理数据库连接 | |
| * @author admin | |
| * | |
| */ | |
| public class DBUtil { | |
| //org.apache.commons.dbcp.BasicDataSource; | |
| // 数据库连接池 | |
| private static BasicDataSource ds; | |
| // private static String className; | |
| // private static String url; | |
| // private static String username; | |
| // private static String password; | |
| // | |
| // 初始化 | |
| static{ | |
| //初始化静态属性 | |
| //1. 加载配置文件 config.properties | |
| /** | |
| * java.util.Properties | |
| * 用来读取properties文件并解析其中的每一行内容。 | |
| */ | |
| Properties prop = new Properties(); | |
| try { | |
| prop.load(new FileInputStream("config.properties")); | |
| // className = prop.getProperty("className"); | |
| // password = prop.getProperty("password"); | |
| // url = prop.getProperty("url"); | |
| // username = prop.getProperty("username"); | |
| //// System.out.println(className+password+url+username); | |
| String className = prop.getProperty("classname"); | |
| String password = prop.getProperty("password"); | |
| String url = prop.getProperty("url"); | |
| String username = prop.getProperty("username"); | |
| int maxactive = Integer.parseInt(prop.getProperty("maxactive")); | |
| int maxwait = Integer.parseInt(prop.getProperty("maxwait")); | |
| //初始化连接池 | |
| ds = new BasicDataSource(); | |
| //将JDBC所需要的信息设置到连接至连接池中 | |
| //Class.forName() | |
| ds.setDriverClassName(className); | |
| // Driver.Manager.getConnection | |
| ds.setUrl(url); | |
| ds.setUsername(username); | |
| ds.setPassword(password); | |
| ds.setMaxActive(maxactive); | |
| ds.setMaxWait(maxwait); | |
| } catch (FileNotFoundException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * 获取数据库连接 | |
| * @throws Exception | |
| */ | |
| public static Connection getConnection() throws Exception{ | |
| // Class.forName(className); | |
| // Connection conn = DriverManager.getConnection(url,username,password); | |
| // return conn; | |
| /** | |
| * 连接池提供方法 | |
| * Connection getConnection() | |
| * 该方法可以返回一个连接池中可用连接 | |
| * 这是一个阻塞方法,当连接池中有空闲连接 | |
| * 可以使用时,会立刻返回 | |
| * 当没有时,会进入阻塞 | |
| *阻塞时间由创建连接池时设定 | |
| * | |
| *在等待期间若有空闲连接,立即返回 | |
| *当超过最大等待时间,仍没有可用连接时,该方法抛出异常 | |
| * | |
| */ | |
| return ds.getConnection(); | |
| } | |
| /** | |
| * 关闭数据库连接 | |
| */ | |
| public static void closeConnection(Connection conn){ | |
| try { | |
| /** | |
| * 若该连接市通过连接池获取的,要调用这个连接的close方法 | |
| * 并不是与数据库断开连接,而仅仅是将该连接还给连接池 | |
| * | |
| * | |
| */ | |
| conn.setAutoCommit(true); | |
| conn.close(); | |
| } catch (SQLException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| ``` | |
| ## 批量插入 | |
| ``` | |
| conn = DBUtil.getConnection(); | |
| /*** | |
| * 在一个事务中插入100000条可以减少数据库写入操作次数,提高效率 | |
| */ | |
| conn.setAutoCommit(false); | |
| String sql = "INSERT INTO userinfo " | |
| + "(id, username, password, email, nickname, account)" | |
| + "VALUES " | |
| + "(?,?,'123456',?,?,5000)"; | |
| /*** | |
| * 使用ps可以100000条SQL使用一个执行计划 | |
| * 从而提高SQL执行效率 | |
| * | |
| */ | |
| PreparedStatement ps = conn.prepareStatement(sql); | |
| long start = System.currentTimeMillis(); | |
| for(int i = 100000;i< 200000; i++){ | |
| ps.setInt(1, i); | |
| ps.setString(2, "test"+i); | |
| ps.setString(3, "test"+i+"@qq.com"); | |
| ps.setString(4, "nick"+i); | |
| // ps.executeUpdate(); | |
| //添加到本地缓存 | |
| ps.addBatch(); | |
| } | |
| //执行批操作(将缓存的内容一次性发给数据库执行) | |
| int[] d = ps.executeBatch(); | |
| conn.commit(); | |
| long end = System.currentTimeMillis(); | |
| System.out.println("耗时"+(end-start)+"ms"); | |
| ``` | |
| ## 返回值得字段名 | |
| ``` | |
| conn = DBUtil.getConnection(); | |
| String sql ="INSERT INTO dept " | |
| + "(deptno, dname,loc) " | |
| + "VALUES " | |
| + "(seq_dept_id.NEXTVAL,?,?) "; | |
| /** | |
| * 使用connection带两个参数的方法 | |
| * 生成ps,其中第二个参数是一个字符串数组,数组中每一个字符 | |
| * 内容应当是需要知道插入后该条记录希望返回值得字段名 | |
| */ | |
| PreparedStatement ps = conn.prepareStatement(sql, new String[]{"deptno","dname","loc"}); | |
| ps.setString(1, "php"); | |
| ps.setString(2, "广州"); | |
| int i = ps.executeUpdate(); | |
| if(i > 0){ | |
| System.out.println("插入成功"); | |
| /** | |
| * 获取刚插入的这个部门中deptno字段 | |
| */ | |
| ResultSet rs = ps.getGeneratedKeys(); | |
| rs.next(); | |
| int deptno = rs.getInt(1); | |
| String dname = rs.getString(2); | |
| String loc = rs.getString(3); | |
| System.out.println("插入的部门id为:"+deptno); | |
| System.out.println("插入的部门名称为:"+dname); | |
| System.out.println("插入的部门地区为:"+loc); | |
| } | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment