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)