java工具类–自动将数据库表生成javabean
最近和数据库的表打交道挺多的,因为暂时做的是接口活。
在这过程中发现要把表转换成对应的javabean类型,字段少的表还行,如果不小心碰到几十个字段的他妈的写起来就有点麻烦了,万一碰到几百个的呢,那不是要崩溃。
于是想写个工具类,自动生成javabean。
先说下思路:
1.读取数据库表,获取里面的字段名。
准备连接数据库的驱动包,这里只是针对了oracle数据库和mysql数据库
2.构建一个stringBuffer字符串,用来生成我们需要的类。
3.写入文件
要求具备相应的文件流知识。
好了,准备工作就这些,下面附上代码,
package com.tool;
/*
* 给出数据库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) {
return code.toString();
}
private StringBuffer createGenerate(String property) {
String prop = property.toLowerCase();
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 = DriverManager.getConnection(url,table,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("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 = meta.getColumnCount();
String[] strColumenName = new String[count];
for(int i = 1;i <= count; i++) {
strColumenName[i-1] = meta.getColumnName(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("org.gjt.mm.mysql.Driver","jdbc:mysql://117.79.84.144:3306/wordpress","wangjun","wangjun123","wp_users");
//连接数据库
sqlToBean.connect();
sqlToBean.createClassName(className);
//获取表的字段
String[] str ;
str = sqlToBean.getColumenName();
for(int i = 0;i<str.length ;i++) {
sqlToBean.createGenerate(str[i]);
}
coding.append("\n");
sqlToBean.createMethod(str);
coding.append("\n}");
//写入文件
sqlToBean.writeData(sqlToBean.getCoding(coding),className);
sqlToBean.destroy();
System.out.println("如果觉得这工具类不错,请关注我们的网站:http://www.itbuluoge.com,期待你的入住,程序员俱乐部,为您提供更多的帮助!");
System.out.println("如果觉得这工具类不错,请关注我们的网站:http://www.itbuluoge.com,期待你的入住,程序员俱乐部,为您提供更多的帮助!");
}
}
java工具类–自动将数据库表生成javabean的更多相关文章
- Hibernate由model类自动同步数据库表结构
在开发中遇到了个问题,每次测试数据库增加表结构的时候,本地pull下最新代码导致启动报错,上网搜了快速解决办法---->hibernate 配置属性中,hibernate.hbm2ddl.aut ...
- C# 通过自定义特性 实现根据实体类自动创建数据库表
.Net新手通常容易把属性(Property)跟特性(Attribute)搞混,其实这是两种不同的东西 属性指的类中封装的数据字段:而特性是对类.字段.方法和属性等元素标注的声明性信息 如下代码(Id ...
- 由数据库表生成jpa实体工具
package cn.net.yto.aaa.dao.generator; /** * 由数据库表生成jpa实体工具 * * @author huike * Created by gf.liu on ...
- django根据已有数据库表生成model类
django根据已有数据库表生成model类 创建一个Django项目 django-admin startproject 'xxxx' 修改setting文件,在setting里面设置你要连接的数据 ...
- SqlServer数据库表生成C# Model实体类SQL语句——补充
在sql语句最前边加上 use[数据库名] 原链接:https://www.cnblogs.com/jhli/p/11552105.html --[SQL骚操作]SqlServer数据库表生成C ...
- SpringBoot使用Hibernate,实现自动创建数据库表【博客数据库设计】
我们准备设计博客,那就要设计数据库. 我们可以使用Hibernate来自动生成数据库. 博客数据库的结构: 实体类: 博客 Blog 博客分类 Type 博客标签 Tag 博客评论 Comment 用 ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- 排名前 16 的 Java 工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- 快速创建SpringBoot2.x应用之工具类自动创建web应用、SpringBoot2.x的依赖默认Maven版本
快速创建SpringBoot2.x应用之工具类自动创建web应用简介:使用构建工具自动生成项目基本架构 1.工具自动创建:http://start.spring.io/ 2.访问地址:http://l ...
随机推荐
- 如何访问Microsoft Azure Storage
首先先要创建存储账户 http://www.cnblogs.com/SignalTips/p/4119128.html 可以通过以下的几个方式访问 通过Visual Studio 2013 Commu ...
- PAT乙级真题1008. 数组元素循环右移问题 (20)
原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...
- OSGI容器与插件
插件必须符合osgi规范才能插到osgi容器中,osgi容器查看插件jar中MANIFEST.MF中osgi容器. 所谓插件----就是打包好的jar文件, 内部都封装好了一些功能
- IIS WMI Provider
section contains information about the classes that are implemented by the IIS WMI provider in the M ...
- java package and import
1.Package Package类的主要作用是解决命名冲突.package中所存放的所有文件,一般分一下就分这三种 1,java程序源文件,扩展名为.java. 2,编译好的java类文件,扩展名为 ...
- Java加解密与数字签名
** Java加解密 ** 实现方式:JDK实现,CC,BC JDK提供比较基础的底层的实现:CC提供一些简化的操作:BC提供补充 一.Base64加密 非常简单,加密解密就一个函数. 代码如下: 二 ...
- 常见的装置与其在Linux当中的档名
需要特别留意的是硬盘机(不论是IDE/SCSI/U盘都一样),每个磁碟机的磁盘分区(partition)不同时, 其磁碟档名还会改变呢!下一小节我们会介绍磁盘分区的相关概念啦!需要特别注意的是磁带机的 ...
- CGI与Servlet的区别和联系
1. 定义: CGI(Common Gateway Interface 公共网关接口)是HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. 2. 功能: 绝大多 ...
- WPFMediaKit照相功能
最近写的一个WPF照相功能,往各位吐槽,提供优化 在WPF 设计器中添加如下代码 xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.C ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...