首页 > 数据库 >将语料文本写入数据库20231104

将语料文本写入数据库20231104

时间:2023-11-04 19:45:17浏览次数:38  
标签:String cid 写入 public sql entity null 语料 20231104

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BaseDao {
    
    public Connection conn = null;
    public PreparedStatement ps = null;
    public ResultSet rs = null;

    public void getConnection() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/languages_material_database?serverTimezone=Asia/Shanghai&&useTimezone=true", "root", "123456");
    }

    public ResultSet executeQuery(String sql, Object[] param) throws Exception {
        this.getConnection();
        ps = conn.prepareStatement(sql);
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                ps.setObject(i + 1, param[i]);
            }
        }
        rs = ps.executeQuery();
        return rs;
    }

    public int executeUpdate(String sql, Object[] param) throws Exception {
        this.getConnection();
        ps = conn.prepareStatement(sql);
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                ps.setObject(i + 1, param[i]);
            }
        }
        int rows = ps.executeUpdate();
        return rows;
    }
}
import java.sql.ResultSet;

public class LanguagesBaseDao extends BaseDao {

    public void saveToDatabase(LanguagesBaseEntity entity) throws Exception {
        String sql = "insert into languages_material_base values("
                + entity.getCid()
                + ","
                + "'" + entity.getTitle().replaceAll("'", "''") +"',"
                + "'" + entity.getContent().replaceAll("'", "''") + "',"
                + "sysdate(),"
                + "sysdate()"
                + ")";
        System.out.println(sql);
        executeUpdate(sql, null);
    }
    
    public Long getMaxCid() throws Exception {
        Long cid = null;
        String sql = "select max(cid) count from languages_material_base";
        ResultSet rs = executeQuery(sql, null); 
        if(rs.next()) {
            cid = rs.getLong("count");
        }
        return cid;
    }
    
}
import java.sql.Date;

public class LanguagesBaseEntity {

    private Long cid;
    
    private String title;
    
    private String content;
    
    private Date createTime;
    
    private Date udpateTime;

    public Long getCid() {
        return cid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUdpateTime() {
        return udpateTime;
    }

    public void setUdpateTime(Date udpateTime) {
        this.udpateTime = udpateTime;
    }
    
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class SaveTxtFileToDatabase {

    public static final String ROOT_PATH = "material/";
    
    public static LanguagesBaseDao languagesBaseDao = new LanguagesBaseDao();
    
    public static void main(String[] args) {
        String root = ROOT_PATH;
        readAllFilesAndSaveToDatabase(root);
    }

    public static void readAllFilesAndSaveToDatabase(String root) {
        try {
            File dir = new File(root);
            if(dir != null && dir.isDirectory()) {
                File[] files = dir.listFiles();
                for(File file : files) {
                    if(file != null && file.isFile()) {
                        Long cid = languagesBaseDao.getMaxCid();
                        if(cid == null) {
                            cid = 1L;
                        } else {
                            cid += 1;
                        }
                        LanguagesBaseEntity entity = readFile(file.getAbsolutePath());
                        entity.setCid(cid);
                        languagesBaseDao.saveToDatabase(entity);
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void readFileAndSaveToDatabase(String filePath) {
        try {
            File file = new File(filePath);
            if(file != null && file.isFile()) {
                Long cid = languagesBaseDao.getMaxCid();
                if(cid == null) {
                cid = 1L;
                } else {
                cid += 1;
                }
                LanguagesBaseEntity entity = readFile(file.getAbsolutePath()); 
                languagesBaseDao.saveToDatabase(entity);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static LanguagesBaseEntity readFile(String filePath) {
        LanguagesBaseEntity entity = new LanguagesBaseEntity();
        try {
            String text = "";
            File file = new File(filePath);
            if(file != null && file.isFile()) {
                entity.setTitle(file.getName());
                FileInputStream fis = new FileInputStream(file.getAbsolutePath());
                BufferedReader br = new BufferedReader(new InputStreamReader(fis, "utf-8"));
                String line = null;
                while((line = br.readLine()) != null) {
                    text += line;
                }
                entity.setContent(text);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return entity;
    }
    
}

 

标签:String,cid,写入,public,sql,entity,null,语料,20231104
From: https://www.cnblogs.com/JimmySeraph/p/17809707.html

相关文章

  • 文件读入和写入
    #include<bits/stdc++.h>#include<fstream>usingnamespacestd;intmain(intargc,char**argv){chardata[100];ofstreama;a.open("hehe.txt");cout<<"请写下你想对你的班主任的话,呵呵";cin>>data;a&......
  • C++使用多线程将数据写入文件
    #include<iostream>#include<vector>#include<thread>#include<fstream>//使用多线程将数据写入文件voidwriteToFile(conststd::vector<std::string>&data,conststd::string&filename){//创建一个文件输出流std::ofstreamfile......
  • 文件名: ?Ciwindows\system32 inetsrconfiglapplicationHost.config 错误:无法写入配
    出现原因:出现这个问题,一般是在程序运行的时候更新程序,导致的.解决方案:MicrosoftWindows[版本6.3.9600](c)2013MicrosoftCorporation。保留所有权利。C:\Users\Administrator>netstopiisadmin/yIISAdminService服务正在停止....IISAdminService服务已成功停......
  • 【MME编写入门】光照模型
    float4x4WorldViewMatrix:WORLDVIEW;float4x4WorldViewProjMatrix:WORLDVIEWPROJECTION;float4x4WorldMatrix:WORLD;//需要用到的矩阵//需要用到光的位置、相机位置float3LightDirection:DIRECTION<stringObject="Light";>;fl......
  • 【MME编写入门】后处理模板
    1float4ClearColor={1,1,1,0};2floatClearDepth=1.0;34floatScript:STANDARDSGLOBAL<5stringScriptOutput="color";6stringScriptClass="scene";7stringScriptOrder="postprocess";8......
  • 用migrate将映像数据写入数据库常见问题01
    django开发时,models.py文件创建正常,makemigrations也正常,但migrate却提示没有需要迁移的数据,查看sqlite数据库也没有建立相应的数据表。相关文件#coding:utf-8#file-name:models.pyfromdjango.dbimportmodelsclassDepartment(models.Model): '''部门表''' title=mode......
  • 实战攻防演练-Linux写入ssh密钥,利用密钥登录
    前言密钥形式登录的原理是利用密钥生成器制作一对密钥,一只公钥和一只私钥。将公钥添加到服务器的某个账户上,然后在客户端利用私钥即可完成认证并登录。这样一来,没有私钥,任何人都无法通过SSH暴力破解你的密码来远程登录到系统。此外,如果将公钥复制到其他账户甚至主机,利用私钥也......
  • 深入研究:Go语言文件写入的性能差异 原创 Go先锋 Go先锋
    深入研究:Go语言文件写入的性能差异原创 Go先锋 Go先锋 2023-10-2811:03 发表于广东收录于合集#Go语言包22个#性能效率1个Go先锋读完需要7分钟速读仅需3分钟  概述在Go语言开发中,文件操作是一个非常常见的任务。在不同的应用场景下,可能会面临选......
  • Python 利用pandas和mysql-connector获取Excel数据写入到MySQL数据库
    如何将Excel数据插入到MySQL数据库中在实际应用中,我们可能需要将Excel表格中的数据导入到MySQL数据库中,以便于进行进一步的数据分析和处理。本文将介绍如何使用Python将Excel表格中的数据插入到MySQL数据库中。导入必要的库首先,我们需要导入pandas库和MySQLConnector/Python库......
  • excel 2DB---excel数据批量写入数据库
    Excel2DB是一款读取excel数据自动入库的软件,支持读取xls、xlsx、csv格式。优点:1、绿色免安装2、无需单独打开软件运行、可直接cmd、rpa、python等第三方软件调用3、存储速度快、大数据读取4、可以添加自定义字段5、一次配置长期有效6、支持sheet分别(并表)存放7、支持自......