測試下通過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
