在线mysql转bean_数据库表转换成javaBean对象⼩⼯具package test.utils;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* 数据库表转换成javaBean对象⼩⼯具(已⽤了很长时间),
数组c语言题目
* 1 bean属性按原始数据库字段经过去掉下划线,并⼤写处理⾸字母等等.
* 2 ⽣成的bean带了数据库的字段说明.
* 3 各位⾃⼰可以修改此⼯具⽤到项⽬中去.
*/
public class GenerateJavaFileUtils {
private String tablename = "";
private String[] colnames;
private String[] colTypes;
private int[] colSizes; // 列名⼤⼩
private int[] colScale; // 列名⼩数精度
private boolean importUtil = false;
private boolean importSql = false;
private boolean importMath = false;
/**
* @param args
*/
public void tableToEntity(String tName) {
tablename = tName;
// 数据连Connection获取,⾃⼰想办法就⾏.
Connection conn = getConnection();
String strsql = "SELECT * FROM " + tablename;// +" WHERE ROWNUM=1"
// 读⼀⾏记录;
try {
System.out.println(strsql);
PreparedStatement pstmt = conn.prepareStatement(strsql); uteQuery();
ResultSetMetaData rsmd = MetaData();
int size = ColumnCount(); // 共有多少列
colnames = new String[size];
colTypes = new String[size];
colSizes = new int[size];
colScale = new int[size];
for (int i = 0; i < ColumnCount(); i++) {
colnames[i] = ColumnName(i + 1).toLowerCase(); colTypes[i] = ColumnTypeName(i + 1).toLowerCase(); colScale[i] = Scale(i + 1);
System.out.CatalogName(i + 1));
if ("datetime".equals(colTypes[i])) {
importUtil = true;
}
if ("image".equals(colTypes[i]) || "text".equals(colTypes[i])) { importSql = true;
}
if (colScale[i] > 0) {
importMath = true;
}
colSizes[i] = Precision(i + 1);
}
String content = parse(colnames, colTypes, colSizes);
try {
FileWriter fw = new FileWriter(initcap(tablename) + ".java"); PrintWriter pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
releaseConnection(conn);
}
}
/**
* 解析处理(⽣成实体类主体代码)
*/
private String parse(String[] colNames, String[] colTypes, int[] colSizes) { StringBuffer sb = new StringBuffer();
sb.append("\r\nimport java.io.Serializable;\r\n");
if (importUtil) {
sb.append("import java.util.Date;\r\n");
}
if (importSql) {
sb.append("import java.sql.*;\r\n\r\n");
}
if (importMath) {
sb.append("import java.math.*;\r\n\r\n");
}
// 表注释
processColnames(sb);
sb.append("public class " + initcap(tablename)
+ " implements Serializable {\r\n");
processAllAttrs(sb);
processAllMethod(sb);
sb.append("}\r\n");
System.out.String());
String();
}
* 处理列名,把空格下划线'_'去掉,同时把下划线后的⾸字母⼤写 要是整个列在3个字符及以内,则去掉'_'后,不把"_"后⾸字母⼤写. * 同时把数据库列名,列类型写到注释中以便查看,
*
* @param sb
*/
private void processColnames(StringBuffer sb) {
别人说你yht什么意思sb.append("\r\n/** " + tablename + "\r\n");
String colsiz = "";
for (int i = 0; i < colnames.length; i++) {
colsiz = colSizes[i] <= 0 ? "" : (colScale[i] <= 0 ? "("
+ colSizes[i] + ")" : "(" + colSizes[i] + "," + colScale[i]
+ ")");
sb.append("\t" + colnames[i].toUpperCase() + " "
+ colTypes[i].toUpperCase() + colsiz + "\r\n");
char[] ch = colnames[i].toCharArray();
char c = 'a';
if (ch.length > 3) {
for (int j = 0; j < ch.length; j++) {
c = ch[j];
if (c == '_') {
if (ch[j + 1] >= 'a' && ch[j + 1] <= 'z') {
ch[j + 1] = (char) (ch[j + 1] - 32);
}
}
}
}
String str = new String(ch);
全国大学生计算机二级c语言题库colnames[i] = placeAll("_", "");
}
sb.append("*/\r\n");
}
/**
* ⽣成所有的⽅法
* @param sb
*/
private void processAllMethod(StringBuffer sb) {
for (int i = 0; i < colnames.length; i++) {
sb.append("\tpublic void set"
+ initcap(colnames[i])
+ "("
+ oracleSqlType2JavaType(colTypes[i], colScale[i],
colSizes[i]) + " " + colnames[i] + "){\r\n");
sb.append("\t\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n"); sb.append("\t}\r\n");
sb.append("\tpublic "
+ oracleSqlType2JavaType(colTypes[i], colScale[i],
colSizes[i]) + " get" + initcap(colnames[i])
+ "(){\r\n");
sqlite格式的视频怎么打开sb.append("\t\treturn " + colnames[i] + ";\r\n");
sb.append("\t}\r\n");
}
}
/**
* 解析输出属性
*
* @return
20个化学元素符号*/
private void processAllAttrs(StringBuffer sb) {
sb.append("\tprivate static final long serialVersionUID = 1L;\r\n"); for (int i = 0; i < colnames.length; i++) {
sb.append("\tprivate "
+ oracleSqlType2JavaType(colTypes[i], colScale[i],
colSizes[i]) + " " + colnames[i] + ";\r\n");
}
sb.append("\r\n");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论