JDBC处理CLOB/BLOB类型

CLOB存取一个文本

import java.io.*;
import java.sql.*;

public class Main {//com.mysql.cj.jdbc.Driver
    private static final String URL="jdbc:mysql://localhost:3306/student?serverTimezone=UTC";
    private static final String USERNAME="yucan";
    private static final String PASSWORD="1234";

    public static void ClobDemo(){
        Connection connection=null;
        PreparedStatement pstatement=null;
        try{
            //a.导入驱动。加载具体的驱动类
            Class.forName( "com.mysql.cj.jdbc.Driver" );
            //b.建立连接
            connection=DriverManager.getConnection( URL,USERNAME,PASSWORD );
            String sql="insert into mynovel values(?,?)";
            pstatement=connection.prepareStatement( sql );
            pstatement.setInt( 1,1 );
            File file=new File("C:\\Users\\22873\\Desktop\\软件测试\\all.txt");
            InputStream inputStream=new FileInputStream( file );
            Reader reader=new InputStreamReader( inputStream,"UTF-8" );      //转换流可以设置编码格式
            pstatement.setCharacterStream( 2,reader,(int)file.length() );
            int count=pstatement.executeUpdate();//发送sql,执行
            if(count>0){
                System.out.println( "执行成功!" );
            }
            reader.close();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                if(pstatement!=null)
                    pstatement.close();
                if(connection!=null)
                    connection.close();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void ClobRead(){
        Connection connection=null;
        PreparedStatement pstatement=null;
        ResultSet resultSet=null;
        try{
            //a.导入驱动。加载具体的驱动类
            Class.forName( "com.mysql.cj.jdbc.Driver" );
            //b.建立连接
            connection=DriverManager.getConnection( URL,USERNAME,PASSWORD );
            String sql="select NOVEL from mynovel where id= ?";
            pstatement=connection.prepareStatement( sql );
            pstatement.setInt( 1,1 );
            resultSet=pstatement.executeQuery();
            if(resultSet.next()){
                Reader reader=resultSet.getCharacterStream( "NOVEL" );
                Writer writer=new FileWriter( "src\\小说.txt" );
                char[] chs=new char[100];
                int len=-1;
                while((len=reader.read(chs))!=-1){
                    writer.write( chs,0,len );
                }
                writer.close();
                reader.close();
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                if(pstatement!=null)
                    pstatement.close();
                if(connection!=null)
                    connection.close();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ClobDemo();
       // ClobRead();
    }
}

BLOB存取一个文本

import java.io.*;
import java.sql.*;

public class Main {//com.mysql.cj.jdbc.Driver
    private static final String URL="jdbc:mysql://localhost:3306/student?serverTimezone=UTC";
    private static final String USERNAME="yucan";
    private static final String PASSWORD="1234";

    public static void BlobDemo(){
        Connection connection=null;
        PreparedStatement pstatement=null;
        try{
            //a.导入驱动。加载具体的驱动类
            Class.forName( "com.mysql.cj.jdbc.Driver" );
            //b.建立连接
            connection=DriverManager.getConnection( URL,USERNAME,PASSWORD );
            String sql="insert into mymusic values(?,?)";
            pstatement=connection.prepareStatement( sql );
            pstatement.setInt( 1,1 );
            File file=new File("C:\\Users\\22873\\Desktop\\软件测试\\a.mp3");
            InputStream inputStream=new FileInputStream( file );
            pstatement.setBinaryStream(2,inputStream,(int)file.length());
            int count=pstatement.executeUpdate();//发送sql,执行
            if(count>0){
                System.out.println( "执行成功!" );
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                if(pstatement!=null)
                    pstatement.close();
                if(connection!=null)
                    connection.close();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void BlobRead(){
        Connection connection=null;
        PreparedStatement pstatement=null;
        ResultSet resultSet=null;
        try{
            //a.导入驱动。加载具体的驱动类
            Class.forName( "com.mysql.cj.jdbc.Driver" );
            //b.建立连接
            connection=DriverManager.getConnection( URL,USERNAME,PASSWORD );
            String sql="select music from mymusic where id= ?";
            pstatement=connection.prepareStatement( sql );
            pstatement.setInt( 1,1 );
            resultSet=pstatement.executeQuery();
            if(resultSet.next()){
                InputStream inputStream=resultSet.getBinaryStream("music");
                OutputStream outputStream=new FileOutputStream( "src\\music.mp3" );

                byte[] chs=new byte[100];
                int len=-1;
                while((len=inputStream.read(chs))!=-1){
                    outputStream.write( chs,0,len );
                }
                outputStream.close();
                inputStream.close();
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                if(pstatement!=null)
                    pstatement.close();
                if(connection!=null)
                    connection.close();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        BlobDemo();
       // BlobRead();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值