package com.example.demo;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.filechooser.FileSystemView;
public class GenEntityTable {
private static final String driver="com.mysql.jdbc.Driver";
private static final String pwd="root";
private static final String user="root";
private static final String url = "jdbc:mysql://127.0.0.1:3306/test" + "?user=" + user + "&password=" + pwd + "&useUnicode=true&characterEncoding=UTF-8";
private static Connection getConnection=null;
public static void main(String[] args) {
FileSystemView fsv=FileSystemView.getFileSystemView();
getConnection=getConnections();
try {
DatabaseMetaData dbmd=getConnection.getMetaData();
ResultSet resultSet = dbmd.getTables(null, "%", "%", new String[] { "TABLE" });
while (resultSet.next()) {
String tableName=resultSet.getString("TABLE_NAME");
ResultSet rs2 = dbmd.getColumns(null, "%", tableName, "%");
System.out.println("###"+tableName);
System.out.println("| 列名 | 类型 | 注释 |");
System.out.println("| ------ | ------ | ------ |");
while(rs2.next()){
String name = rs2.getString("COLUMN_NAME");
String type = rs2.getString("TYPE_NAME");
String op=rs2.getString("REMARKS");
System.out.println("| "+name+" | "+type+" | "+op+" |");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 创建数据库连接
public static Connection getConnections() {
try {
Class.forName(driver);
getConnection = DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return getConnection;
}
// 判断属性类型
public static String sqlType2JavaType(String sqlType) {
String str = null;
if (sqlType.equalsIgnoreCase("bit")) {
str = "boolean";
} else if (sqlType.equalsIgnoreCase("tinyint")) {
str = "byte";
} else if (sqlType.equalsIgnoreCase("smallint")) {
str = "short";
} else if (sqlType.equalsIgnoreCase("int")) {
str = "int";
} else if (sqlType.equalsIgnoreCase("bigint")) {
str = "long";
} else if (sqlType.equalsIgnoreCase("float")) {
str = "float";
} else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")
|| sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
|| sqlType.equalsIgnoreCase("smallmoney")) {
str = "double";
} else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")
|| sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
|| sqlType.equalsIgnoreCase("text")) {
str = "String";
} else if (sqlType.equalsIgnoreCase("datetime")) {
str = "String";
} else if (sqlType.equalsIgnoreCase("image")) {
str = "Blod";
}
return str;
}
}