java方法
1、读取文本然后通过正则表达式过滤出关键信息
public class cin_txt {
public static void main(String args[]) {
try {
String pathname = "beijing.txt";
File filename = new File(pathname);
InputStreamReader reader = new InputStreamReader(
new FileInputStream(filename));
BufferedReader br = new BufferedReader(reader);
String line = "";
line = br.readLine();
while (line != null) {
line = br.readLine();
if(line != null) {
String regEx ="data-value='([0-9]+)'([\\s\\S]*?)title='([\\S]+)'";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(line);
while(mat.find()){
Dao.add(mat.group(1),mat.group(3));
}
}
}
br.close();
System.out.println("插入成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、连接mysql,插入关键字
public class Dao {
private static final String USER = "root";
private static final String PASSWD = "123456";
private static final String URL = "jdbc:mysql://localhost:3306/data?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
public static void add(String value,String title) throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, USER, PASSWD);
} catch (SQLException e) {
e.printStackTrace();
}
PreparedStatement pstmt;
String sql = "insert into hotel_brand_test(brand_value,title) Values(?,?) ";
pstmt = (PreparedStatement) connection.prepareStatement(sql);
pstmt.setString(1, value);
pstmt.setString(2, title);
pstmt.executeUpdate();
try {
pstmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}