JDBC连接mysql数据库获取数据并生成Txt输出

测试下通过jdbc连接mysql数据,获取默认表格COLLATION_CHARACTER_SET_APPLICABILITY里的数据,并生成txt文件输出。

 1 import java.io.File;
 2 import java.io.FileWriter;
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.Statement;
 7 
 8 public class testMysqlJdbc2 {
 9     public static void main(String[] args) {
10         //执行获取txt文件操作
11         try {
12             getTxt();
13         }catch (Exception e){
14             e.printStackTrace();
15         }
16 
17     }
18 
19     //创建jdbc连接mysql数据库,获取COLLATION_CHARACTER_SET_APPLICABILITY表格下的所有数据
20     public static void getTxt()throws Exception{
21         //创建jdbc需要的条件
22         Connection con = null;
23         String sql ;
24         //url拼接直接带上了用户名和密码,可以根据本地情况进行修改
25         String url = "jdbc:mysql://localhost:3306/information_schema?user=root&password=&useUnicode=true&characterEncoding=UTF8";
26         //创建IO需求的条件,并声明输出路径
27         File file = null;
28         FileWriter fw = null;
29         file = new File("F:\\ziyuan\\testIo\\testIO.txt");
30         if(!file.exists()){
31             file.createNewFile();
32         }
33         fw = new FileWriter(file);
34         try {
35             //加载驱动
36             Class.forName("com.mysql.jdbc.Driver");
37             //创建连接
38             con = DriverManager.getConnection(url);
39             //生成声明
40             Statement stat = con.createStatement();
41             sql = "select * from COLLATION_CHARACTER_SET_APPLICABILITY";
42             //获取sql结果
43             ResultSet result = stat.executeQuery(sql);
44             fw.write("COLLATION_NAME CHARACTER_SET_NAME"+"\r\n");
45             while (result.next()){
46                 fw.write(result.getString("COLLATION_NAME")+"  " + result.getString("CHARACTER_SET_NAME") + "\r\n");
47                 fw.flush();
48             }
49             System.out.println("完成查询插入txt功能");
50         }catch (Exception e){
51             e.printStackTrace();
52         }finally {
53             con.close();
54             fw.close();
55         }
56     }
57 }

基本的操作就是上面这些了,如果有更高效或者更合理的方法,请不吝赐教。

 1 import java.io.File;
 2 import java.io.FileWriter;
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.Statement;
 7 
 8 public class testMysqlJdbc2 {
 9     public static void main(String[] args) {
10         try {
11             getTxt();
12         }catch (Exception e){
13             e.printStackTrace();
14         }
15 
16     }
17 
18     //创建jdbc连接mysql数据库,获取COLLATION_CHARACTER_SET_APPLICABILITY表格下的所有数据
19     public static void getTxt()throws Exception{
20         Connection con = null;
21         String sql ;
22         String url = "jdbc:mysql://localhost:3306/information_schema?user=root&password=zzw19931024&useUnicode=true&characterEncoding=UTF8";
23         File file = null;
24         FileWriter fw = null;
25         file = new File("F:\\ziyuan\\testIo\\testIO.txt");
26         if(!file.exists()){
27             file.createNewFile();
28         }
29         fw = new FileWriter(file);
30         try {
31             //加载驱动
32             Class.forName("com.mysql.jdbc.Driver");
33             //创建连接
34             con = DriverManager.getConnection(url);
35             //生成工厂
36             Statement stat = con.createStatement();
37             sql = "select * from COLLATION_CHARACTER_SET_APPLICABILITY";
38             //获取sql结果
39             ResultSet result = stat.executeQuery(sql);
40             fw.write("COLLATION_NAME CHARACTER_SET_NAME"+"\r\n");
41             while (result.next()){
42                 fw.write(result.getString("COLLATION_NAME")+"  " + result.getString("CHARACTER_SET_NAME") + "\r\n");
43                 fw.flush();
44             }
45             System.out.println("完成查询插入txt功能");
46         }catch (Exception e){
47             e.printStackTrace();
48         }finally {
49             con.close();
50             fw.close();
51         }
52
posted @ 2018-09-06 14:19  呜呼~厉害了  阅读(2478)  评论(0编辑  收藏  举报