Tools.java 7.89 KB
package com.taover.codegenerate.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import com.taover.codegenerate.db.GenerateDaoPO;
import com.taover.codegenerate.model.po.ColumnModel;
import com.taover.codegenerate.tools.StringUtil;

public class Tools {
    
    private static String path = null;
    private static String resourcesPath = null;
    
	public static Connection getConnection(Properties properties) {
		Connection conn = null;
		try {
			Class.forName(properties.getProperty("driver"));
			Properties prop = new Properties();
			prop.setProperty("user", properties.getProperty("User"));
			prop.setProperty("password", properties.getProperty("Password"));
			prop.setProperty("remarks", "true");
			conn = DriverManager.getConnection(properties.getProperty("URL"),
					prop);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static String getPath() {
	    
		 //兼容传入path使用
	    if(path != null){
	        return path;	        
	    }
	    
	    GenerateDaoPO td = null;
        String str = null;
        try {
            td = new GenerateDaoPO();
            str = System.getProperty("user.dir") + File.separator + "bin";
        } catch (Exception e) {
            str = ".";
            e.printStackTrace();
        }
        return str;
	    
	
	}
	
	public static String getResourcesPath() {	    
	    return resourcesPath;	        
	    
	}
    public static void setResourcesPath(String resourcesPath) {
        Tools.resourcesPath = resourcesPath;
    }

    public static void setPath(String path) {
        Tools.path = path;
    }

    public static Properties getProperties(String propertiesName) {
	    Properties properties = null;
	    try {
	        properties = new Properties();
	        InputStream inputStream = new FileInputStream(getResourcesPath() +File.separator +propertiesName + ".properties");
	        properties.load(inputStream);
	        inputStream.close();
	    } catch (Exception e) {
	        properties = null;
	        e.printStackTrace();
	    }
	    return properties;
	}
	
	public static Properties getProperties() {
		Properties properties = null;
		try {
			properties = new Properties();
			InputStream inputStream = new FileInputStream(getPath() +File.separator
					+ "default.properties");
			properties.load(inputStream);
			inputStream.close();
		} catch (Exception e) {
			properties = null;
			e.printStackTrace();
		}
		return properties;
	}
	public static Properties getProductProperties() {
		Properties properties = null;
		try {
			properties = new Properties();
			InputStream inputStream = new FileInputStream(getPath()+File.separator
					+ "product.properties");
			properties.load(inputStream);
			inputStream.close();
		} catch (Exception e) {
			properties = null;
			e.printStackTrace();
		}
		return properties;
	}

	public static String getColumnListStr(Vector<TableColumn> tableColumns,
			String tableName) throws SQLException {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < tableColumns.size(); i++) {
			if (i == tableColumns.size() - 1) {
				sb.append(tableColumns.get(i).getColumnName());
				break;
			}
			if (tableColumns.get(i).getColumnName().equals("check")) {
				sb.append("\\\"" + tableColumns.get(i).getColumnName()
						+ "\\\", ");
			} else {
				sb.append(tableColumns.get(i).getColumnName() + ", ");
			}
		}
		return sb.toString();
	}

	public static List<ColumnModel> getColums(Vector<TableColumn> tableColumns) {
		if (tableColumns == null || tableColumns.size() == 0)
			return null;
		List<ColumnModel> list = new ArrayList<ColumnModel>();
		for (int i = 0; i < tableColumns.size(); i++) {
			String sColumnDBName = tableColumns.get(i).getColumnName();
			String sColumnBeanName = StringUtil.formatBeanNameFirstLow(sColumnDBName);
			ColumnModel cm = new ColumnModel();
			cm.setColumnName(sColumnDBName);
			cm.setColumnMethodName(sColumnBeanName);
			cm.setColumnRemarks(tableColumns.get(i).getRemarks());
			cm.setColumnUName(StringUtil
					.formatBeanNameFirstUpper(sColumnDBName));
			String seq = tableColumns.get(i).getSeqName();
			if (seq != null && !seq.isEmpty()) {
				cm.setSeqName(seq);
			}
			cm.setColumnJAVAType(tableColumns.get(i).getJAVADataType());
			list.add(cm);
		}
		return list;
	}

	public static void createFile(String path, String filename, String incontent, boolean isCover)
			throws IOException {
		java.io.File file = new java.io.File(path + File.separator + filename);
		if(!isCover && file.exists()){
			return;
		}
		
		createFile(path);
		if (file.exists() && file.isFile())
			file.delete();
		if (!file.exists())
			file.createNewFile();

		try {
			FileOutputStream out = new FileOutputStream(path + File.separator+ filename);
			PrintStream p = new PrintStream(out);
			p.println(incontent);
			out.close();
			p.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}
	
	public static void createFile(String path, String filename, String incontent)
			throws IOException {
		java.io.File file = new java.io.File(path + File.separator + filename);
		createFile(path);
		if (file.exists() && file.isFile())
			file.delete();
		if (!file.exists())
			file.createNewFile();

		try {
			FileOutputStream out = new FileOutputStream(path + File.separator+ filename);
			PrintStream p = new PrintStream(out);
			p.println(incontent);
			out.close();
			p.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	public static void createFile(String path) {
		File tempFile = new File(path);
			tempFile.mkdirs();


		/*StringTokenizer st = new StringTokenizer(path, File.separator);
		String path1 = st.nextToken() + File.separator;
		String path2 = path1;
		while (st.hasMoreTokens()) {
			path1 = st.nextToken() + File.separator;
			path2 += path1;
			File inbox = new File(path2);
			if (!inbox.exists())
				inbox.mkdir();
		}*/
	}
	
	public static Statement createStmt(Connection conn) {
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stmt;
	}
	
	public static int executeUpdate(Connection conn, String sql) {
		Statement stmt = null;
		int ret = 0;
		try {
			stmt = conn.createStatement();
			ret = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	public static PreparedStatement prepareStmt(Connection conn, String sql) {
		PreparedStatement pStmt = null;
		try{
			pStmt = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pStmt;
	}
	
	public static PreparedStatement prepareStmt(Connection conn, String sql, int autoGeneratedKeys) {
		PreparedStatement pStmt = null;										 
		try{
			pStmt = conn.prepareStatement(sql, autoGeneratedKeys);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pStmt;
	}
	
	public static ResultSet executeQuery(Statement stmt, String sql) {
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public static void close(Connection conn) {
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn = null;
		}
	}
	
	public static void close(Statement stmt) {
		if(stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt = null;
		}
	}
	
	public static void close(ResultSet rs) {
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs = null;
		}
	}
}