将语料文本写入数据库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; } }
2015年10月-2023年6月 总计7年.
所有总计:7年.
我是唯物唯心二元论的,暂时主学法语.
所有总计:7年.
我是唯物唯心二元论的,暂时主学法语.