zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    C#快速随机按行读取大型文本文件
    OpenReadWithHttps
    fiddler不能监听 localhost和 127.0.0.1的问题 .
    C#放缩、截取、合并图片并生成高质量新图的类
    JS判断只能是数字和小数点
    HTML5 Support In Visual Studio 2010
    GridView 获取列字段的几种途径
    微信朋友圈如何同时分享(图片+文字) Android版
    【Android】 PopupWindow使用小结
    Android 第三方应用接入微信平台(2)
  • 原文地址:https://www.cnblogs.com/sjxbg/p/5857191.html
Copyright © 2011-2022 走看看