MySQL Export bulk export table data (program)

Source: Internet
Author: User
Tags stmt stringbuffer

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+ "export Success");
} 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 Export bulk export table data (program)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.