mysql 匯出大量匯出表資料 (程式)

來源:互聯網
上載者:User

標籤:case   drive   substr   string   append   not   val   into   values   

private static String driverName = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
Connection con = null ;
Statement stmt =null;
PreparedStatement pstmt = null ;
try {
Class.forName(driverName);
con = DriverManager.getConnection("jdbc:mysql://192.168.5.148/xxx", "root", "123456");
String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘ifms‘ AND TABLE_TYPE =‘BASE TABLE‘ 
stmt = con.createStatement();
pstmt = con.prepareStatement(sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
String tableName = res.getString(1);
/*stmt = con.createStatement();*/
if(tableName.contains("`")) continue;
File file = new File("C:\\Users\\hq\\Desktop\\sql\\mysql\\a_tables\\spf_data\\"+tableName+"_data.sql");
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(file));
ResultSet rs = pstmt.executeQuery("desc `"+tableName+"`");
StringBuffer sb = new StringBuffer();
List<String> list = new ArrayList<String>();
while(rs.next()){
sb.append("`"+rs.getString(1)+"`,");
list.add("`"+rs.getString(2)+"`");
}
String str = sb.substring(0,sb.length()-1);
//str+=")";
System.out.println("select "+str+" from `"+tableName+"`");
ResultSet ress = pstmt.executeQuery("select "+str+" from `"+tableName+"`" );
while(ress.next()){
StringBuffer stb = new StringBuffer();
for(int i = 0 ;i<list.size();i++){
if(list.get(i).toLowerCase().contains("int")||list.get(i).toLowerCase().contains("decimal")||list.get(i).toLowerCase().contains("float")||list.get(i).toLowerCase().contains("double")){
stb.append(ress.getString(i+1)+",");
}else{
if(ress.getString(i+1)==null){
stb.append(ress.getString(i+1)+",");
}else{
stb.append("‘"+ress.getString(i+1)+"‘,");
}
}
}
String subs = stb.substring(0,stb.length()-1);
osw.write(" insert into `"+tableName+"`("+str+") values \n ("+subs+");\n");
}
System.out.println(tableName+"匯出成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}
}

mysql 匯出大量匯出表資料 (程式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.