生成数据库字典
2016-06-25 14:01
459 查看
boss要数据库字典,几百张表手动会很累.于是手动写了生成数据库字典,可再次用于维护.
package Filter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.StringUtil;
import com.mysql.jdbc.StringUtils;
public class DoDbToExcel {
static Connection con = dbTest.getConnection();
//接收结果集,排打数据,保存.
public static boolean opTable(ResultSet res) throws IOException, SQLException{
int j = 3;
try {
FileInputStream fis = new FileInputStream(new File("C:/Users/Administrator/Desktop/数据库定义书.xls"));
HSSFWorkbook book = new HSSFWorkbook(fis); //打开新的工作 load(原有db字典)
int i = book.getNumberOfSheets();
for(int j1=4;j1<i;j1++){ //删除3以后的sheet
book.removeSheetAt(4);
}
while(res.next()){ //遍历结果集 tableName
HSSFSheet sheet = book.cloneSheet(3); //克隆模板(sheet3)
if(StringUtils.isEmpty(res.getString(2))){
book.setSheetName(j+1,"表注释为空"+j);
}else{
book.setSheetName(j+1, res.getString(2));//设置sheet名
}
System.out.println(sheet.getSheetName());
HSSFRow row = sheet.getRow(6); //获得行
row.getCell(1).setCellValue(res.getString(2)); //设置数据到单元格
row.getCell(3).setCellValue(res.getString(1));
op(res.getString(1), sheet);//查询所有表列.设置到单元格 tableData
//}
j++;
}
FileOutputStream fos = new FileOutputStream(new File("C:/Users/Administrator/Desktop/数据库定义书1.xls")); //打开新的工作
book.write(fos); //输出
fis.close(); //输入关闭
fos.close(); //输出关闭
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
con.close();
}
return false;
}
public static boolean op(String tableName,HSSFSheet sheet) throws SQLException{ //查询表里所有数据 思路一个表一个表来,先表名、注释,然后此表内所有列,注释
int i = 11;
int j = 1;
Statement st = con.createStatement(); //下面同上 不赘述
String sql = "Select COLUMN_NAME fieldName, COLUMN_TYPE dataType, COLUMN_COMMENT fieldnote "
+ " from INFORMATION_SCHEMA.COLUMNS"
+ " Where table_name = '"
+ tableName
+ "' "
+ " AND table_schema = 'mstps';";
ResultSet re = st.executeQuery(sql);
while(re.next()){
j++;
if(sheet.getRow(i).getCell(2)==null){
HSSFRow row1 = sheet.createRow(i);
HSSFCellStyle hcs = sheet.getRow(11).getCell(2).getCellStyle();
row1.createCell(2).setCellStyle(hcs);
row1.getCell(2).setCellValue(re.getString(1));
//
HSSFCellStyle hcs1 = sheet.getRow(11).getCell(3).getCellStyle();
row1.createCell(3).setCellStyle(hcs1);
row1.getCell(3).setCellValue(re.getString(3));
//
HSSFCellStyle hcs2 = sheet.getRow(11).getCell(4).getCellStyle();
row1.createCell(4).setCellStyle(hcs2);
row1.getCell(4).setCellValue(re.getString(2));
//
HSSFCellStyle hcs3 = sheet.getRow(11).getCell(12).getCellStyle();
row1.createCell(12).setCellStyle(hcs3);
row1.getCell(12).setCellValue("v2");
}else{
HSSFRow row1 = sheet.getRow(i);
row1.getCell(2).setCellValue(re.getString(1));
row1.getCell(3).setCellValue(re.getString(3));
row1.getCell(4).setCellValue(re.getString(2));
row1.getCell(12).setCellValue("v2");
}
i++;
}
return true;
}
}
------------------------------------------------------------------------------------------------------------------------------------
package Filter;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
//数据库连接
public class dbTest {
public static final String DATABASE_URL = "jdbc:mysql://192.168.1.235/mstps";
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";
private static Connection con = null;
public static Connection getConnection(){
try{
Class.forName(DRIVER_CLASS);
con=DriverManager.getConnection(DATABASE_URL, user, password);
System.out.println("数据库连接成功,请稍候..");
return con;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
return con;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
package Filter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test {
public static void main(String[] args) throws SQLException, IOException {
Connection con = dbTest.getConnection(); //连接数据库
Statement st = con.createStatement(); // 预执行sql
String sql = "Select table_name dBname,TABLE_COMMENT sheetNotes from INFORMATION_SCHEMA.TABLES Where table_schema = 'mstps';";
ResultSet re = st.executeQuery(sql); //执行sql返回resultSet
DoDbToExcel.opTable(re);//打开原有db字典,取得模板,排打数据,存入新的excel.
con.close();
System.out.println("secsess !you are genius!");//代码不是很完善,望补充,大家共同进步.例:表名重复的sheet没做处理 其实也很简单,sql查出重复的表名,进行修改. 更多问题不赘述.
}
}
package Filter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.StringUtil;
import com.mysql.jdbc.StringUtils;
public class DoDbToExcel {
static Connection con = dbTest.getConnection();
//接收结果集,排打数据,保存.
public static boolean opTable(ResultSet res) throws IOException, SQLException{
int j = 3;
try {
FileInputStream fis = new FileInputStream(new File("C:/Users/Administrator/Desktop/数据库定义书.xls"));
HSSFWorkbook book = new HSSFWorkbook(fis); //打开新的工作 load(原有db字典)
int i = book.getNumberOfSheets();
for(int j1=4;j1<i;j1++){ //删除3以后的sheet
book.removeSheetAt(4);
}
while(res.next()){ //遍历结果集 tableName
HSSFSheet sheet = book.cloneSheet(3); //克隆模板(sheet3)
if(StringUtils.isEmpty(res.getString(2))){
book.setSheetName(j+1,"表注释为空"+j);
}else{
book.setSheetName(j+1, res.getString(2));//设置sheet名
}
System.out.println(sheet.getSheetName());
HSSFRow row = sheet.getRow(6); //获得行
row.getCell(1).setCellValue(res.getString(2)); //设置数据到单元格
row.getCell(3).setCellValue(res.getString(1));
op(res.getString(1), sheet);//查询所有表列.设置到单元格 tableData
//}
j++;
}
FileOutputStream fos = new FileOutputStream(new File("C:/Users/Administrator/Desktop/数据库定义书1.xls")); //打开新的工作
book.write(fos); //输出
fis.close(); //输入关闭
fos.close(); //输出关闭
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
con.close();
}
return false;
}
public static boolean op(String tableName,HSSFSheet sheet) throws SQLException{ //查询表里所有数据 思路一个表一个表来,先表名、注释,然后此表内所有列,注释
int i = 11;
int j = 1;
Statement st = con.createStatement(); //下面同上 不赘述
String sql = "Select COLUMN_NAME fieldName, COLUMN_TYPE dataType, COLUMN_COMMENT fieldnote "
+ " from INFORMATION_SCHEMA.COLUMNS"
+ " Where table_name = '"
+ tableName
+ "' "
+ " AND table_schema = 'mstps';";
ResultSet re = st.executeQuery(sql);
while(re.next()){
j++;
if(sheet.getRow(i).getCell(2)==null){
HSSFRow row1 = sheet.createRow(i);
HSSFCellStyle hcs = sheet.getRow(11).getCell(2).getCellStyle();
row1.createCell(2).setCellStyle(hcs);
row1.getCell(2).setCellValue(re.getString(1));
//
HSSFCellStyle hcs1 = sheet.getRow(11).getCell(3).getCellStyle();
row1.createCell(3).setCellStyle(hcs1);
row1.getCell(3).setCellValue(re.getString(3));
//
HSSFCellStyle hcs2 = sheet.getRow(11).getCell(4).getCellStyle();
row1.createCell(4).setCellStyle(hcs2);
row1.getCell(4).setCellValue(re.getString(2));
//
HSSFCellStyle hcs3 = sheet.getRow(11).getCell(12).getCellStyle();
row1.createCell(12).setCellStyle(hcs3);
row1.getCell(12).setCellValue("v2");
}else{
HSSFRow row1 = sheet.getRow(i);
row1.getCell(2).setCellValue(re.getString(1));
row1.getCell(3).setCellValue(re.getString(3));
row1.getCell(4).setCellValue(re.getString(2));
row1.getCell(12).setCellValue("v2");
}
i++;
}
return true;
}
}
------------------------------------------------------------------------------------------------------------------------------------
package Filter;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
//数据库连接
public class dbTest {
public static final String DATABASE_URL = "jdbc:mysql://192.168.1.235/mstps";
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";
private static Connection con = null;
public static Connection getConnection(){
try{
Class.forName(DRIVER_CLASS);
con=DriverManager.getConnection(DATABASE_URL, user, password);
System.out.println("数据库连接成功,请稍候..");
return con;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
return con;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
package Filter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test {
public static void main(String[] args) throws SQLException, IOException {
Connection con = dbTest.getConnection(); //连接数据库
Statement st = con.createStatement(); // 预执行sql
String sql = "Select table_name dBname,TABLE_COMMENT sheetNotes from INFORMATION_SCHEMA.TABLES Where table_schema = 'mstps';";
ResultSet re = st.executeQuery(sql); //执行sql返回resultSet
DoDbToExcel.opTable(re);//打开原有db字典,取得模板,排打数据,存入新的excel.
con.close();
System.out.println("secsess !you are genius!");//代码不是很完善,望补充,大家共同进步.例:表名重复的sheet没做处理 其实也很简单,sql查出重复的表名,进行修改. 更多问题不赘述.
}
}
相关文章推荐
- MySQL数据库存放目录
- Sqlite3常用的插入方法及性能测试
- redis集群环境的搭建和错误分析
- 帆软报表与SQL语句
- MySQL 存储过程探秘
- MySQL 存储过程探秘
- ibatis动态sql配置启动时提示:The content of elements must consist of well-formed character data...
- MySQL中多表删除方法(转载)
- Mongodb学习-索引
- mysql主从复制
- [SQL Server]分页功能的实现
- 面向对象数据库
- mysql配置优化
- 关于数据库管理工具的使用----把数据库中的表等生成word文档
- mysql二进制日志设置
- 如何在Django中使用mongodb数据库?
- cx_Oracle在sublime text里运行遇到 ImportError错误解决办法
- MYSQL---数据库中表的操作
- Sql Server之旅——终点站 nolock引发的三级事件的一些思考
- MySQL Server 5.6