/**
* Title:
* Description:
* Copyright: Copyright (c) 2009
* Company: Sangame.com
* @author liuyi
* @version 1.0
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class PojoFactory {
//定义生成文件的包路径
private static String pack = "com.jimmy.models";
//定义要反射出的数据库
// private static String driver = "com.mysql.jdbc.Driver";
// private static String url = "jdbc:mysql://localhost:3306/sailing";
// private static String username = "root";//mksp
// private static String password = "root";//sangame.com
private static String driver = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@192.168.1.110:1521:sgdb";//mksp@
private static String username = "fetion";//mksp
private static String password = "fetion";//sangame.com
public static void main(String[] args) {
new PojoFactory().writeEtmvcPojo();//创建Etmvc的pojo文件,需要数据库驱动包以及et-ar.jar
//new PojoFactory().writePojo();//创建原始的pojo文件,需数据库驱动包
}
public void writeEtmvcPojo() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = conn.getMetaData();
if("com.mysql.jdbc.Driver".equals(driver)){
String[] types = { "TABLE" };
ResultSet resultSet = dbmd.getTables(null, "mksp", "%", types);
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
System.out.println(tableName);
String className = String.valueOf(tableName.charAt(0))
.toUpperCase()
+ tableName.substring(1);
String sql = "select * from " +tableName;
System.out.println(sql);
ResultSetMetaData rsmd = null;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
rsmd = rs.getMetaData();
File path = new File(System.getProperty("user.dir")+"\\src\\"+pack.replace('.', '\\'));
//String path = filePath.toString()+"\\src\\"+pack.replace('.', '\\')+"\\";
System.out.println("path================="+path);
if (!path.exists()) {
path.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path+"\\" + className
+ ".java", false);
StringBuilder sb = new StringBuilder();
StringBuilder one = new StringBuilder();
StringBuilder two = new StringBuilder();
StringBuilder three = new StringBuilder();
one.append("package "+pack+";\r\n");
one.append("/**" +"\r\n"
+ " * Title:
"+"\r\n"
+ " * Company: Sangame.com
"+"\r\n"
+ " * @author liuyi"+"\r\n"
+ " * @version 1.0"+"\r\n"
+ " */"+"\r\n");
one.append("import com.et.ar.ActiveRecordBase;" + "\r\n"
+ "import com.et.ar.annotations.Column;" + "\r\n"
+ "import com.et.ar.annotations.Id;" + "\r\n"
+ "import com.et.ar.annotations.Table;" + "\r\n");
two.append("\r\n\r\n@Table(name=\"" + tableName + "\")\r\n");
two.append("public class " + className
+ " extends ActiveRecordBase {" + "\r\n");
String timestamp = "";
String date = "";
String time = "";
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String s = rsmd.getColumnTypeName(i);
String type = s;
if (s.indexOf(" ") != -1) {// 防止取出的类似type====INTEGER
// UNSIGNED
type = s.substring(0, s.indexOf(" "));
}
String name = rsmd.getColumnName(i).toLowerCase();
System.out.println("type====" + type);
System.out.println("name====" + name);
ResultSet pkRSet = dbmd.getPrimaryKeys(null, null,
tableName);
String keyName = "";
// 取出表中的主键名字
while (pkRSet.next()) {
keyName = pkRSet.getObject("COLUMN_NAME").toString();
}
System.out.println("keyName=====" + keyName);
String sid = "Column";
if (keyName.equals(name)) {
sid = "Id";
}
if (type.trim().equalsIgnoreCase("Integer")){
type = "Integer";
} else if (type.trim().equalsIgnoreCase("Varchar")) {
type = "String";
} else if (type.trim().equalsIgnoreCase("BIGINT")) {
type = "Long";
} else if (type.trim().equalsIgnoreCase("DOUBLE")) {
type = "Double";
} else if (type.trim().equalsIgnoreCase("FLOAT")) {
type = "Float";
} else if (type.trim().equalsIgnoreCase("Timestamp")) {
type = "Timestamp";
timestamp = "yes";
} else if (type.trim().equalsIgnoreCase("Date")) {
type = "Date";
date = "yes";
} else if (type.trim().equalsIgnoreCase("Datetime")) {
type = "Timestamp";
timestamp = "yes";
} else if (type.trim().equalsIgnoreCase("TIME")) {
type = "Time";
time = "yes";
} else if (type.trim().equalsIgnoreCase("YEAR")) {
type = "Date";
date = "yes";
} else {
type = "String";
}
String outs = " @" + sid + " private " + type + " "
+ name + ";\r\n";
two.append(outs);
// get set
// 首字母转大写
String name2 = String.valueOf(name.charAt(0)).toUpperCase()
+ name.substring(1).toLowerCase();
String set = " public void set" + name2 + "(" + type
+ " " + name + "){\r\n" + " this." + name
+ " = " + name + ";\r\n" + " }\r\n";
String get = " public " + type + " get" + name2
+ "(){\r\n" + " return " + name + ";\r\n"
+ " }\r\n";
three.append(get);
three.append(set);
}
if (timestamp.equals("yes")) {
one.append("import java.sql.Timestamp;" + "\r\n");
}
if (time.equals("yes")) {
one.append("import java.sql.Time;" + "\r\n");
}
if (date.equals("yes")) {
one.append("import java.sql.Date;" + "\r\n");
}
sb.append(one);
sb.append(two);
sb.append(three);
sb.append("}");
String temp = sb.toString();
fos.write(temp.getBytes());
fos.close();
}
}else if("oracle.jdbc.driver.OracleDriver".equals(driver)){
Statement st = conn.createStatement();
ResultSet resultSet = st.executeQuery("SELECT * FROM USER_ALL_TABLES");
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
System.out.println(tableName);
String className = String.valueOf(tableName.charAt(0))
.toUpperCase()
+ tableName.substring(1);
String sql = "select * from " +tableName;
System.out.println(sql);
ResultSetMetaData rsmd = null;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
rsmd = rs.getMetaData();
File path = new File(System.getProperty("user.dir")+"\\src\\"+pack.replace('.', '\\'));
//String path = filePath.toString()+"\\src\\"+pack.replace('.', '\\')+"\\";
System.out.println("path================="+path);
if (!path.exists()) {
path.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path+"\\" + className
+ ".java", false);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, Charset.forName("utf-8")));
StringBuilder sb = new StringBuilder();
StringBuilder one = new StringBuilder();
StringBuilder two = new StringBuilder();
StringBuilder three = new StringBuilder();
one.append("package "+pack+";\r\n");
one.append("/**" +"\r\n"
+ " * Title:
"+"\r\n"
+ " * Company: Sangame.com
"+"\r\n"
+ " * @author liuyi"+"\r\n"
+ " * @version 1.0"+"\r\n"
+ " */"+"\r\n");
one.append("import com.et.ar.ActiveRecordBase;" + "\r\n"
+ "import com.et.ar.annotations.Column;" + "\r\n"
//+ "import com.et.ar.annotations.Id;" + "\r\n"
+ "import com.et.ar.annotations.Table;" + "\r\n");
two.append("\r\n\r\n@Table(name=\"" + tableName + "\")\r\n");
two.append("public class " + className
+ " extends ActiveRecordBase {" + "\r\n");
String timestamp = "";
String date = "";
String id = "";
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String s = rsmd.getColumnTypeName(i);
String type = s;
// if (s.indexOf(" ") != -1) {// 防止取出的类似type====INTEGER
// // UNSIGNED
// type = s.substring(0, s.indexOf(" "));
// }
String name = rsmd.getColumnName(i).toLowerCase();
System.out.println("type====" + type);
System.out.println("name====" + name);
ResultSet pkRSet = dbmd.getPrimaryKeys(null, null,
tableName);
String keyName = "";
// 取出表中的主键名字
while (pkRSet.next()) {
keyName = pkRSet.getObject("COLUMN_NAME").toString();
}
System.out.println("keyName=====" + keyName);
String sid = "Column";
if (keyName.equals(name)) {
sid = "Id";
id = "yes";//有主键时设置id="yes"使程序自动导入import com.et.ar.annotations.Id;
}
if (type.trim().equalsIgnoreCase("Number")){
type = "Integer";
} else if (type.trim().equalsIgnoreCase("Varchar2")) {
type = "String";
} else if (type.trim().equalsIgnoreCase("SMALLINT")) {
type = "Short";
} else if (type.trim().equalsIgnoreCase("DOUBLE")) {
type = "Double";
} else if (type.trim().equalsIgnoreCase("REAL")) {
type = "Float";
} else if (type.trim().equalsIgnoreCase("Timestamp")) {
type = "Timestamp";
timestamp = "yes";
} else if (type.trim().equalsIgnoreCase("Date")) {
type = "Date";
date = "yes";
} else {
type = "String";
}
String outs = " @" + sid + " private " + type + " "
+ name + ";\r\n";
two.append(outs);
// get set
// 首字母转大写
String name2 = String.valueOf(name.charAt(0)).toUpperCase()
+ name.substring(1).toLowerCase();
String set = " public void set" + name2 + "(" + type
+ " " + name + "){\r\n" + " this." + name
+ " = " + name + ";\r\n" + " }\r\n";
String get = " public " + type + " get" + name2
+ "(){\r\n" + " return " + name + ";\r\n"
+ " }\r\n";
three.append(get);
three.append(set);
}
if (id.equals("yes")) {
one.append("import com.et.ar.annotations.Id;" + "\r\n");
}
if (timestamp.equals("yes")) {
one.append("import java.sql.Timestamp;" + "\r\n");
}
if (date.equals("yes")) {
one.append("import java.sql.Date;" + "\r\n");
}
sb.append(one);
sb.append(two);
sb.append(three);
sb.append("}");
String temp = sb.toString();
bw.write(temp);
/*fos.write(temp.getBytes());
fos.close();*/
bw.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
分享到:
相关推荐
这个"Owner"概念在C# WinForm的窗体调用中扮演着关键角色,它涉及到窗体的生命周期管理和事件处理。 首先,让我们理解“Owner”是什么。在C#中,当一个窗体创建另一个窗体时,新创建的窗体可以有一个“所有者”...
一、准备工作 配置DeviceAdmin ...Google没有提供类似获取设备管理器权限那样的api,所以个人设备的deviceowner权限一般是通过adb shell命令赋予的,而一些大公司是采用批量订购设备,定制room的方式获取DeviceOwner
在Android系统中,"device owner"是一个特殊的角色,赋予了应用程序至高无上的管理权限,类似于设备管理员(Device Administrator)。这个角色通常用于企业环境中,以便IT管理员能够对设备进行严格的控制,确保数据...
page_owner_sort
标题中的“DB_OWNER权限备份hta到启动项提权的小工具”指的是一个利用SQL Server数据库中的DB_OWNER角色权限,将HTA(HTML应用程序)文件备份并植入到系统启动项中的工具。这个工具可能被用于网络安全攻防演练或者...
一般按鈕贴图或图标都是把按钮风格设为BS_OWNERDRAW类型,然后重载DrawItem函数,进行自定义重绘。但是我觉得这样挺不好的。因为按钮一旦设为BS_OWNERDRAW,则他的各种GUI属性将全部丢失,比如按钮的外边框、阴影、...
### MSSQL Db_owner 角色注入直接获得系统权限解析 #### 概述 在MSSQL环境下,通过特定的技术手段可以实现对具有`db_owner`角色的账户进行注入操作,进而获取系统的高级权限。这种技术通常被应用于渗透测试或者...
关于Window自画控件CustomDraw,OwnerDraw的好文章
OWNER 是一个简单的 Java 对 Properties 文件的封装库,假设 properties 配置如下: port=80 hostname=foobar.com maxThreads=100 只需要简单的封装: public interface ServerConfig extends Config { int port...
《深入剖析Owner源码》 在软件开发领域,理解源码是提升技术能力的重要途径,尤其是在面对复杂的系统架构时。本篇文章将针对"owner-源码"进行深度解析,帮助读者掌握其背后的逻辑与设计思想。 "owner"一词在编程中...
在Windows编程中,自绘(Owner Draw)是一种技术,允许开发者对控件进行自定义绘制,以实现独特的视觉效果或特定的功能。在这个“ownerdraw_tab.zip”压缩包中,我们找到了一个关于自绘TAB控件的例子,这将帮助...
在Windows操作系统中,DB_OWNER是一个重要的SQL Server数据库角色,拥有者可以对数据库进行几乎所有的管理操作,包括备份、恢复、修改结构和数据等。 DB_OWNER权限: 1. **DB_OWNER**:在SQL Server中,这是数据库...
标题 "v1_beam_owner11j_ABAQUS_" 暗示了这是一个关于使用ABAQUS软件进行IPE梁的屈曲分析的项目。"owner11j"可能是项目所有者或负责人的标识,而"ABAQUS"是世界领先的有限元分析软件,常用于复杂的结构和动力学问题...
标题 "usb_usb_owner9cf_PIC_Microcontroller_源码" 暗示这是一个关于使用PIC微控制器实现USB功能的代码示例。PIC微控制器是由Microchip Technology公司生产的一系列广泛应用的微控制器,它们以其高效能和低功耗而...
《HP XP7 Owner Guide》是针对HP XP7磁盘阵列的操作指南,旨在为存储系统管理员或授权服务提供商提供详细的操作指导。该指南涵盖了HP XP7磁盘阵列的硬件描述、管理方法、控制面板及LED指示器的说明、故障排查以及...
根据给定文件“LVDS Owner’s Manual”的相关信息,本文将详细介绍LVDS(低电压差分信号)技术的核心概念、工作原理、拓扑结构、布局指南等,并涉及部分DVI(数字视讯接口)与HDMI(高清晰度多媒体接口)的相关知识...
在WPF(Windows Presentation Foundation)开发中,"owner"是一个重要的概念,特别是在创建和管理对话框(Dialog)时。对话框是一种特殊的窗口,通常用于获取用户输入或展示信息,如消息提示框(MessageBox)。...
"fileowner查询文件创建者/所有者"就是一个针对这种需求的工具或命令,主要用于域用户环境下的文件权限管理。 在Windows操作系统中,文件的所有权通常与用户账户关联,每个文件都有一个特定的创建者或所有者,这个...
dell笔记本电脑m7510 操作手册 dell笔记本电脑m7510 操作手册 dell笔记本电脑m7510 操作手册 dell笔记本电脑m7510 操作手册 dell笔记本电脑m7510 操作手册 英文版
【标题】"dml-master.zip_C语言_Kmeans_louvain_owner3v7_sheet4fu" 涉及到两个核心的图论与数据挖掘算法——K-Means聚类算法和Louvain方法,这两者都是社区检测或网络划分的重要工具,而它们在这里是以C语言实现的...