环境:XP+JDK1.5+Oracle10g
注意事项
1 请确保你的用户有权限访问
select * from DBA_COL_COMMENTS
2 java类的属性名称一律小写
3 所要jar为:commons-lang-2.4.jar,ojdbc14.jar
4 请在src下放置jdbc.properties属性文件,并且文件中的属性名称不能修改
5 创建java类属性类型全部为String类型
创建原因:
现在ORM很流行,但是在有的项目中并没有用到,java实体类的编写实在没什么可写的,如字段太多的话,手工编写将是一块恶梦,有了这个小的生成工具类,以后就方便多了!
说明:生成的java类实现了java.io.Serializable接口,并且在属性字段,get,set方法上如原字段有注释就加上,没有保持为空
工程图片如下:
引入net.liuzd.tools.tabletoclass.jar包的图片效果
测试类代码如下:
package test;
import net.liuzd.tools.db.entity.JavaDataBaseBean;
import net.liuzd.tools.db.entity.JdbcInfo;
import net.liuzd.tools.db.util.JavaSourceFactory;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class TestCreateJavaBean {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
String packageName = "net.liuzd.tools.db";
String classFilePath = "E:/net/liuzd/tools/db";
//生成一个类文件的演示
String tableName = "TUSER";
String className = "Tuser";
//请把拷贝的java代码格式化: Ctrl+F/
/*******第一种:生成一个java类文件的方式*********************/
//JavaSourceFactory.create(tableName,packageName,className,classFilePath);
//支持链式风格编程:请确保一定要传入以下四个必备元素,否则报错
/*JavaSourceFactory.create(
new JavaDataBaseBean().setTableName(tableName).setPackageName(packageName).setClassName(className).setClassFilePath(classFilePath)
); */
/*******第二种:生成多个java类文件的方式*********************/
//默认读取jdbc.properties配置文件中的数据库连接方式
//JavaSourceFactory.create(packageName, classFilePath, new String[]{"x","x"});
//指定读取用户所在库的多个表
/*JavaSourceFactory.create(packageName, classFilePath,
new JdbcInfo("orcl","orcl","jdbc:oracle:thin:@192.168.10.1:1521:orcl","oracle.jdbc.driver.OracleDriver"),
"x","x");*/
/*******第三种:生成用户下所有表对应的java类文件********************/
//默认读取jdbc.properties配置文件中的数据库连接方式
JavaSourceFactory.create(packageName, classFilePath);
//创建指定用户下所有的类文件
/*JavaSourceFactory.create(packageName, classFilePath,
new JdbcInfo("orcl","orcl","jdbc:oracle:thin:@192.168.10.1:1521:orcl","oracle.jdbc.driver.OracleDriver")
); */
}
}
类生成工厂:JavaSourceFactory.java
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
package net.liuzd.tools.db.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import net.liuzd.tools.db.dao.DbDAO;
import net.liuzd.tools.db.entity.Comments;
import net.liuzd.tools.db.entity.JavaDataBaseBean;
import net.liuzd.tools.db.entity.JdbcInfo;
public class JavaSourceFactory{
/**
* 生成用户名下所有的类文件,默认表名就是类名(首字母大写)
* @param packageName 包名
* @param classFilePath 生成java类的路径
* */
public static void create(String packageName,String classFilePath){
String [] tableNames = null;
createAll(new JavaDataBaseBean(packageName,classFilePath,tableNames));
}
/**
* 生成用户名下所有的类文件,默认表名就是类名(首字母大写)
* @param packageName 包名
* @param classFilePath 生成java类的路径
* @param tableNames 生成指定java类的表名集合
* */
public static void create(String packageName,String classFilePath,String ...tableNames){
createAll(new JavaDataBaseBean(packageName,classFilePath,tableNames));
}
/**
* 生成用户名下所有的类文件,默认表名就是类名(首字母大写)
* @param packageName 包名
* @param classFilePath 生成java类的路径
* @param jdbc 指定读取用户的表
* */
public static void create(String packageName,String classFilePath,JdbcInfo jdbc){
String [] tableNames = null;
createAll(new JavaDataBaseBean(packageName,classFilePath,jdbc,tableNames));
}
/**
* 生成用户名下所有的类文件,默认表名就是类名(首字母大写)
* @param packageName 包名
* @param classFilePath 生成java类的路径
* @param jdbc 指定读取用户的表
* @param tableNames 生成指定表的java类
* */
public static void create(String packageName,String classFilePath,JdbcInfo jdbc,String ...tableNames){
createAll(new JavaDataBaseBean(packageName,classFilePath,jdbc,tableNames));
}
/**
* 根据传递的表名生成类名,默认表名就是类名(首字母大写)
* @param packageName 包名
* @param classFilePath 生成java类的路径
@param tableNames 表名集合
* */
private static void createAll(JavaDataBaseBean jdb){
long startTime = System.currentTimeMillis();
String packageName = jdb.getPackageName();
String classFilePath = jdb.getClassFilePath();
String [] tableNames = jdb.getTableNames();
checkNull(packageName,classFilePath);
//组建查询参数
String queryTableNames = JavaUtils.createQueryParams(tableNames);
String userName = jdb.getJdbcInfo().getUsername().toUpperCase();
JavaUtils.showInfo("开始访问数据库,获取表相关的列名...");
Map<String,List<Comments>> userMap = DbDAO.getCommetnsByTableNames(userName, queryTableNames, jdb.getJdbcInfo());
JavaUtils.showInfo("获取表相关的表名成功,总共获取表个数: " + userMap.size());
String className = null;
int i = 0;
for(Map.Entry<String,List<Comments>> entity : userMap.entrySet()){
className = JavaUtils.oneStringToUpperCase(entity.getKey());
//组合java类文件内容
List<Comments> comments = entity.getValue();
String javaSource = create(comments,className,jdb);
create(javaSource,className,classFilePath);
JavaUtils.showInfo("表名: "+entity.getKey().toUpperCase()+",生成第"+(++i)+"个Java类文件成功...");
}
long endTime = System.currentTimeMillis();
JavaUtils.showInfo("总共生成了"+(i)+"个Java类文件,耗时"+(endTime-startTime)+"毫秒!");
}
/**
* @param tableName 表名
* @param packageName 包名
* @param className 类名
* @param createClassFilePath Java类生成路径
* */
public static void create(String tableName,String packageName,String className,String createClassFilePath){
checkNull(tableName,packageName,className,createClassFilePath);
create(new JavaDataBaseBean(tableName,packageName,className,createClassFilePath));
}
private static void checkNull(String ...params){
for(String param : params){
if(null == param || "".equals(param)){
throw new NullPointerException("亲爱的,不要传递空的字符串给我好吗?真受不了你了!!!");
}
}
}
/**
* @param username 用户名
* @param password 密码
* @param url 连接字符串
* @param driver 驱动名
* @param tableName 表名
* @param packageName 包名
* @param className 类名
* @param createClassFilePath Java类生成路径
* */
public static void create(String userName,String password,String url,String driver,String tableName,String packageName,String className,String createClassFilePath){
checkNull(userName,password,url,driver,tableName,packageName,className,createClassFilePath);
create(new JavaDataBaseBean(userName,password,url,driver,tableName,packageName,className,createClassFilePath));
}
/**
* 请务必传递四个必备参数
* tableName 表名,packageName 包名,className 类名,createClassFilePath Java类生成路径
* 否则请使用其它方法参数
* */
public static void create(JavaDataBaseBean javaBaseBean){
if(null == javaBaseBean){
throw new RuntimeException("亲爱的,不要传递空的对象给我好吗?你太讨厌了,真的,烦!!!");
}
//调用DAO获取表的相关列名与注释
String userName = javaBaseBean.getJdbcInfo().getUsername().toUpperCase();
String tableName = javaBaseBean.getTableName().toUpperCase();
String createClassFilePath = javaBaseBean.getClassFilePath();
JavaUtils.showInfo("开始访问数据库,获取表相关的列名...");
List<Comments> comments = DbDAO.getCommetnsByTableName(userName,tableName,javaBaseBean.getJdbcInfo());
JavaUtils.showInfo("获取表相关的列名成功,总共列数: " + comments.size());
//生成java类文件
String className = javaBaseBean.getClassName();
//组合java类文件内容
JavaUtils.showInfo("开始组装Java类文件内容...");
String javaSource = create(comments,className,javaBaseBean);
JavaUtils.showInfo("开始生成Java类文件...");
create(javaSource,className,createClassFilePath);
JavaUtils.showInfo("生成Java类文件成功...");
}
/**
* 根据字段名与注释生成java源代码
* @param comments 包含java的字段名及注释
* @param className java类名
* @param javaDb java原信息
* @return
*/
private static String create(List<Comments> comments,String className,JavaDataBaseBean javaDb) {
StringBuilder javaBean = new StringBuilder();
// 创建包名
javaBean.append("package ").append(javaDb.getPackageName()).append(";");
javaBean.append(JavaUtils.LINE).append(JavaUtils.LINE);
// 创建头部声明
javaBean.append("import org.apache.commons.lang.builder.ToStringBuilder;");
javaBean.append(JavaUtils.LINE).append(JavaUtils.LINE);
// 创建类名
javaBean.append("public class ").append(className).append(" ").append("implements java.io.Serializable{");
javaBean.append(JavaUtils.LINE).append(JavaUtils.LINE);
javaBean.append(JavaUtils.FILESPACE).append("private static final long serialVersionUID = 1L;");
javaBean.append(JavaUtils.LINE).append(JavaUtils.LINE);
//生成默认空的构造函数
javaBean.append(JavaUtils.FILESPACE).append("public").append(JavaUtils.FILESPACEONE).append(className).append("(){");
javaBean.append(JavaUtils.LINE).append(JavaUtils.LINE);
javaBean.append(JavaUtils.FILESPACE).append("}");
javaBean.append(JavaUtils.LINE).append(JavaUtils.LINE);
//创建构造函数中的全字段名
StringBuilder filedNames = new StringBuilder();
//构建构造函数中的设置值
StringBuilder setBeanValues = new StringBuilder();
for (Comments comment : comments) {
String fileType = comment.getFiledType();
String columnName = comment.getColumn_name().toLowerCase();
filedNames.append(fileType).append(JavaUtils.FILESPACEONE).append(columnName).append(",");
setBeanValues.append(JavaUtils.FILESPACE).append(JavaUtils.FILESPACE).append("this.").append(columnName)
.append(JavaUtils.FILESPACEONE).append("=").append(JavaUtils.FILESPACEONE).append(columnName).append(";")
.append(JavaUtils.LINE);
//创建字段及相应注释
javaBean.append(createContext(comment,fileType,columnName,className));
}
javaBean.append(JavaUtils.LINE).append(JavaUtils.FILESPACE);
javaBean.append("public").append(JavaUtils.FILESPACEONE).append(className).append("(");
//去除最后一个逗号
javaBean.append(JavaUtils.delLastComma(filedNames.toString())).append("){")
.append(JavaUtils.LINE)
.append(setBeanValues.toString())
.append(JavaUtils.FILESPACE).append("}").append(JavaUtils.LINE).append(JavaUtils.FILESPACE);
//toString
javaBean.append(JavaUtils.LINE).append(JavaUtils.FILESPACE);
javaBean.append("@Override public String toString() {");
javaBean.append(JavaUtils.LINE);
javaBean.append(JavaUtils.FILESPACE).append(JavaUtils.FILESPACE).append("return ToStringBuilder.reflectionToString(this);");
javaBean.append(JavaUtils.LINE);
javaBean.append(JavaUtils.FILESPACEONE).append(JavaUtils.FILESPACE).append("}");
javaBean.append(JavaUtils.LINE);
javaBean.append("}");
return javaBean.toString();
}
private static String createContext(Comments comment,String fileType,String columnName,String className) {
StringBuffer filedContext = new StringBuffer();
String comments = comment.getComments();
String methoColumnName = JavaUtils.oneStringToUpperCase(columnName);
if(null != comments){
/**表字段名称为: 相应注释为:单位信息表自编号*/
filedContext.append(JavaUtils.FILESPACE).append("/** 表字段名称为:").append(columnName)
.append(" 相应注释为:").append(comments).append(" */");
}
filedContext.append(JavaUtils.LINE);
filedContext.append(JavaUtils.FILESPACE).append("private").append(JavaUtils.FILESPACEONE).append(fileType).append(JavaUtils.FILESPACEONE).append(columnName).append(JavaUtils.FILESPACEONE).append("=").append(JavaUtils.FILESPACEONE).append("null;");
filedContext.append(JavaUtils.LINE).append(JavaUtils.LINE);
//读取方法的注释
if(null != comments){
filedContext.append(JavaUtils.FILESPACE).append("/** 获取").append(comments).append(" */").append(JavaUtils.LINE);
}
filedContext.append(JavaUtils.FILESPACE).append("public").append(JavaUtils.FILESPACEONE).append(fileType).append(" get")
.append(methoColumnName).append("(){");
filedContext.append(JavaUtils.LINE);
filedContext.append(JavaUtils.FILESPACE).append(JavaUtils.FILESPACE).append("return this.").append(comment.getColumn_name().toLowerCase()).append(";")
.append(JavaUtils.LINE)
.append(JavaUtils.FILESPACE).append("}")
.append(JavaUtils.LINE).append(JavaUtils.LINE);
//设置值的注释
if(null != comments){
filedContext.append(JavaUtils.FILESPACE).append("/**设置").append(comments).append("*/").append(JavaUtils.LINE);
}
filedContext.append(JavaUtils.FILESPACE).append("public").append(JavaUtils.FILESPACEONE).append(className).append(JavaUtils.FILESPACEONE).append("set")
.append(methoColumnName).append("(")
.append(fileType).append(JavaUtils.FILESPACEONE).append(columnName).append("){")
.append(JavaUtils.LINE).append(JavaUtils.FILESPACE).append(JavaUtils.FILESPACE)
.append("this.").append(columnName).append(" = ").append(columnName).append(";")
.append(JavaUtils.LINE).append(JavaUtils.FILESPACE).append(JavaUtils.FILESPACE)
.append("return this;")
.append(JavaUtils.LINE).append(JavaUtils.FILESPACE)
.append("}")
.append(JavaUtils.LINE).append(JavaUtils.LINE);
return filedContext.toString();
}
/**
* 根据java源代码生成java类文件
* @param javaSource java源代码内容
* @param className java类名称
* @param createClassFilePath java类生成路径
*/
public static void create(String javaSource,String className,String createClassFilePath){
File mkDirFile = new File(createClassFilePath);
if(!mkDirFile.exists()){
mkDirFile.mkdirs();
}
String filePathName = className+".java";
JavaUtils.showInfo("路径: " + mkDirFile.getAbsolutePath() + ",className: " + filePathName);
File javaSourceFile = new File(mkDirFile,filePathName);
if(javaSourceFile.exists()){
javaSourceFile.delete();
}
try {
java.io.FileWriter fw = new FileWriter(javaSourceFile);
fw.write(javaSource);
fw.flush();
fw.close();
} catch (IOException e) {
throw new RuntimeException("生成Java类文件出错..." + e.getMessage(), e);
}
JavaUtils.showInfo("Java类生成"+(javaSourceFile.exists() ? "成功" : "失败")+",路径:" + javaSourceFile.getAbsolutePath());
}
}
jdbc.properties属性文件内容如下:
#注意事项,以下这四个名称不能修改
username=xx
password=xx
url=jdbc:oracle:thin:@192.168.10.1:1521:orcl
driver=oracle.jdbc.driver.OracleDriver
ConnectionUtils.java
package net.liuzd.tools.db.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import net.liuzd.tools.db.entity.JdbcInfo;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class ConnectionUtils {
private ConnectionUtils() {
}
/**
* 正式使用这个连接
* */
public static Connection getConnectionFactoy(JdbcInfo jdbc) throws ClassNotFoundException, SQLException{
Class.forName(jdbc.getDriver());
return DriverManager.getConnection(
jdbc.getUrl(),
jdbc.getUsername(),
jdbc.getPassword()
);
}
}
DbDAO.java
package net.liuzd.tools.db.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.liuzd.tools.db.entity.Comments;
import net.liuzd.tools.db.entity.JdbcInfo;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class DbDAO {
/**
* @param userName 用户名
* @param tableName 表名
* @param JdbcInfo 数据连接方式
* @return
*/
public static List<Comments> getCommetnsByTableName(String userName,String tableName,JdbcInfo jdbc) {
List<Comments> lists = new ArrayList<Comments>();
String sql = "select * from DBA_COL_COMMENTS t where t.owner=? and t.table_name=? order by t.column_name";
java.sql.Connection conn = null;
java.sql.PreparedStatement psmt = null;
java.sql.ResultSet rs = null;
try {
conn = ConnectionUtils.getConnectionFactoy(jdbc);
psmt = conn.prepareStatement(sql);
psmt.setString(1, userName.trim());
psmt.setString(2, tableName.trim());
rs = psmt.executeQuery();
while (rs.next()) {
lists.add(new Comments(
rs.getString("owner"),
rs.getString("table_name"),
rs.getString("column_name"),
rs.getString("comments"))
);
}
} catch (SQLException e) {
throw new RuntimeException("获取数据出错..." + e.getMessage(), e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("未能找到驱动文件..." + e.getMessage(), e);
} finally {
try {
if (null != rs) {
rs.close();
rs = null;
}
if (null != psmt) {
psmt.close();
psmt = null;
}
if (null != conn) {
conn.close();
conn = null;
}
} catch (SQLException e) {
throw new RuntimeException("关闭数据库连接出错..." + e.getMessage(), e);
}
}
return lists;
}
/**
* @param userName 用户名
* @param tableName 表名集合
* @param JdbcInfo 数据连接方式
* @return
*/
public static Map<String,List<Comments>> getCommetnsByTableNames(String userName,String tableNameInfos,JdbcInfo jdbc) {
Map<String,List<Comments>> userMap = new HashMap<String,List<Comments>>();
String sql = "select * from DBA_COL_COMMENTS t where t.owner=?";
if(null != tableNameInfos && !"".equals(tableNameInfos)){
sql += " and t.table_name in("+tableNameInfos+") ";
}
sql += " order by t.table_name,t.column_name";
java.sql.Connection conn = null;
java.sql.PreparedStatement psmt = null;
java.sql.ResultSet rs = null;
try {
conn = ConnectionUtils.getConnectionFactoy(jdbc);
psmt = conn.prepareStatement(sql);
psmt.setString(1, userName.trim());
rs = psmt.executeQuery();
String tableName = null;
while (rs.next()) {
tableName = rs.getString("table_name");
List<Comments> lists = userMap.get(tableName);
if(null == lists){
lists = new ArrayList<Comments>();
}
lists.add(new Comments(
rs.getString("owner"),
tableName,
rs.getString("column_name"),
rs.getString("comments"))
);
userMap.put(tableName, lists);
}
} catch (SQLException e) {
throw new RuntimeException("获取数据出错..." + e.getMessage(), e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("未能找到驱动文件..." + e.getMessage(), e);
} finally {
try {
if (null != rs) {
rs.close();
rs = null;
}
if (null != psmt) {
psmt.close();
psmt = null;
}
if (null != conn) {
conn.close();
conn = null;
}
} catch (SQLException e) {
throw new RuntimeException("关闭数据库连接出错..." + e.getMessage(), e);
}
}
return userMap;
}
}
JavaDataBaseBean.java
package net.liuzd.tools.db.entity;
import net.liuzd.tools.db.util.JdbcFactory;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class JavaDataBaseBean {
private String [] tableNames = null;
private String tableName = null;
private String packageName = null;
private String className = null;
private String classFilePath = null;
private JdbcInfo jdbcInfo = null;
public String getClassName() {
return className;
}
public JavaDataBaseBean setClassName(String className) {
this.className = className;
return this;
}
/**
* 默认读取位于classpath下的jdbc.properties属性文件
* */
public JavaDataBaseBean(){
setJdbcInfo(JdbcFactory.getJdbcInfo());
}
public JavaDataBaseBean(String packageName,String classFilePath,String ...tableNams){
this();
this.packageName = packageName;
this.classFilePath = classFilePath;
this.tableNames = tableNams;
}
public JavaDataBaseBean(String packageName,String classFilePath,JdbcInfo jdbc,String ...tableNams){
this.packageName = packageName;
this.classFilePath = classFilePath;
this.jdbcInfo = jdbc;
this.tableNames = tableNams;
}
/**
*
* 默认生成的类名为首字母大写之后为小写
* 默认生成的包名为: net.liuzd.tools.db
* */
public JavaDataBaseBean(String tableName){
this();
this.tableName = tableName;
}
/**
* 默认读取位于classpath下的jdbc.properties属性文件
* @param tableName 表名
* @param packageName 包名
* @param className 类名
* */
public JavaDataBaseBean(String tableName,
String packageName,String className) {
this(tableName);
this.className = className;
this.packageName = packageName;
}
public JavaDataBaseBean(String tableName,
String packageName,String className,String classFilePath) {
this(tableName,packageName,className);
this.classFilePath = classFilePath;
}
/**
* @param username 用户名
* @param password 密码
* @param url 连接字符串
* @param driver 驱动名
* @param tableName 表名
* @param packageName 包名
* @param className 类名
* */
public JavaDataBaseBean(String username,String password,String url,String driver,String tableName,
String packageName,String className) {
this.tableName = tableName;
this.className = className;
this.packageName = packageName;
jdbcInfo = new JdbcInfo(username,password,url,driver);
}
public JavaDataBaseBean(String username,String password,String url,String driver,String tableName,
String packageName,String className,String classFilePath) {
this(username,password,url,driver,tableName,packageName,className);
this.classFilePath = classFilePath;
}
public String getTableName() {
return tableName;
}
public JavaDataBaseBean setTableName(String tableName) {
this.tableName = tableName;
return this;
}
public String getPackageName() {
return packageName;
}
public JavaDataBaseBean setPackageName(String packageName) {
this.packageName = packageName;
return this;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public JdbcInfo getJdbcInfo() {
return jdbcInfo;
}
public JavaDataBaseBean setJdbcInfo(JdbcInfo jdbcInfo) {
this.jdbcInfo = jdbcInfo;
return this;
}
public String getClassFilePath() {
return classFilePath;
}
public JavaDataBaseBean setClassFilePath(String classFilePath) {
this.classFilePath = classFilePath;
return this;
}
public String[] getTableNames() {
return tableNames;
}
public JavaDataBaseBean setTableNames(String[] tableNames) {
this.tableNames = tableNames;
return this;
}
}
JdbcInfo.java
package net.liuzd.tools.db.entity;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class JdbcInfo {
private String username;
private String password;
private String url;
private String driver;
public JdbcInfo(){
}
public JdbcInfo(String username, String password, String url, String driver) {
super();
this.username = username;
this.password = password;
this.url = url;
this.driver = driver;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
JavaUtils.java
package net.liuzd.tools.db.util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class JavaUtils {
public static final String FILESPACE = " ";
public static final String FILESPACEONE = " ";
public static final String LINE = "\n";
public static final String TIME = "yyyy-MM-dd hh:mm:ss";
/**
* 获取当前的时间
* */
public static String getCurrentDate(){
SimpleDateFormat sdf = new SimpleDateFormat(TIME);
return sdf.format(new Date());
}
/**
* 首字母设置为大写
* */
public static String oneStringToUpperCase(String param){
String temp = param;
if(null != temp && !"".equals(temp) && temp.length() > 0){
temp = temp.toLowerCase();
String one = temp.substring(0,1).toUpperCase();
String endStr = temp.substring(1,temp.length());
temp = one + endStr;
}
return temp;
}
/**
*组建查询SQL,用于in()中的参数查询
* */
public static String createQueryParams(String ...tableNames){
if(null == tableNames || tableNames.length == 0){
return null;
}
StringBuilder querySql = new StringBuilder();
for(String tableName : tableNames){
querySql.append("'").append(tableName).append("'").append(",");
}
String tempSql = delLastComma(querySql.toString());
return tempSql;
}
/**
* 去掉最后一个逗号
* */
public static String delLastComma(String value){
if(null == value || "".equals(value)){
return null;
}
if(value.endsWith(",")){
value = value.substring(0,value.length()-1);
}
return value;
}
public static void showInfo(String message){
System.out.println(getCurrentDate() + FILESPACEONE + message);
}
}
JdbcFactory.java
package net.liuzd.tools.db.util;
import net.liuzd.tools.db.entity.JDBCENUM;
import net.liuzd.tools.db.entity.JdbcInfo;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class JdbcFactory {
public static JdbcInfo jdbc = null;
private JdbcFactory(){
}
static{
jdbc = new JdbcInfo(
ReadProperty.getPropertyByKey(JDBCENUM.USERNAME),
ReadProperty.getPropertyByKey(JDBCENUM.PASSWORD),
ReadProperty.getPropertyByKey(JDBCENUM.URL),
ReadProperty.getPropertyByKey(JDBCENUM.DRIVER)
);
}
public static JdbcInfo getJdbcInfo(){
return jdbc;
}
}
ReadProperty.java
package net.liuzd.tools.db.util;
import java.io.InputStream;
import java.util.Properties;
import net.liuzd.tools.db.entity.JDBCENUM;
/**
* @author liuzd
* @version 1.0
* @since 1.5
* @time 2011-09-23
* 网名:咫尺天涯
* 博客:http://liuzidong.iteye.com/
*/
public class ReadProperty {
private static Properties beanProps = new Properties();
private static final String BEANPROPERTIES = "jdbc.properties";
private Properties userProperties = null;
static {
try {
InputStream inStream = ReadProperty.class.getClassLoader()
.getResourceAsStream(BEANPROPERTIES);
beanProps.load(inStream);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
public static Properties getProperty() {
return beanProps;
}
public static String getPropertyByKey(String key) {
return beanProps.getProperty(key);
}
public static String getPropertyByKey(JDBCENUM jdbcEnum) {
return getPropertyByKey(jdbcEnum.name().toLowerCase());
}
@SuppressWarnings("unused")
private ReadProperty(){
}
/**
* 特别为用户刘下一个接口,传递属性文件名称进入读取
* */
public ReadProperty(String propertiesName){
userProperties = new Properties();
try {
InputStream inStream = ReadProperty.class.getClassLoader()
.getResourceAsStream(propertiesName);
userProperties.load(inStream);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
/**
* 允许用户调用进行读取属性文件中的值
* */
public String getValueByProperties(String propertiesName){
return userProperties.getProperty(propertiesName);
}
}
附件是打包好了的jar及工程文件
- 大小: 27.7 KB
- 大小: 15.2 KB
- LZDTools_project_.rar (27.5 KB)
- 下载次数: 0
- net.liuzd.tools.tabletoclass.jar (15 KB)
- 下载次数: 0