|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
/* * Created on 2004-10-15 * * Title: QuickCoder * Description: 一段简朴的根据SQLServer数据库表结构生成C#实体类的Java代码 曾经在学校听殷兆粼教授这样讲过,UML将来应该能够根据已经设计好的业务逻辑,自动生成数据库表和数据间的关联等,并生成实体代码,最好能够再生成部分业务逻辑代码。这样我们的设计就跟画画相同了,我们要做的就是画UML图。数据库结构、程序代码都自动生成。 这里是我在公司的一个项目中为了把数据库实体表生成C#实体而写的一段代码。功能不多,但是可以省敲很多代码。 * Company: http://www.hakatasoft.com * Author: Shuqun, Zhou * */ package coder; import java.io.*; import java.sql.*; import java.util.*; /** * @author Administrator * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class QuickCoder { public static Map MSSQLServerDataTypeCSharpTypeMap = new HashMap(); static { MSSQLServerDataTypeCSharpTypeMap.put("char", "string"); MSSQLServerDataTypeCSharpTypeMap.put("varchar", "string"); MSSQLServerDataTypeCSharpTypeMap.put("nvarchar", "string"); MSSQLServerDataTypeCSharpTypeMap.put("text", "string"); MSSQLServerDataTypeCSharpTypeMap.put("datetime", "System.DateTime"); MSSQLServerDataTypeCSharpTypeMap.put("int", "int"); MSSQLServerDataTypeCSharpTypeMap.put("int identity", "float"); MSSQLServerDataTypeCSharpTypeMap.put("float", "float"); MSSQLServerDataTypeCSharpTypeMap.put("bit", "string"); MSSQLServerDataTypeCSharpTypeMap.put("numeric", "double"); MSSQLServerDataTypeCSharpTypeMap.put("money", "double"); } public static void prt(Object o) { System.out.println(o); } public static void prt(int i) { System.out.println(i); } public static void table2CSharp(ResultSetMetaData rsmd, String namespace, String className) throws IOException, SQLException { StringBuffer cs = new StringBuffer(); String line = System.getProperty("line.separator"); cs.append("using System;").append(line).append(line); cs.append("/**").append(line); cs.append(" * Generate by QuickCoder.").append(line); cs.append(" * 使用前请修改文件名和类名(去掉`Auto_\\\'前缀)").append(line); cs.append(" */").append(line).append(line); cs.append("namespace ").append(namespace).append(" {").append(line); cs.append("/tpublic class ").append(className).append(" {").append(line); int c = rsmd.getColumnCount(); String dbType, type, name; Object t; for (int i = 1; i <= c; i++) { name = rsmd.getColumnLabel(i).replace(\\\' \\\', \\\'_\\\'); dbType = rsmd.getColumnTypeName(i); t = MSSQLServerDataTypeCSharpTypeMap.get(dbType); if (t != null) { type = t.toString(); } else { type = "____" + dbType; prt(namespace + "" + className + " " + dbType); } cs.append("/t/t").append(type) .append(" m_").append(name).append(";").append(line); /* 由于同组一工程师说这种C#的getter, setter写法不好,我就改成下面那种写法了 不过我认为他是在用Java的思想审阅C#,我找不到这样写的不好之处 cs.append("/t/tpublic ").append(type).append(" ").append(name).append(" ").append("{").append(line); cs.append("/t/t/tget {").append(line); cs.append("/t/t/t/treturn m_").append(name).append(";").append(line); cs.append("/t/t/t}").append(line); cs.append("/t/t/tset {").append(line); cs.append("/t/t/t/tm_").append(name).append(" = ").append("value;").append(line); cs.append("/t/t/t}").append(line); cs.append("/t/t}").append(line); */ cs.append(line); cs.append("/t/tpublic ").append(type).append(" Get").append(name).append("() {").append(line); cs.append("/t/t/treturn this.m_").append(name).append(";").append(line); cs.append("/t/t}").append(line); cs.append(line); cs.append("/t/tpublic void Set").append(name).append("(").append(type).append(" ").append(name).append(") {").append(line); cs.append("/t/t/tthis.m_").append(name).append(" = ").append(name).append(";").append(line); cs.append("/t/t}").append(line); if ("int".equals(type) || "float".equals(type) || "double".equals(type)) { cs.append(line); cs.append("/t/tpublic string").append(" Get").append(name).append("AsString() {").append(line); cs.append("/t/t/treturn this.m_").append(name).append(".ToString();").append(line); cs.append("/t/t}").append(line); cs.append(line); cs.append("/t/tpublic void Set").append(name).append("(string s) {").append(line); cs.append("/t/t/tthis.m_").append(name).append(" = ").append(type).append(".Parse(s);").append(line); cs.append("/t/t}").append(line); } cs.append(line); } cs.append("/t}").append(line); cs.append("}").append(line); //prt(cs); File file = new File("C:/tmp", className + ".cs"); FileOutputStream fos = new FileOutputStream(file); fos.write(cs.toString().getBytes()); fos.close(); } public static void main(String[] args) throws Exception { prt("Start"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:SCE", "SCE", "SCE"); Connection con2 = DriverManager.getConnection("jdbc:odbc:SCE", "SCE", "SCE"); Statement stmt = con2.createStatement(); DatabaseMetaData meta = con.getMetaData(); ResultSet rs = meta.getTables(null, null, null, null); String tableName, tableType; ResultSet rs2; while (rs.next()) { //rsmd; //System.out.println(rsmd.getColumnCount()); tableName = rs.getString("TABLE_NAME"); tableType = rs.getString("TABLE_TYPE"); if ("TABLE".equals(tableType) && tableName.startsWith("tb_")) { //prt(tableName); rs2 = stmt.executeQuery("SELECT * FROM [" + tableName + "]"); table2CSharp(rs2.getMetaData(), "Sce.Action.Objects", "Auto_O_" + tableName.substring(3, tableName.length()).replace(\\\'-\\\', \\\'_\\\')); rs2.close(); } } rs.close(); stmt.close(); con.close(); con2.close(); prt("End"); } } 返回类别: 教程 上一教程: java虚拟机学习笔记1 下一教程: java迈步从头越(java环境配置) 您可以阅读与"一段简朴的根据SQLServer数据库表结构生成C#实体类的Java代码"相关的教程: · 关于JAVA中连接各类数据库的代码及其补充说明 · JAVA数据库接口JDBC基础教程之驱动设置 · JSP+JAVABEAN开发模式下,数据库SQL语句的编写规范 · 使用hibernate扩展工具hbm2java根据配置文件生成持久化对象类(2.1.2) · 一个操作数据库的Java Bean…… |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |