[jboss-user] [EJB 3.0] - there is a very simple entity bean generator for mysql

johnhollon do-not-reply at jboss.com
Fri Mar 2 08:49:29 EST 2007


  | package com.jdi.dbport.utils;
  | import java.sql.*;
  | import java.io.*;
  | 
  | /**
  |  * Create Entity Bean (byte code) from existing tables in database.
  |  * This generator can only generate basic code for programming, so 
  |  * you should optimize the generated code.
  |  * before you use this tool, be sure that there is a folder called
  |  * "001_JDI_Generate_EntityBean_TEMP" in the root of C hard driver.
  |  * @author CHR
  |  *
  |  */
  | 
  | public class GenerateEntityBeans {
  | 	private static String packageName = "com.jdi.persistence";
  | 	private static String directory = "C:\\001_JDI_Generate_EntityBean_TEMP\\";
  | 	
  | 	StringBuffer start = new StringBuffer()
  | 	.append("/** Generated Entity Bean - YOU SHOULD OPTIMIZE IT - Copyright (C) ??? */\n");
  | 	
  | 	StringBuffer packagingAndImporting = new StringBuffer()
  | 	.append("package " + packageName + ";" + "\n" +
  | 			"import javax.persistence.*; \n" +
  | 			"import java.util.*; \n" +
  | 			"import java.sql.*; \n" +
  | 			"import java.math.*;\n\n");
  | 	
  | 	private StringBuffer doGenerateClassName(String tablename,String classname){
  | 			String classdeclaring = 
  | 				"@Entity \n " +
  | 				"@Table(name=\"" + tablename + "\") \n " +
  | 				"public class " + classname +"{\n";
  | 			StringBuffer mainclasstext = new StringBuffer().append(start).append(packagingAndImporting);
  | 			mainclasstext.append(classdeclaring);
  | 			return mainclasstext;
  | 	}
  | 	
  | 	private StringBuffer doGenerateProperties(String columnname, String datatype, String keytype, 
  | String nullable, String defaultvalue){
  | 		String useddatatype;
  | 		String compareString = datatype.concat("111111111");
  | 		//Integer,Int,Mediumint,tinyint,smallint as int
  | 		if(compareString.substring(0,7).equalsIgnoreCase("Integer")||compareString.substring(0,3).equalsIgnoreCase("Int")||
  | 				compareString.substring(0,9).equalsIgnoreCase("MEDIUMINT")||compareString.substring(0,8).equalsIgnoreCase("SMALLINT")||
  | 				compareString.substring(0,7).equalsIgnoreCase("TINYINT"))
  | 			useddatatype = "int";
  | 		//bigint as long
  | 		else if(compareString.substring(0,6).equalsIgnoreCase("BIGINT"))
  | 			useddatatype = "long";
  | 		//float,double,decimal,real,numeric as double
  | 		else if(compareString.substring(0,5).equalsIgnoreCase("FLOAT")||compareString.substring(0,6).equalsIgnoreCase("DOUBLE")||
  | 				compareString.substring(0,7).equalsIgnoreCase("DECIMAL")||compareString.substring(0,4).equalsIgnoreCase("REAL")||
  | 				compareString.substring(0,7).equalsIgnoreCase("NUMERIC"))
  | 			useddatatype = "double";
  | 		//char, varchar, date, datetime, timestamp as String
  | 		else if(compareString.substring(0,4).equalsIgnoreCase("CHAR")||compareString.substring(0,7).equalsIgnoreCase("VARCHAR")||
  | 				compareString.substring(0,4).equalsIgnoreCase("DATE") ||compareString.substring(0,8).equalsIgnoreCase("DATETIME") ||
  | 				compareString.substring(0,9).equalsIgnoreCase("TIMESTAMP"))
  | 			useddatatype = "String";
  | 		else useddatatype = "String";
  | 		String propertiesclaring1 = "\tprivate " + useddatatype + " " + columnname + ";\n";
  | 		String propertiesclaring2 = "\t at Column(name=\""+ columnname +"\")\n";
  | 		String propertiesgetter = "\tpublic " + useddatatype + " get" + columnname + "(){\n\t\treturn " + columnname +";\n\t}\n";
  | 		String propertiessetter = "\tpublic void " + "set" + columnname + "(" + useddatatype + " " + columnname + "){\n\t\tthis." + columnname + " = "	+ columnname + ";\n\t}\n\n";
  | 		String propertiesID = "\t at Id\n\t at GeneratedValue\n";
  | 		StringBuffer propertiesText = new StringBuffer().append(propertiesclaring1);
  | 		if(keytype!=null && keytype.equalsIgnoreCase("PRI"))
  | 			propertiesText.append(propertiesID);
  | 		propertiesText.append(propertiesclaring2).append(propertiesgetter).append(propertiessetter);
  | 		return propertiesText;
  | 	}
  | 	
  | 	private StringBuffer doGenerateJavaCode(Connection conn, String tablename,String classname){
  | 		StringBuffer javaCode = new StringBuffer();
  | 		javaCode.append(doGenerateClassName(tablename,classname));
  | 		String querycolumns = "show columns from " + tablename;
  | 		try{
  | 			Statement stmt = conn.createStatement();
  | 			ResultSet rs = stmt.executeQuery(querycolumns);
  | 			while(rs.next()){
  | 				javaCode.append(doGenerateProperties(rs.getString("Field"),rs.getString("Type"),
  | 						rs.getString("Key"),rs.getString("Null"),rs.getString("Default")));
  | 			}
  | 			rs.close();
  | 			stmt.close();
  | 			javaCode.append("\n}");
  | 			
  | 		}catch(Exception e){
  | 			System.out.println(e.getMessage());
  | 		}
  | 		return javaCode;	
  | 	}
  | 	
  | 	private void writeToFile(StringBuffer sb, String filename){
  | 		try{
  | 			File out = new File(filename);
  | 			FileWriter fw = new FileWriter(out);
  | 			for (int i = 0; i < sb.length(); i++){
  | 				char c = sb.charAt(i);
  | 				fw.write(c);
  | 			}
  | 			fw.flush();
  | 			fw.close();
  | 		}catch(Exception e){
  | 			System.out.println(e.getMessage());
  | 		}
  | 	}
  | 	public GenerateEntityBeans (Connection conn, String tablename, String classname, String directory){
  | 		writeToFile(doGenerateJavaCode(conn,tablename,classname),directory);
  | 	}
  | 	public static void main(String args[]){
  | 		try{
  | 			Class.forName("com.mysql.jdbc.Driver").newInstance();
  | 			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdi","jdi","jdi");
  | 			String query1 = "show tables";
  | 			Statement stmt = conn.createStatement();
  | 			ResultSet rs = stmt.executeQuery(query1);
  | 			while(rs.next()){
  | 				String tablename = rs.getString(1);
  | 				String classname = tablename + "EntityBean";
  | 				new GenerateEntityBeans(conn,tablename,classname,directory + classname + ".java");
  | 			}
  | 			stmt.close();
  | 			rs.close();
  | 			conn.close();
  | 		}catch(Exception e){
  | 			System.out.print(e.toString());
  | 		}finally{
  | 			;
  | 		}
  | 		
  | 	}
  | }
  | 
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4024640#4024640

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4024640



More information about the jboss-user mailing list