数据库表转javaBean

复制后修改部分代码

package com.study;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class TableToJavaBean {

	Connection conn = null;
	
	Statement stmt = null;
	
	public Connection getConnection(){
		
		String driverName = "";
		
	    String url = "";
		
		String username = "";
		
	    String pwd = "";
		
		try {
   			InputStream in = this.getClass().getResourceAsStream("/dbconfig.properties");
   	   	    
   	   		Properties pros = new Properties();  
   	   		
			pros.load(in);
			
			driverName = pros.getProperty("database.driver");
			url = pros.getProperty("database.url");
			username =  pros.getProperty("database.user");
			pwd =  pros.getProperty("database.password");
			
			System.out.println(driverName+"=="+url);
			
			Class.forName(driverName);
			
		    conn = DriverManager.getConnection(url, username, pwd);
		    
		} catch (SQLException e) {
		} catch (ClassNotFoundException e) {
		} catch (IOException e) {
		}
		return conn;
	}
	
	
	public void closeDB(PreparedStatement pstmt) {
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (Exception ex) {
			}
		}
	}
	
	public void closeDB(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception ex) {
			}
		}
	}

	public void closeDB(Connection con) {
		if (con != null) {
			try {
				con.close();
			} catch (Exception ex) {
			}
		}
	}

	
	public void toPojo(String tableName) {
		
		String sql = "select * from "+tableName;
 		try {
 			
 			conn = getConnection();
 			
 			PreparedStatement statement = conn.prepareStatement(sql);
 			//获取数据库的元数据 
 			ResultSetMetaData metadata = statement.getMetaData();
 			//数据库的字段个数
 			int len = metadata.getColumnCount();
 			//字段名称
 			colnames = new String[len+1];
 			//字段类型 --->已经转化为java中的类名称了
 			colTypes = new String[len+1];
 			for(int i= 1;i<=len;i++){
 				colnames[i] = metadata.getColumnName(i); //获取字段名称
 				colTypes[i] = sqlType2JavaType(metadata.getColumnTypeName(i)); //获取字段类型		
 			}
 		} catch (SQLException e) {

 		}
	}

	 	private String[] colnames; // 列名数组
	 	//列名类型数组  
	 	private String[] colTypes;
	 
	 	/*
	 	 * mysql的字段类型转化为java的类型*/
	 	private String sqlType2JavaType(String sqlType) {  
	         
	         if(sqlType.equalsIgnoreCase("bit")){  
	             return "boolean";  
	         }else if(sqlType.equalsIgnoreCase("tinyint")){  
	             return "byte";  
	         }else if(sqlType.equalsIgnoreCase("smallint")){  
	             return "short";  
	         }else if(sqlType.equalsIgnoreCase("int")){  
	             return "int";  
	         }else if(sqlType.equalsIgnoreCase("bigint")){  
	             return "long";  
	         }else if(sqlType.equalsIgnoreCase("float")){  
	             return "float";  
	         }else if(sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")   
	                 || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")   
	                 || sqlType.equalsIgnoreCase("smallmoney")){  
	             return "double";  
	         }else if(sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")   
	                 || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")   
	                 || sqlType.equalsIgnoreCase("text")){  
	             return "String";  
	         }else if(sqlType.equalsIgnoreCase("datetime") ||sqlType.equalsIgnoreCase("date")){  
	             return "Date";  
	         }else if(sqlType.equalsIgnoreCase("image")){  
	             return "Blod";  
	         }  
	           
	         return null;  
	     }
	 	/*获取整个类的字符串并且输出为java文件
	 	 * */
	 	public  StringBuffer getClassStr(String tableName){
	 		//输出的类字符串
	 		StringBuffer str = new StringBuffer("");
	 		//获取表类型和表名的字段名
	 		this.toPojo(tableName);
	 		//校验
	 		if(null == colnames && null == colTypes) return null;
	 		//拼接
	 		str.append("public class "+tableName+" {\r\n");
	 		//拼接属性
	 		for(int index=1; index < colnames.length ; index++){
	 			str.append(getAttrbuteString(colnames[index],colTypes[index]));
	 		}
	 		//拼接get,Set方法		
	 		for(int index=1; index < colnames.length ; index++){
	 			str.append(getGetMethodString(colnames[index],colTypes[index]));
	 			str.append(getSetMethodString(colnames[index],colTypes[index]));
	 		}
	 		str.append("}\r\n");
	 		//输出到文件中
	 		File file = new File(tableName+".java");
	 		BufferedWriter write = null;

	 		try {
	 			write = new BufferedWriter(new FileWriter(file));
	 			write.write(str.toString());
	 			write.close();
	 		} catch (IOException e) {

	 			e.printStackTrace();
	 			if (write != null)
	 				try {
	 					write.close();
	 				} catch (IOException e1) {			
	 					e1.printStackTrace();
	 				}
	 		}
	 		return str;
	 	}
	 	/*
	 	 * 获取字段字符串*/
	 	public StringBuffer getAttrbuteString(String name, String type) {
	 		if(!check(name,type)) {
	 			System.out.println("类中有属性或者类型为空");
	 			return null;
	 		};
	 		String format = String.format("    private %s %s;\n\r", new String[]{type,name});
	 		return new StringBuffer(format);
	 	}
	 	/*
	 	 * 校验name和type是否合法*/
	 	public boolean check(String name, String type) {
	 		if("".equals(name) || name == null || name.trim().length() ==0){
	 			return false;
	 		}
	 		if("".equals(type) || type == null || type.trim().length() ==0){
	 			return false;
	 		}
	 		return true;
	 		
	 	}
	 	/*
	 	 * 获取get方法字符串*/
	 	private StringBuffer getGetMethodString(String name, String type) {
	 		if(!check(name,type)) {
	 			System.out.println("类中有属性或者类型为空");
	 			return null;
	 		};
	 		String Methodname = "get"+GetTuoFeng(name);
	 		String format = String.format("    public %s %s(){\n\r", new Object[]{type,Methodname});
	 		format += String.format("        return this.%s;\r\n", new Object[]{name});
	 		format += "    }\r\n";
	 		return new StringBuffer(format);
	 	}
	 	//将名称首字符大写
	 	private String GetTuoFeng(String name) {
	 		name = name.trim();
	 		if(name.length() > 1){
	 			name = name.substring(0, 1).toUpperCase()+name.substring(1);
	 		}else
	 		{
	 			name = name.toUpperCase();
	 		}
	 		return name;
	 	}
	 	/*
	 	 * 获取字段的get方法字符串*/
	 	private Object getSetMethodString(String name, String type) {
	 		if(!check(name,type)) {
	 			System.out.println("类中有属性或者类型为空");
	 			return null;
	 		};
	 		String Methodname = "set"+GetTuoFeng(name);
	 		String format = String.format("    public void %s(%s %s){\n\r", new Object[]{Methodname,type,name});
	 		format += String.format("        this.%s = %s;\r\n", new Object[]{name,name});
	 		format += "    }\r\n";
	 		return new StringBuffer(format);
	 	}
	 }

  

posted @ 2015-08-07 10:03  凉城  阅读(2479)  评论(1编辑  收藏  举报