根据数据库表来创建javabean,如果数据库表的字段用了下划线的需要自己改下代码,我这里默认就是数据库字段直接是ename这样的。
代码很简单整体如下:
package com.zzg.demo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class GetJavaBean {
public static void main(String[] args){
ArrayList a=new ArrayList();
a.add("emp");
a.add("dept");
HashMapmap=new HashMap();
map.put("driver", "oracle.jdbc.driver.OracleDriver");
map.put("connectionURL", "jdbc:oracle:thin:@127.0.0.1:1521:orcl");
map.put("username", "scott");
map.put("password", "tiger");
map.put("dir", "D:\\");
map.put("tablist", a);
GetJavaBean.getClassBean(map);
}
public static void getClassBean(HashMapmap){
getDateSource(map);
}
//map中的数据driver,connectionURL,username,password,dir
public static void getDateSource(HashMapmap){
ArrayList tabList = (ArrayList) map.get("tablist");
Connection con=null;
Statement st=null;
ResultSet rs=null;
try{
Class.forName((String) map.get("driver"));
con=DriverManager.getConnection((String)map.get("connectionURL"),(String)map.get("username"),(String)map.get("password"));
st=con.createStatement();
rs=selectTable2Rs(tabList,st,(String)map.get("dir"));
}catch(Exception e){e.printStackTrace();
}finally{
try{
rs.close();
}catch(Exception e){}
try{
st.close();
}catch(Exception e){}
try{
con.close();
}catch(Exception e){}
}
}
/**
*
* @param tabList 要查询的表的list
* @param st
* @param dir 要输出的文件的位置,到文件夹
* @return
* @throws SQLException
* @throws IOException
*/
public static ResultSet selectTable2Rs(List tabList,Statement st,String dir) throws SQLException, IOException{
Iterator iterator=tabList.iterator();
ResultSet rs=null;
while(iterator.hasNext()){
String tabName=iterator.next();
String sql="select * from "+tabName;
rs=st.executeQuery(sql);
operateTable(rs,tabName,dir);
}
return rs;
}
/**
* 挖取数据
* @param rs
* @param tabName 要查询的数据库表的名字
* @param dir 要输出的文件的位置,到文件夹
* @throws SQLException
* @throws IOException
*/
public static void operateTable(ResultSet rs,String tabName,String dir) throws SQLException, IOException{
ResultSetMetaData rsmd = rs.getMetaData();
int colsLength = rsmd.getColumnCount();
List colTypes = new ArrayList();
List colNames = new ArrayList();
for(int i=0;i
String name=rsmd.getColumnName(i+1);
String type=rsmd.getColumnClassName(i+1);
colNames.add(i, name.toLowerCase());
colTypes.add(i,type);
}
output2JavaFile(colNames,colTypes,tabName,dir);
}
public static void output2JavaFile(List colNames,List colTypes,String tabName,String dir) throws IOException{
String fileStream=outputStream(colNames, colTypes, tabName);
String dirFile=dir+getClassName(tabName)+".java";
File file = new File(dirFile);
if(file.exists()){
System.out.println("警告:");
}else{
BufferedWriter writer = new BufferedWriter(new FileWriter(dirFile));
writer.write(fileStream.toString());
writer.flush();
//关闭资源
writer.close();
System.out.println("文件"+getClassName(tabName)+".java"+"写入成功");
}
};
public static String outputStream(List colNames,List colTypes,String tabName){
tabName=getClassName(tabName);
StringBuilder sb=new StringBuilder();
sb.append("public class "+tabName+"{\n");
sb.append(getClassStream(colNames,colTypes));
sb.append("public ").append(tabName).append("(){}").append("\n");
sb.append(outputMethod(colNames, colTypes));
sb.append("\n}");
//System.out.println(sb.toString());
return sb.toString();
}
public static String getClassStream(List colNames,List colTypes){
StringBuilder sb=new StringBuilder();
for(int i=0;i
sb.append("private ");
sb.append(colTypes.get(i)+" "+colNames.get(i));
sb.append(";\n");
}
return sb.toString();
}
public static String outputMethod(List colNames,List colTypes){
StringBuilder sb=new StringBuilder();
for(int i=0;i
sb.append("\n");
sb.append(getMethod(colNames.get(i),colTypes.get(i)));
sb.append("\n");
sb.append(setMethod(colNames.get(i)));
}
return sb.toString();
}
public static String getMethod(String name,String type){
StringBuilder sb=new StringBuilder();
sb.append("public ").append(type).append(" get");
sb.append(name.substring(0,1).toUpperCase()+name.substring(1,name.length()));
sb.append("(){").append("\n");
sb.append(" return ").append(name).append(";").append("\n");
sb.append("}").append("\n");
return sb.toString();
}
public static String setMethod(String name){
StringBuilder sb=new StringBuilder();
sb.append("public void ").append("set");
sb.append(name.substring(0,1).toUpperCase()+name.substring(1,name.length()));
sb.append("(){").append("\n");
sb.append(" this. ").append(name).append("=").append(name).append(";").append("\n");
sb.append("}").append("\n");
return sb.toString();
}
public static String getClassName(String tabName){
return tabName.toLowerCase().substring(0,1).toUpperCase()+
tabName.toLowerCase().substring(1,tabName.length());
}
}
后面我会对类型进行改进,一般大家用比较多的一般是int,String,Date这样。但是现在用的是java.sql.*的。