首页 > 数据库技术 > 详细

mysql 批量导出建表语句 (视图,函数同理)

时间:2017-01-03 21:52:00      阅读:823      评论:0      收藏:0      [点我收藏+]

private static String driverName = "com.mysql.jdbc.Driver";

public static void main(String[] args)
throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}

Connection con = DriverManager.getConnection("jdbc:mysql://192.168.5.148/ifms", "root", "123456");
String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘ifms‘ AND TABLE_TYPE =‘BASE TABLE‘";
Statement stmt = (Statement) con.createStatement();
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
String tableName = res.getString(1);
stmt = (Statement) con.createStatement();
if(tableName.contains("`")) continue;
ResultSet rs = stmt.executeQuery("show create table `"+tableName+"`");
while(rs.next()){
File file = new File("C:\\Users\\hq\\Desktop\\sql\\mysql\\b_tables\\"+tableName+".sql");
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(file));
osw.write(rs.getString(2));
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();
}
}
}
}
}

mysql 批量导出建表语句 (视图,函数同理)

原文:http://www.cnblogs.com/sx2zx/p/6246426.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!