자바 자동 생 성 mybatis 업데이트 sql 문장

5917 단어 자바 시스템
package com.hebrf.test.sql;

import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class CreUpSql {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String[] noUpKeys = {"BH"};
		String[][] contions = {{"BH","BH ="} };
		new CreUpSql().createUpdateSql("tableName", noUpKeys,contions);
	}

	/**
	 *      sql  
	 * @param tableName   
	 * @param noUpKeys        
	 * @param contions where    [0]       【1】      +       :  like =   
	 */
	public void createUpdateSql(String tableName,String[] noUpKeys,String[][] contions) {
		String fileName = "D:" + File.separator + "sqlwork" + File.separator
				+ tableName + "UPDATE" + ".txt";

		String[][] strs = getFieldsNames(tableName);
		
		checkArrNull(strs);
		
		
		File f = createNewFile(fileName);
		
		createUpFile(f,strs,noUpKeys,tableName,contions);
	}

	/**
	 *   str      values 
	 *          ture
	 * @return
	 */
	public boolean check(String str,String[] values){
		boolean flag = false;
		for(int i = 0; i < values.length; i++){
			if(str.equals(values[i])){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	/**
	 *     sql    
	 * @param f
	 * @param strs
	 * @param tableName
	 */
	private void createUpFile(File f, String[][] strs,String[] noUpKeys, String tableName,String[][] contions) {
		
		OutputStream out;
		try {
			out = new FileOutputStream(f, true);
			
			String str = " UPDATE " + tableName + " ";
			writeStrToFile(out,f,str);
			
			writeStrToFile(out,f,"");
			// *******************  SET***************************************************************************
			for (int i = 0; i < strs.length; i++) {
				if(noUpKeys!=null){
					if(check(strs[i][0],noUpKeys)){
						continue;
					}
				}
				String temp = "#{" + strs[i][0] + "}";
				if (strs[i][1].startsWith("DATE")
						|| strs[i][1].startsWith("TIME")) {
					temp = "to_date(" + temp + ",'yyyy-mm-dd hh24:mi:ss')";
				}
				String nullstr = strs[i][0]+"=null";
				if (i == (strs.length - 1)) {
				} else {
					temp += ",";
					nullstr += ",";
				}
				temp = "" + strs[i][0]+"="+temp + "";
		//upNullFlag        (    )  upNullFlag   false     VO     
				temp = temp+"" + nullstr + "";
				temp = "\t" + temp +"";
				
				writeStrToFile(out,f,temp);
			}
			writeStrToFile(out,f,"");
			if(contions!=null && contions.length > 0){
				creatWhere(out, f,contions);
			}
			out.close();
			System.out.println(tableName + "        -->" + f.toString());
			Desktop.getDesktop().open(f);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param out
	 * @param f
	 * @param contions [0]       【1】      +       :  like =   
	 */
	private void creatWhere(OutputStream out, File f, String[][] contions) {
		// TODO Auto-generated method stub
		writeStrToFile(out,f,"");

		for(int i=0; i < contions.length; i++){
			String temp = "\tand "+contions[i][1]+" #{"+contions[i][0]+"}";
			writeStrToFile(out,f,temp);
		}
		writeStrToFile(out,f,"");
	}

	/**
	 *            
	 * @param out
	 * @param f
	 * @param str
	 */
	private void writeStrToFile(OutputStream out, File f, String str) {
		// TODO Auto-generated method stub
		byte[] b = ("\r
\t\t"+str).getBytes(); for (int i = 0; i < b.length; i++) { try { out.write(b[i]); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } } } /** * * @param strs */ private void checkArrNull(String[][] strs) { if (strs == null || strs.length == 0) { try { throw new Exception(" "); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return; } } } /** * * @param fileName * @return */ public File createNewFile(String fileName) { File f = new File(fileName); try { if (f.exists()) { // f.delete(); } f.createNewFile();// sql } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return f; } /** * * * @param tabname * @return */ public String[][] getFieldsNames(String tabname) { Connection conn = ConnUtil.getConn(); Statement stat = null; ResultSet rs = null; ResultSetMetaData data = null; // ResultSetMetaData String[][] resultStrs = null; int coloumCount = 0; try { stat = conn.createStatement(); String sql = "select * from " + tabname; rs = stat.executeQuery(sql);// data = rs.getMetaData(); coloumCount = data.getColumnCount(); resultStrs = new String[coloumCount][2]; for (int i = 0; i < coloumCount; i++) { resultStrs[i][0] = data.getColumnName(i + 1); resultStrs[i][1] = data.getColumnTypeName(i + 1); } // if (rs != null) { rs.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } return resultStrs; } }

좋은 웹페이지 즐겨찾기