(2-24)使用jdbc实现web数据存储(三)

笔记如下

细节描述

  • 使用ResouceBundle读取.properties文件


    2.png
  • 注册驱动:

    查api可知在Driver类中存在静态块,自己注册了驱动,因此只要加载累的字节码,就可以运行Driver类中的静态块.
    4.png
3.png
  • jdbc批处理

    @Test
    public void testStatment() throws Exception {
        
        //create database --- use --create
        
        Connection conn = JdbcUtils.getConnection();
        
        Statement stmt = conn.createStatement();
        
        stmt.addBatch("create database mydbxxx");
        stmt.addBatch("use mydbxxx");
        //stmt.addBatch("create table girls(id int primary key,name varchar(30),age int)");
        stmt.addBatch("insert into girls values(1,'张柏芝',40)");
        stmt.addBatch("insert into girls values(2,'林青霞',25)");
        stmt.addBatch("insert into girls values(3,'刘亦菲',22)");
        
        stmt.addBatch("update girls set age = 31 where name='张柏芝'");
        stmt.executeBatch();
        
    }
    
    @Test
    public void testPreaparedStatment() throws Exception {
        
        Connection conn = JdbcUtils.getConnection();
        
        PreparedStatement stmt = conn.prepareStatement("insert into users values(null,?,?,?)");
        
        for (int i = 0; i <= 200; i++) {
            stmt.setString(1, "chen"+i);
            stmt.setString(2, "mima"+i);
            stmt.setString(3, "小异"+i);
            stmt.addBatch();
            
            //为了提高性能,可以提前执行批处理
//          
//          if(i%50==0) {
//              stmt.executeBatch();
//              stmt.clearBatch();
//          }
            
        }
        
        stmt.executeBatch();
        
    }
  • jdbc多数据处理
public class BigData {
    //存小说
    @Test
    public void test1() throws Exception {
        
        Connection conn = JdbcUtils.getConnection();
        
        PreparedStatement stmt = conn.prepareStatement("insert into mynovel values(null,?)");
        
        //替换占位符
        File file = new File("src/zhansheng.txt");
        Reader reader = new FileReader(file);
        stmt.setCharacterStream(1, reader, (int)file.length());
        
        int count = stmt.executeUpdate();
        
        JdbcUtils.release(null, stmt, conn);
        
        
    }
    
    @Test
    public void test2() throws Exception {
        
        Connection conn = JdbcUtils.getConnection();
        
        PreparedStatement stmt = conn.prepareStatement("select * from mynovel");
        
        ResultSet rs = stmt.executeQuery();
        
        if(rs.next()) {
            rs.getInt("id");
            Reader reader = rs.getCharacterStream("noveltext");
            
            FileWriter writer = new FileWriter("src/backup.txt");
            
            
            int len=0;
    //      byte[] buf = new byte[1024];
            while((len=reader.read())>0) {
                
                writer.write(len);          
                
            }
            reader.close();
            writer.close();
        }
        
        
        JdbcUtils.release(null, stmt, conn);
        
        
    }
    
}

你可能感兴趣的:((2-24)使用jdbc实现web数据存储(三))