import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import Element.sqlElem;
/**
* 必须新建一个实体类 实体类名称:sqlElem
* 内设字段如下:
* private String name;//
* 字段名 private String cls;//类型
* @author mfkwfc
*/
public class CreateElementandDao {
/**
* 开始读取文本文件第一步验证
*/
public void start(String url) {
File file = new File(url);
if (file.exists()
&& url.substring(url.length() - 3, url.length()).equals("txt")) {
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
Map<String, List<sqlElem>> map = readtxt(buff);
if (map != null) {
String tempurl = url.substring(0, url.lastIndexOf("/") + 1);
readDao(map,tempurl);
readElement(map, tempurl);
}
} catch (FileNotFoundException e) {
System.out.println("读取失败了哦。");
e.printStackTrace();
}
} else {
System.out.println("该文件不存在哦或者后缀名错误");
}
}
/**
* 通过验证做读取动作
*/
private Map<String, List<sqlElem>> readtxt(BufferedReader buff) {
Map<String, List<sqlElem>> map = null;
try {
String tempSql = "";
while (buff.ready()) {
String text = buff.readLine();
tempSql += text;
}
buff.close();
String[] temps = tempSql.split(";");
for (String temp : temps) {
int len = temp.indexOf("table");
if (len == -1)// 可能会有创建数据库代码假如遇到则跳过
continue;
temp = temp.substring(len + 5, temp.length());// 剥第一层皮得到表名
int tablelen = temp.indexOf("(");
String tableName = temp.substring(0, tablelen).trim();// 获得表的名称
temp = temp.substring(tablelen + 1, temp.lastIndexOf(")"))
.trim();// 剥第二层皮得到里边的具体字段
String[] charat = temp.split(",");// 分割里边的具体字段
if (map == null)
map = new HashMap<String, List<sqlElem>>();
List<sqlElem> ls = null;
for (String ch : charat) {
if (ls == null)
ls = new ArrayList<sqlElem>();
ch = ch.trim();
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import Element.sqlElem;
/**
* 必须新建一个实体类 实体类名称:sqlElem
* 内设字段如下:
* private String name;//
* 字段名 private String cls;//类型
* @author mfkwfc
*/
public class CreateElementandDao {
/**
* 开始读取文本文件第一步验证
*/
public void start(String url) {
File file = new File(url);
if (file.exists()
&& url.substring(url.length() - 3, url.length()).equals("txt")) {
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
Map<String, List<sqlElem>> map = readtxt(buff);
if (map != null) {
String tempurl = url.substring(0, url.lastIndexOf("/") + 1);
readDao(map,tempurl);
readElement(map, tempurl);
}
} catch (FileNotFoundException e) {
System.out.println("读取失败了哦。");
e.printStackTrace();
}
} else {
System.out.println("该文件不存在哦或者后缀名错误");
}
}
/**
* 通过验证做读取动作
*/
private Map<String, List<sqlElem>> readtxt(BufferedReader buff) {
Map<String, List<sqlElem>> map = null;
try {
String tempSql = "";
while (buff.ready()) {
String text = buff.readLine();
tempSql += text;
}
buff.close();
String[] temps = tempSql.split(";");
for (String temp : temps) {
int len = temp.indexOf("table");
if (len == -1)// 可能会有创建数据库代码假如遇到则跳过
continue;
temp = temp.substring(len + 5, temp.length());// 剥第一层皮得到表名
int tablelen = temp.indexOf("(");
String tableName = temp.substring(0, tablelen).trim();// 获得表的名称
temp = temp.substring(tablelen + 1, temp.lastIndexOf(")"))
.trim();// 剥第二层皮得到里边的具体字段
String[] charat = temp.split(",");// 分割里边的具体字段
if (map == null)
map = new HashMap<String, List<sqlElem>>();
List<sqlElem> ls = null;
for (String ch : charat) {
if (ls == null)
ls = new ArrayList<sqlElem>();
ch = ch.trim();