栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

将Oracle的BLOB类型字段传输到MySQL的LONGTEXT类型字段

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将Oracle的BLOB类型字段传输到MySQL的LONGTEXT类型字段

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

将Oracle的BLOB类型字段传输到MySQL的LONGTEXT类型字段
  • 一、相关资料
    • 1、POM依赖
    • 2、配置文件
    • 3、代码
    • 4、Oracle数据库
    • 5、MySQL数据库
    • 6、效果图
  • 二、其他
    • 1、Maven依赖爆红解决方案


一、相关资料 1、POM依赖
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.2.0
        

        
        
            com.oracle
            ojdbc6
            11.2.0.5
        

        
            mysql
            mysql-connector-java
            runtime
        
2、配置文件

application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123123

jdbc.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521/orcl
jdbc.username=root
jdbc.password=123123

3、代码

实体类

@TableName("HIS_NHYY")
public class MyTest implements Serializable {
    int id;
    String text;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return "MyTest{" +
                "id=" + id +
                ", text='" + text + ''' +
                '}';
    }
}

Oracle连接工具类

public class TZDBConn {
    private static final String DRIVER = getValue("jdbc.driverClassName");
    private static final String URL = getValue("jdbc.url");
    private static final String USERNAME = getValue("jdbc.username");
    private static final String PASSWORD = getValue("jdbc.password");

    private static Connection connection = null;
    private static PreparedStatement sta = null;
    private static ResultSet rs = null;

    
    private static String getValue(String key) {
        // 资源包绑定
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        return bundle.getString(key);
    }

    
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    
    public Connection getConnection() {
        try {
            connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    
    public ResultSet Query(String sql, Object...obj){
        connection=getConnection();
        try {
            sta=connection.prepareStatement(sql);
            if(obj!=null){
                for(int i=0;i getMyTest(String sql) {
        List myTestList=new ArrayList<>();
        TZDBConn dbconn=new TZDBConn();
        try {
            rs =dbconn.Query(sql, null);
            while(rs.next()){
                MyTest myTest=new MyTest();
//                myTest.setId(rs.getInt("id"));
                Blob blob = rs.getBlob("text");
                String content = new String(blob.getBytes(1, (int)blob.length()),"GBK");
                myTest.setText(content);
                myTestList.add(myTest);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return myTestList;
    }
}

持久层

public interface TextDao extends baseMapper {
}

测试代码

@SpringBootTest
class MySpringboot2ApplicationTests {

    private TZDBConn tzDBConn = new TZDBConn();

    @Autowired
    TextDao textDao;


    @Test
    void oracleTest() {
        String sql="SELECt * FROM HIS_NHYY";
        List myTest = tzDBConn.getMyTest(sql);
        for(MyTest s:myTest){
            textDao.insert(s);
        }

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper
//                .select("id", "name", "age")
                .eq("id", 2);
//                .like("name", "j");

        List userInfos = textDao.selectList(queryWrapper);

        userInfos.forEach(System.out::println);
    }
}

启动类

@SpringBootApplication
@MapperScan("com.example.myspringboot2.dao")
public class MySpringboot2Application {

    public static void main(String[] args) {
        SpringApplication.run(MySpringboot2Application.class, args);
    }

}
4、Oracle数据库

5、MySQL数据库

6、效果图

二、其他 1、Maven依赖爆红解决方案

链接地址
举例
下载Oracle依赖到本地我是放在D:学习Oracleojdbc6.jar

#我在该地址使用cmd
C:Users13113.m2wrapperdistsapache-maven-3.6.3-bin1iopthnavndlasol9gbrbg6bf2apache-maven-3.6.3bin

#注意:不能把jar包放在本地仓库目录里,会报错的
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.5 -Dpackaging=jar -Dfile=D:学习Oracleojdbc6.jar
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号