|
@@ -0,0 +1,96 @@
|
|
|
+package com.diagbot.util;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+
|
|
|
+public class DBConn {
|
|
|
+ public static void main(String[] args) throws Exception{
|
|
|
+
|
|
|
+ String username = "root";// 数据库用户名
|
|
|
+ String password = "langtong";// 数据库密码
|
|
|
+
|
|
|
+ //sqlServer
|
|
|
+ String sqlServerDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
|
|
+ String sqlServerUrl = "jdbc:sqlserver://192.1.2.92:1433;DatabaseName=lis";
|
|
|
+
|
|
|
+ //mysql8.0以上
|
|
|
+ String mysqlHighDriver = "com.mysql.cj.jdbc.Driver";
|
|
|
+ String mysqlHighUrl = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";
|
|
|
+
|
|
|
+ //mysql8.0以下
|
|
|
+ String mysqlLowDriver = "com.mysql.jdbc.Driver";
|
|
|
+ String mysqlLowUrl = "jdbc:mysql://localhost:3306/world";
|
|
|
+
|
|
|
+ //oracle
|
|
|
+ String oracleDriver = "oracle.jdbc.OracleDriver";
|
|
|
+ String oracleUrl = "jdbc:oracle:thin:@localhost:1521:XE";
|
|
|
+
|
|
|
+ File file1;
|
|
|
+ File file2;
|
|
|
+
|
|
|
+ FileOutputStream fop = null;
|
|
|
+
|
|
|
+ Connection conn = null;
|
|
|
+ try {
|
|
|
+ Class.forName(mysqlLowDriver);
|
|
|
+ conn = DriverManager.getConnection(mysqlLowUrl, username, password);
|
|
|
+
|
|
|
+ /*System.out.println("国家代码:");
|
|
|
+ Scanner input = new Scanner(System.in);
|
|
|
+ input.useDelimiter("\n");
|
|
|
+ String countryCode = input.next();*/
|
|
|
+
|
|
|
+ PreparedStatement pst = null;
|
|
|
+ pst = conn.prepareStatement("select DISTINCT (CountryCode) from city");
|
|
|
+ ResultSet rs = pst.executeQuery();
|
|
|
+
|
|
|
+ while(rs.next()){
|
|
|
+ String filePar = "C:/Users/94556/Desktop/test/" + rs.getString("CountryCode");// 文件夹路径
|
|
|
+ file1 = new File(filePar);
|
|
|
+ if (!file1.exists()) {
|
|
|
+ file1.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ PreparedStatement pst1 = null;
|
|
|
+ pst1 = conn.prepareStatement("select * from city where CountryCode = '" + rs.getString("CountryCode") +"'");
|
|
|
+ ResultSet rs1 = pst1.executeQuery();
|
|
|
+
|
|
|
+ while(rs1.next()){
|
|
|
+
|
|
|
+ file2 = new File("C:/Users/94556/Desktop/test/" + rs.getString("CountryCode") + "/" + rs1.getString("Name") + ".txt");
|
|
|
+ fop = new FileOutputStream(file2);
|
|
|
+
|
|
|
+ if (!file2.exists()) {
|
|
|
+ file2.createNewFile();
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuffer content = new StringBuffer("城市:" + rs1.getString("Name") +
|
|
|
+ ",国家代号:" + rs1.getString("CountryCode") +
|
|
|
+ ",行政区:" + rs1.getString("District") +
|
|
|
+ ",人口:" + rs1.getString("Population") + "\n");
|
|
|
+
|
|
|
+ byte[] contentInBytes = content.toString().getBytes();
|
|
|
+ fop.write(contentInBytes);
|
|
|
+
|
|
|
+ }
|
|
|
+ fop.flush();
|
|
|
+ fop.close();
|
|
|
+
|
|
|
+ rs1.close();
|
|
|
+ pst1.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ rs.close();
|
|
|
+ pst.close();
|
|
|
+ conn.close();
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|