package sqlite.jdbc.toEntity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 查询数据库,查询所有表名,及该表的所有字段,并生成 utf-8编码格式的java文件
* @author 震
*
*/
public class SQLiteTest{
/** sqlite 数据库目录 */
private String dbPath = "";
private Connection connection = null;
private Statement statement;
/**保存到...*/
private File savaFile;
/**包名*/
private String packageName = "";
/**
*
* @param savaFile 保存到.....目录
* @param packageName 实体类的包名
* @param dbPath sqlite数据库文件 所在目录
*/
public SQLiteTest(File savaFile,String packageName,String dbPath){
this.savaFile = savaFile;
this.packageName = packageName;
this.dbPath =dbPath;
try {
Class.forName("org.sqlite.JDBC");
this.connection = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
this.statement = connection.createStatement();
this.statement.setQueryTimeout(30); // set timeout to 30 sec.
} catch (SQLException e) {
System.out.println(e.toString());
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
}
public List<String> getTableName() throws SQLException, ClassNotFoundException{
ResultSet rs = statement.executeQuery("SELECT * FROM sqlite_master WHERE type = 'table'");
List<String> tableName = new ArrayList<String>();
while (rs.next()) {
//剔除android_metadata表
if (!rs.getString("name").equals("android_metadata")) {
tableName.add(rs.getString("name"));
}
}
for (int i = 0; i < tableName.size(); i++) {
fileWriters_LOG(tableName.get(i));
System.out.println("表名" + i + ":"+ tableName.get(i)+" 实体类生成。");
}
return tableName;
}
/**
*
* @return String[] 表中的所有字段。
* @throws ClassNotFoundException
* @throws SQLException
*/
public String[] getTableFields(String tableName) throws ClassNotFoundException, SQLException{
ResultSet rs = statement.executeQuery("SELECT * FROM " + tableName);
ResultSetMetaData rsmd = rs.getMetaData();
int count=rsmd.getColumnCount();
String[] name=new String[count];
for (int i = 0; i < count; i++) {
name[i] = rsmd.getColumnName(i+1);
}
return name;
}
/**
* @说明 Writer写入文件 保存为 UTF-8格式
* @author xuzhen
* @param content
* 写入的数据
* @return void
* @throws SQLException
* @throws ClassNotFoundException
* @throws IOException
*/
public boolean fileWriters_LOG(String tableName) throws ClassNotFoundException, SQLException{
/** 获取该表中的所有字段*/
String[] Fields = getTableFields(tableName);
/** 实体类文件 加前缀 */
tableName = "Tab_" + tableName;
String fileName = tableName+".java";
/***/
if (!savaFile.exists()) {
System.out.println(savaFile.getAbsolutePath()+"文件夹不存在,已经创建....");
savaFile.mkdirs();
}
/** 若存在,则删除,若不删除,则在原文件内重复写入*/
File isHave = new File(savaFile.getAbsoluteFile()+"/"+fileName);
if (isHave.exists()) {
isHave.delete();
}
FileOutputStream f;
OutputStreamWriter osw = null;
try {
f = new FileOutputStream(savaFile.getAbsoluteFile() +"/"+ fileName, true);
osw = new OutputStreamWriter(f, "UTF-8");
StringBuffer javaCode = new StringBuffer();
javaCode.append(packageName+"\n");
javaCode.append("import java.io.Serializable;\n");
javaCode.append("/** 默认实现Serializable接口,方便intent传值 */ \n");
javaCode.append("public class "+tableName+" implements Serializable{ \n");
javaCode.append("/** 默认String类型,数据如需计算,再单独转换类型 */ \n");
for (int i = 0; i < Fields.length; i++) {
javaCode.append("public String "+Fields[i]+"; \n");
}
javaCode.append("\n\n\n\n");
for (int i = 0; i < Fields.length; i++) {
javaCode.append(getModles(Fields[i]));
}
javaCode.append("} \n");
osw.write(javaCode.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
osw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
/**
* 生成 get set 方法
* @param field 字段名
* @return
*/
private String getModles(String field){
String methodName = field.replaceFirst(field.substring(0, 1), field.substring(0, 1).toUpperCase());
String parm = field.toLowerCase();
StringBuffer m = new StringBuffer();
m.append("\n");
m.append("public String get" + methodName + "() {\n");
m.append("\t return this." + field + ";\n");
m.append("}\n");
m.append("public void set" + methodName + "(String "+parm+") {\n");
m.append("\t this." + field + " = " + parm + ";\n");
m.append("}\n\n");
return m.toString();
}
}
- 1
- 2
前往页