程序员偷懒小程序:根据字段,类型,size自动生成建表sql语句

package yuwenzhi.javaUtils;

import java.io.BufferedReader;
import java.io.FileReader;

public class ShowCreateTable {
	public static void main(String[] args) {
		long startTime = System.nanoTime();    //获取开始时间

		test();    //测试的代码段

		long endTime = System.nanoTime();    //获取结束时间

		System.out.println("程序运行时间:" + (endTime - startTime) + "ns");    //输出程序运行时间
		
		
	}
	public static void test() {
		try {
			FileReader	reader = new FileReader("f:/test.txt");
			BufferedReader bufferedReader=new BufferedReader(reader);
			
			String line;
			String[] lineArray;
			int count=1;
			StringBuilder stringBuilder=new StringBuilder();
			stringBuilder.append("create table test(");
			while((line=bufferedReader.readLine())!=null) {
				lineArray=line.split("\\s+");
				
				stringBuilder.append(lineArray[0]+" ");
				if(lineArray[1].toLowerCase().equals("varchar")|| lineArray[1].toLowerCase().equals("int")) {
					stringBuilder.append(lineArray[1]+"("+lineArray[2]+")");
				}
				if(lineArray[1].toLowerCase().equals("datetime")) {
					stringBuilder.append(lineArray[1]);
				}
				if(lineArray.length==4) {
					stringBuilder.append(" not null,");
				}else {
					stringBuilder.append(",");
				}
				count++;
				
			}
			stringBuilder.append("primary key(8888,78888));");
			
			System.out.println(stringBuilder.toString());
			
		
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(JAVASE,sql,自动生成,mysql)