1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.sql.*;
public class MyEntityUtils { 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;
public static void main(String[] args) { MyEntityUtils t = new MyEntityUtils(); t.tableToEntity("imginfo"); }
public Connection getConnection() { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://127.0.0.1:3306/jwt"; String username = "root"; String password = "tuzhihao"; Connection con = null; try { Class.forName(driver); con = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return con; }
public void tableToEntity(String tName) { tablename = tName; Connection conn = getConnection(); String strsql = "SELECT * FROM " + tablename; try { System.out.println(strsql); PreparedStatement pstmt = conn.prepareStatement(strsql); pstmt.executeQuery(); ResultSetMetaData rsmd = pstmt.getMetaData(); int size = rsmd.getColumnCount(); colnames = new String[size]; colTypes = new String[size]; colSizes = new int[size]; colScale = new int[size]; for (int i = 0; i < rsmd.getColumnCount(); i++) { rsmd.getCatalogName(i + 1); colnames[i] = rsmd.getColumnName(i + 1).toLowerCase(); colTypes[i] = rsmd.getColumnTypeName(i + 1).toLowerCase(); colScale[i] = rsmd.getScale(i + 1); System.out.println(rsmd.getCatalogName(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] = rsmd.getPrecision(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 { try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
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.println(sb.toString()); return sb.toString();
}
private void processColnames(StringBuffer sb) { sb.append("\r\n/** " + tablename + "\r\n"); String colsiz = ""; String colsca = ""; 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); colnames[i] = str.replaceAll("_", ""); } sb.append("*/\r\n"); }
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"); sb.append("\t\treturn " + colnames[i] + ";\r\n"); sb.append("\t}\r\n"); } }
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"); }
private String initcap(String str) { char[] ch = str.toCharArray(); if (ch[0] >= 'a' && ch[0] <= 'z') { ch[0] = (char) (ch[0] - 32); } return new String(ch); }
private String oracleSqlType2JavaType(String sqlType, int scale, int size) { if (sqlType.equals("integer")) { return "Integer"; } else if (sqlType.equals("long")) { return "Long"; } else if (sqlType.equals("float") || sqlType.equals("float precision") || sqlType.equals("double") || sqlType.equals("double precision") ) { return "BigDecimal"; } else if (sqlType.equals("number") || sqlType.equals("decimal") || sqlType.equals("numeric") || sqlType.equals("real")) { return scale == 0 ? (size < 10 ? "Integer" : "Long") : "BigDecimal"; } else if (sqlType.equals("varchar") || sqlType.equals("varchar2") || sqlType.equals("char") || sqlType.equals("nvarchar") || sqlType.equals("nchar")) { return "String"; } else if (sqlType.equals("datetime") || sqlType.equals("date") || sqlType.equals("timestamp")) { return "Date"; }else if(sqlType.equals("int")){ return "int"; } return null; }
}
|