本次采用的是MySql数据库,通过查询对应的表的字段,获得各个字段名,通过元数据获取每个字段的数据类型,再通过判断将数据库中的数据类型转换成java中对应的数据类型。通过循环遍历以及根据javaBean的标准,通过文件操作将java文件生成在项目下对应的包路径中。
生成javaBean的代码如下:
package createjavaBean;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class CreateJavaBean extends ConnectionDatabase {
// 包名
public static String strpackage;
// 数据库表名
public static String tableName;
public static StringBuffer sb = new StringBuffer();
public static Connection con = null;
public static ResultSet res = null;
public static ResultSetMetaData rsmd = null;
// 加载配置文件
static{
loadConfig();
}
public static void loadConfig(){
InputStream stream = ConnectionDatabase.class.getClassLoader().getResourceAsStream("db.properties");
Properties pro = new Properties();
try {
pro.load(stream);
strpackage = pro.getProperty("strpackage");
tableName = pro.getProperty("tableName");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void execute() throws IOException, SQLException {
con = getConnection();
String strsql = "select * from " + tableName;
Statement st;
try {
st = con.createStatement();
res = st.executeQuery(strsql);
rsmd = res.getMetaData();
String newstrpack = "";
newstrpack = strpackage.replace(".", "/");
System.out.println(newstrpack);
// 类名
String className = tableName.substring(0, 1).toUpperCase()
+ tableName.substring(1);
File file = new File("src" + "/" + newstrpack + "/" + className
+ ".java");
sb.append("package " + strpackage + ";\n");
sb.append("public class " + className + " implements Serializable{");
sb.append("\n");
for (int i = 1; i < rsmd.getColumnCount(); i++) {
String type = typename(getType(rsmd.getColumnTypeName(i)));
// 打印属性
System.out.println(getType(rsmd.getColumnTypeName(i)));
sb.append("\tprivate\t");
sb.append(type + "\t");
sb.append(rsmd.getColumnName(i) + ";");
sb.append("\n");
// 打印get方法
sb.append("\t//get方法\n");
sb.append("\tpublic\t" + type + "\tget" + rsmd.getColumnName(i).substring(0, 1).toUpperCase()+rsmd.getColumnName(i).substring(1)
+ "(){\n");
sb.append("\t\treturn " + rsmd.getColumnName(i) + ";\n\t\n");
sb.append("}\n");
// 打印set方法
sb.append("\t//set方法\n");
//
sb.append("\tpublic\t" + type + "\tset" + rsmd.getColumnName(i).substring(0, 1).toUpperCase()+rsmd.getColumnName(i).substring(1)
+ "(" + type + "\t" + rsmd.getColumnName(i) + ")"
+ "{\n");
sb.append("\treturn\tthis." + rsmd.getColumnName(i) + "\t = "
+ rsmd.getColumnName(i) + ";\n");
sb.append("}\n");
}
sb.append("}");
String strsb = sb.toString();
// 创建一个FileWriter对象
FileWriter fw = new FileWriter(file);
// 创建一个BufferedWriter对象
BufferedWriter bw = new BufferedWriter(fw);
bw.write(strsb);
bw.newLine();
// 关闭文件流
bw.flush();
fw.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
closeResultSet(res);
closeConneciton(con);
}
System.out.println(sb);
}
public static String typename(String typename) {
String tystr = null;
if (typename.equals("VARCHAR")) {
tystr = "String";
}else if (typename.equals("INTEGER")) {
tystr = "Integer";
}else if(typename.equals("TIMESTAMP")){
tystr = "Timestamp";
}else if(typename.equals("BIGINT")){
tystr = "Integer";
}else if(typename.equals("TEXT")){
tystr = "String";
}else if(typename.equals("DATETIME")){
tystr = "Date";
}else if(typename.equals("CLOB")){
tystr = "String";
}else{
tystr = "Object";
}
return tystr;
}
public static void main(String[] args) {
try {
execute();
} catch (IOException | SQLException e) {
e.printStackTrace();
}
}
public static String getType(String type) {
String colType = null;
if (type.indexOf("(") > 0) {
colType = type.substring(0, type.indexOf("("));
} else {
colType = type;
}
return colType;
}
}
连接数据库的代码如下:
package createjavaBean;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionDatabase{
private static String url;
private static String className;
private static String userName;
private static String password;
public static void closeResultSet(ResultSet res){
if(res != null){
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeConneciton(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
static{
loadConfig();
}
public static void loadConfig(){
InputStream stream = ConnectionDatabase.class.getClassLoader().getResourceAsStream("db.properties");
Properties pro = new Properties();
try {
pro.load(stream);
url = pro.getProperty("url");
className = pro.getProperty("className");
userName = pro.getProperty("userName");
password = pro.getProperty("password");
Class.forName(className);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
try {
return DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
}
public static void main(String[] args) {
try {
Class.forName("ConnectionDatabase");
System.out.println(getConnection());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
properties配置文件内容如下:
## using MYSQL database
url=jdbc:mysql://localhost:3306/information
className=com.mysql.jdbc.Driver
userName=root
password=123456
##package path it is has been existing
strpackage=com.changda.student.entity
## database table name
tableName=employee
此次使用的jar包为:mysqljdbc.jar