• 使用jdbc存储图片和大文本


    package cn.itcast.i_batch;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import java.util.Arrays;
    
    import org.junit.Test;
    
    import cn.itcast.e_tool.JDBCUtils;
    public class Demo {
        @Test
        //1 使用Statement对象批量执行sql
        public void fun1() throws Exception{
            
            //1 获得连接
            Connection conn = JDBCUtils.getConnection();
            //2 获得Statement
        Statement st =    conn.createStatement();
            //3 添加多条sql语句到st中
        
        st.addBatch("create table t_stu ( id int primary key auto_increment , name varchar(20) )");
        st.addBatch("insert into t_stu values(null,'tom')");
        st.addBatch("insert into t_stu values(null,'jerry')");
        st.addBatch("insert into t_stu values(null,'jack')");
        st.addBatch("insert into t_stu values(null,'rose')");
            //4 执行sql
        int[]  results = st.executeBatch();
        System.out.println(Arrays.toString(results));
            //5关闭资源
            JDBCUtils.close(conn, st, null);
            }
        @Test
        //2 使用PrepareStatement对象批量执行sql
        public void fun2() throws Exception{
            
            //1 获得连接
            Connection conn = JDBCUtils.getConnection();
            //2 书写sql语句
            String sql = "insert into t_stu values(null,?)";
            //3 创建PrepareStatement
            PreparedStatement ps = conn.prepareStatement(sql);
            //4 循环.添加参数
            for(int i=0;i<100;i++){
                ps.setString(1, "用户"+i);
                ps.addBatch();
            }
            //5 批量执行
            int[]  results =ps.executeBatch();
            System.out.println(Arrays.toString(results));
            //5关闭资源
            JDBCUtils.close(conn, ps, null);
            }
    }

     1.使用jdbc存储大文本

    package cn.itcast.g_text;
    
    import java.io.File;
    import java.io.FileReader;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    import org.junit.Test;
    
    import cn.itcast.e_tool.JDBCUtils;
    public class Demo {
        @Test
        //演示向mysql中存放大文本数据
        //存储大文本必须使用PrepareStatement对象
        public void fun1() throws Exception{
            
            //1 获得连接
            Connection conn = JDBCUtils.getConnection();
            //2 书写sql
            String sql = "insert into mytext values(null,?)";
            //3 创建PrepareStatement
            PreparedStatement ps = conn.prepareStatement(sql);
            //4 设置参数
            //参数1:参数的索引
            //参数2:需要保存的文本的流
            //参数3:文件长度
            
            File f = new File("src/text.txt");
            
            FileReader reader = new FileReader(f);
            
            ps.setCharacterStream(1, reader, (int)f.length());
            
            //5 执行sql
            int result = ps.executeUpdate();
            System.out.println(result);
            //6关闭资源
            JDBCUtils.close(conn, ps, null);
            }
        
    }

    2.使用jdbc存储图片

    ackage cn.itcast.h_blob;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    import org.junit.Test;
    
    import cn.itcast.e_tool.JDBCUtils;
    public class Demo {
        @Test
        //演示向mysql中存放图片
        //存储图片必须使用PrepareStatement对象
        public void fun1() throws Exception{
            
            //1 获得连接
            Connection conn = JDBCUtils.getConnection();
            //2 书写sql
            String sql = "insert into myblob values(null,?)";
            //3 创建PrepareStatement
            PreparedStatement ps = conn.prepareStatement(sql);
            //4 设置参数
            //参数1:参数的索引
            //参数2:需要保存的图片的流
            //参数3:图片文件长度
            
            File f = new File("src/wg.PNG");
            
            InputStream  is = new FileInputStream(f);
            
            ps.setBinaryStream(1, is, (int)f.length());
            
            //5 执行sql
            int result = ps.executeUpdate();
            System.out.println(result);
            //6关闭资源
            JDBCUtils.close(conn, ps, null);
            }
        
    }

    3.批量执行sql

  • 相关阅读:
    SP3946 MKTHNUM
    P1948 [USACO08JAN]电话线Telephone Lines(二分答案+最短路)
    CF375D Tree and Queries(dsu on tree)
    P2051 [AHOI2009]中国象棋(动态规划)
    P3810 【模板】三维偏序(陌上花开)(cdq分治)
    P4390 [BOI2007]Mokia 摩基亚(cdq分治)
    P2163 [SHOI2007]园丁的烦恼(cdq分治)
    UVA11270 Tiling Dominoes(轮廓线动态规划)
    P2475 [SCOI2008]斜堆(递归模拟)
    P2617 Dynamic Rankings(带修主席树)
  • 原文地址:https://www.cnblogs.com/sjxbg/p/5857191.html
Copyright © 2020-2023  润新知