java⼯具类–⾃动将数据库表⽣成javabean
最近和数据库的表打交道挺多的,因为暂时做的是接⼝活。
在这过程中发现要把表转换成对应的javabean类型,字段少的表还⾏,如果不⼩⼼碰到⼏⼗个字段的他妈的写起来就有点⿇烦了,万⼀碰到⼏百个的呢,那不是要崩溃。
于是想写个⼯具类,⾃动⽣成javabean。
先说下思路:
1.读取数据库表,获取⾥⾯的字段名。
准备连接数据库的驱动包,这⾥只是针对了oracle数据库和mysql数据库
2.构建⼀个stringBuffer字符串,⽤来⽣成我们需要的类。
3.写⼊⽂件
要求具备相应的⽂件流知识。
好了,准备⼯作就这些,下⾯附上代码,
l;
/*
* 给出数据库JAR包,数据库链接路径,数据库表空间名,数据库名,数据库密码,表名
*可以提取出来创建表属性的javaBean⽂件,并且提供标准的get,set⽅法。
* 此程序将所有字段和数据提取出来定义为String类型,如有别的需要可以提取表中字段的类型和别的表信息,⾃动⽣成
* java⽂件
* \t 表⽰空格
* \r 表⽰换⾏等价于 \n
* ResultSetMetaData 关键
* */
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlToBean {
private Connection conn = null;    //保存链接路径
private Statement stmt = null;    //建⽴连接
private ResultSetMetaData meta = null;  //保存表属性信息
private ResultSet rs = null;  //查询结果集
private OutputStreamWriter osw = null;
private BufferedWriter bw = null;
private FileOutputStream fos = null;
private static StringBuffer coding = new StringBuffer();  //字符串缓冲区
private String driver = null;    //数据库包名
private String url = null;          //路径名
private String table = null;        //表空间名
private String password = null;    //密码
private String tableName = null;    //表名
public SqlToBean(String driver, String url, String table, String password, String tableName) {
this.driver = driver;
this.url = url;
this.table = table;
this.password = password;
this.tableName = tableName;
}
private String getCoding(StringBuffer code) {
String();
}
private StringBuffer createGenerate(String property) {
String prop = LowerCase();
coding.append("\r \t private String " + prop + ";");
return coding;
}
private StringBuffer createMethod(String[] str){
for(int i=0;i<str.length;i++){
//str[i].charAt(0) - 32)转成⼤写思路
str[i] = str[i].toLowerCase();
coding.append("\r \t public void set" + (char)(str[i].charAt(0) - 32)+ str[i].substring(1)+"(String " + str[i] +"){");  coding.append("\r \t\t this."+str[i] + "=" + str[i] + ";");
coding.append("\r \t }");
coding.append("\r \t public String get" + (char)(str[i].charAt(0) - 32)+ str[i].substring(1)+"(){");
coding.append("\r \t\t return this."+str[i] +  ";");
coding.append("\r \t }\n");
}
return coding;
}
/*
* 关闭与数据库的所有链接
* */
private void destroy() {
try {
if(conn != null){
conn.close();
conn = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(rs != null){
rs.close();
rs = null;
}
if(bw != null){
bw.close();
bw = null;
}
if(fos != null) {
fos.close();
fos = null;
}
if(osw != null) {
osw.close();
osw = null;
}
} catch (SQLException e) {
e.printStackTrace();
}  catch (IOException e) {
e.printStackTrace();
}
}
/*
* 数据库连接发⽣异常就关闭链接
* */
private  void connect () {
try {
Class.forName(driver);
conn = Connection(url,table,password);
stmt = ateStatement();
字符串转数组工具类的方法
rs = uteQuery("select  * from " + tableName ); //查询下确定结果集是那个表的  meta = rs.getMetaData();                        //调⽤结果集的记录表信息的⽅法
} catch (ClassNotFoundException e) {
e.printStackTrace();
try {
if(conn != null){
conn.close();
conn = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(rs != null){
rs.close();
rs = null;
}
} catch (SQLException e1) {
e.printStackTrace();
}
}  catch (SQLException e) {
e.printStackTrace();
try {
if(conn != null){
conn.close();
conn = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(rs != null){
rs.close();
rs = null;
}
} catch (SQLException e1) {
e.printStackTrace();
}
}
}
private String[] getColumenName() {
/*得到表的所有列名以字符串数组的形式返回
* */
int count;
String[] str = null;
try {
count = ColumnCount();
String[] strColumenName = new String[count];
for(int i = 1;i <= count; i++) {
strColumenName[i-1] = ColumnName(i);
}
str = strColumenName;
} catch (SQLException e) {
e.printStackTrace();
}
return str;
}
/**
* 写⼊指定的⽂件中
* @param message
*/
private void writeData(String message,String className) {
String file = "C:\\"+className+".java";
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
bw.write(message);
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public StringBuffer createClassName(String className){
coding.append("public class " + className + "{\n");
return coding;
}
public static void main(String[] args) {
String className = "Hellow";
//SqlToBean sqlToBean = new
SqlToBean("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@192.168.3.11:1521:orcl","mamibon","mamibon","my_standard_data2");  SqlToBean sqlToBean = new
SqlToBean("sql.Driver","jdbc:mysql://117.79.84.144:3306/wordpress","wangjun","wangjun123","wp_users");
//连接数据库
//获取表的字段
String[] str ;
str = ColumenName();
for(int i = 0;i<str.length ;i++) {
}
coding.append("\n");
coding.append("\n}");
//写⼊⽂件
sqlToBean.Coding(coding),className);
sqlToBean.destroy();
System.out.println("如果觉得这⼯具类不错,请关注我们的⽹站:www.itbuluoge,期待你的⼊住,程序员俱乐部,为您提供更多的帮助!");
System.out.println("如果觉得这⼯具类不错,请关注我们的⽹站:www.itbuluoge,期待你的⼊住,程序员俱乐部,为您提供更多的帮助!");
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。