/**
*
*/
package com.abel.util;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
/**
* @project lucene
* @description
* @author abel.li
* @creation 2015-1-30
* @email abel0130@126.com
* @version
*/
public class CreateModel {
private String TABLENAME = "table";
private String DATABASE = "db";
public static void main(String[] args) {
List<Model> list = new CreateModel().excute();
for(Model model : list){
System.out.println("\tprivate "+ model.getColumnType() + " " + model.getColumnName() + ";\t\t\t//" + model.getColumnComment());
}
}
public Connection getConnection() {
String url = "";
String user = "";
String pwd = "";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// Class.forName("oracle.jdbc.driver.OracleDriver");
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");//有DB2客户端的Provider实例
// Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");//无DB2客户端的Provider实例
conn = (Connection) DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public List<Model> excute(){
Connection conn = getConnection(); // 得到数据库连接
//myDB为数据库名
String strsql = "SELECT COLUMN_NAME 'colName', DATA_TYPE 'fieldType', COLUMN_COMMENT 'colComment' " +
"FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE table_name = '" + TABLENAME + "' " +
"AND table_schema = '" + DATABASE + "';";
PreparedStatement ps = null;
ResultSet rs = null;
List<Model> list = new ArrayList<Model>();
try {
ps = (PreparedStatement) conn.prepareStatement(strsql);
// CallableStatement cs = conn.prepareCall("{CALL demoSp(? , ?)}") ;
rs = ps.executeQuery();
while (rs.next()) {
Model model = new Model();
model.setColumnName(rs.getString("colName"));
model.setColumnType(sqlType2JavaType(rs.getString("fieldType")));
model.setColumnComment(rs.getString("colComment"));
list.add(model);
}
rs.close();
ps.closeOnCompletion();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return list;
}
private String sqlType2JavaType(String sqlType) {
if (sqlType.equalsIgnoreCase("bit")) {
return "boolean";
} else if (sqlType.equalsIgnoreCase("tinyint")) {
return "byte";
} else if (sqlType.equalsIgnoreCase("smallint")) {
return "short";
} else if (sqlType.equalsIgnoreCase("int")) {
return "int";
} else if (sqlType.equalsIgnoreCase("bigint")) {
return "long";
} else if (sqlType.equalsIgnoreCase("float")) {
return "float";
} else if (sqlType.equalsIgnoreCase("decimal")
|| sqlType.equalsIgnoreCase("numeric")
|| sqlType.equalsIgnoreCase("real")
|| sqlType.equalsIgnoreCase("double")) {
return "double";
} else if (sqlType.equalsIgnoreCase("money")
|| sqlType.equalsIgnoreCase("smallmoney")) {
return "double";
} else if (sqlType.equalsIgnoreCase("varchar")
|| sqlType.equalsIgnoreCase("char")
|| sqlType.equalsIgnoreCase("nvarchar")
|| sqlType.equalsIgnoreCase("nchar")) {
return "String";
} else if (sqlType.equalsIgnoreCase("datetime")
||sqlType.equalsIgnoreCase("timestamp")) {
return "Date";
} else if (sqlType.equalsIgnoreCase("time")) {
return "Time";
}
else if (sqlType.equalsIgnoreCase("image")) {
return "Blob";
} else if (sqlType.equalsIgnoreCase("text")) {
return "Clob";
}
return null;
}
}
class Model{
private String columnName;
private String columnType;
private String columnComment;
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getColumnType() {
return columnType;
}
public void setColumnType(String columnType) {
this.columnType = columnType;
}
public String getColumnComment() {
return columnComment;
}
public void setColumnComment(String columnComment) {
this.columnComment = columnComment;
}
}
*
*/
package com.abel.util;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
/**
* @project lucene
* @description
* @author abel.li
* @creation 2015-1-30
* @email abel0130@126.com
* @version
*/
public class CreateModel {
private String TABLENAME = "table";
private String DATABASE = "db";
public static void main(String[] args) {
List<Model> list = new CreateModel().excute();
for(Model model : list){
System.out.println("\tprivate "+ model.getColumnType() + " " + model.getColumnName() + ";\t\t\t//" + model.getColumnComment());
}
}
public Connection getConnection() {
String url = "";
String user = "";
String pwd = "";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// Class.forName("oracle.jdbc.driver.OracleDriver");
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");//有DB2客户端的Provider实例
// Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");//无DB2客户端的Provider实例
conn = (Connection) DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public List<Model> excute(){
Connection conn = getConnection(); // 得到数据库连接
//myDB为数据库名
String strsql = "SELECT COLUMN_NAME 'colName', DATA_TYPE 'fieldType', COLUMN_COMMENT 'colComment' " +
"FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE table_name = '" + TABLENAME + "' " +
"AND table_schema = '" + DATABASE + "';";
PreparedStatement ps = null;
ResultSet rs = null;
List<Model> list = new ArrayList<Model>();
try {
ps = (PreparedStatement) conn.prepareStatement(strsql);
// CallableStatement cs = conn.prepareCall("{CALL demoSp(? , ?)}") ;
rs = ps.executeQuery();
while (rs.next()) {
Model model = new Model();
model.setColumnName(rs.getString("colName"));
model.setColumnType(sqlType2JavaType(rs.getString("fieldType")));
model.setColumnComment(rs.getString("colComment"));
list.add(model);
}
rs.close();
ps.closeOnCompletion();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return list;
}
private String sqlType2JavaType(String sqlType) {
if (sqlType.equalsIgnoreCase("bit")) {
return "boolean";
} else if (sqlType.equalsIgnoreCase("tinyint")) {
return "byte";
} else if (sqlType.equalsIgnoreCase("smallint")) {
return "short";
} else if (sqlType.equalsIgnoreCase("int")) {
return "int";
} else if (sqlType.equalsIgnoreCase("bigint")) {
return "long";
} else if (sqlType.equalsIgnoreCase("float")) {
return "float";
} else if (sqlType.equalsIgnoreCase("decimal")
|| sqlType.equalsIgnoreCase("numeric")
|| sqlType.equalsIgnoreCase("real")
|| sqlType.equalsIgnoreCase("double")) {
return "double";
} else if (sqlType.equalsIgnoreCase("money")
|| sqlType.equalsIgnoreCase("smallmoney")) {
return "double";
} else if (sqlType.equalsIgnoreCase("varchar")
|| sqlType.equalsIgnoreCase("char")
|| sqlType.equalsIgnoreCase("nvarchar")
|| sqlType.equalsIgnoreCase("nchar")) {
return "String";
} else if (sqlType.equalsIgnoreCase("datetime")
||sqlType.equalsIgnoreCase("timestamp")) {
return "Date";
} else if (sqlType.equalsIgnoreCase("time")) {
return "Time";
}
else if (sqlType.equalsIgnoreCase("image")) {
return "Blob";
} else if (sqlType.equalsIgnoreCase("text")) {
return "Clob";
}
return null;
}
}
class Model{
private String columnName;
private String columnType;
private String columnComment;
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getColumnType() {
return columnType;
}
public void setColumnType(String columnType) {
this.columnType = columnType;
}
public String getColumnComment() {
return columnComment;
}
public void setColumnComment(String columnComment) {
this.columnComment = columnComment;
}
}