JAVA把Mysql数据库中的表的数据输出至文本文档中
public void sync(String sheet, String column, String path) throws ClassNotFoundException, SQLException, IOException {
Class.forName("com.mysql.jdbc.Driver");
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/searchengine?characterEncoding=utf8&useSSL=false", user, password);
Statement statement = conn.createStatement();
String sql = "SELECT " + column + " FROM " + sheet;
ResultSet resultSet = statement.executeQuery(sql);
File file = new File(path);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file));
while(resultSet.next()){
//同义词库单独处理
if(sheet.equals("synonym")){
for (int i = 1; i < 5; i++) {
if(resultSet.getString(i) != null)
outputStreamWriter.write(resultSet.getString(i));
if(resultSet.getString(i+1) != null)
outputStreamWriter.write(",");
}
if(resultSet.getString(5) != null)
outputStreamWriter.write(resultSet.getString(5));
outputStreamWriter.write("\r\n");
}
//处理扩展词和停用词
else
outputStreamWriter.write(resultSet.getString(1) + "\r\n");
}
outputStreamWriter.flush();
outputStreamWriter.close();
resultSet.close();
statement.close();
conn.close();
System.out.println("同步" + sheet + "词库");
}