oracle根据数据库⽣成java实体类,javabean
了很多地⽅,确实很⿇烦,各种⼆次依赖包,各种要钱,这种没什么技术含量,只是⿇烦点的⼯具类,哪⾥那么多要求?下⾯免费分享给⼤家,虽然粗糙,但是能⽤,点个赞就好,能给个⿎励就更好了,初级程序员⼀枚,求⼤家谅解
package com.databi.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
为了链接oracle数据库 ⽣成表对应的javabean
@author Administrator
*/
public class JavaBeanUtils implements Serializable{
private PreparedStatement ps = null;
private ResultSet rs = null;
private static Connection con = null;
private CallableStatement cst = null;
static class Ora{
static final String DRIVER_CLASS = “oracle.jdbc.driver.OracleDriver”;
static final String DATABASE_URL = “jdbc:oracle:thin:@127.0.0.1:1521:orcl”;
static final String DATABASE_USER = “databi”;
static final String DATABASE_PASSWORD = “123456”;
static final String DATABASE_TABLE = “bas_dept”; //需要⽣成的表名
}
static class MySql{
static final String DRIVER_CLASS = "sql.jdbc.Driver";
mysql下载要钱吗static final String DATABASE_URL = "jdbc:mysql://localhost/plusoft_test?useUnicode=true&characterEncoding=GBK";
static final String DATABASE_USER = "root";
static final String DATABASE_PASSWORD = "1234";
}
public static Connection getOracleConnection() {
try {
Class.forName(Ora.DRIVER_CLASS);
Connection(Ora.DATABASE_URL,Ora.DATABASE_USER,Ora.DATABASE_PASSWORD);
return con;
} catch (Exception ex) {
System.out.Message());
}
return con;
}
public static Connection getMySqlConnection() {
try {
Class.forName(MySql.DRIVER_CLASS);
Connection(MySql.DATABASE_URL,MySql.DATABASE_USER,MySql.DATABASE_PASSWORD);        return con;
} catch (Exception ex) {
ex.printStackTrace();
}
return con;
}
public static List<Map> getOracleTable(String Table) throws SQLException{
getOracleConnection();
List<Map> list = new ArrayList<Map>();
try {
DatabaseMetaData  m_DBMetaData = MetaData();
//getColumns(java.lang.String catalog,  java.lang.String schema,java.lang.String table, java.lang.String col)
ResultSet colrs = Columns(null,Ora.UpperCase(), UpperCase(),"%");        ()) {
Map map = new HashMap();
String columnName = String("COLUMN_NAME");
String columnType = String("TYPE_NAME");
int datasize = Int("COLUMN_SIZE");
int digits = Int("DECIMAL_DIGITS");
int nullable = Int("NULLABLE");
String remarks = String("REMARKS");
//System.out.println(columnName+" "+columnType+" "+datasize+" "+digits+" "+ nullable);
map.put("columnName", columnName);
map.put("columnType", columnType);
map.put("datasize", datasize);
map.put("remarks", remarks);
list.add(map);
// System.out.println(“TABLE_CAT” + “=" + String(“TABLE_CAT”));
// System.out.println(“TABLE_SCHEM” + "=” + String(“TABLE_SCHEM”));
// System.out.println(“TABLE_NAME” + “=" + String(“TABLE_NAME”));
// System.out.println(“COLUMN_NAME” + "=” + String(“COLUMN_NAME”));
// System.out.println(“DATA_TYPE” + “=" + String(“DATA_TYPE”));
// System.out.println(“TYPE_NAME” + "=” + String(“TYPE_NAME”));
// System.out.println(“COLUMN_SIZE” + “=" + String(“COLUMN_SIZE”));
// System.out.println(“BUFFER_LENGTH” + "=” + String(“BUFFER_LENGTH”));
/
/ System.out.println(“DECIMAL_DIGITS” + “=" + String(“DECIMAL_DIGITS”));
// System.out.println(“NUM_PREC_RADIX” + "=” + String(“NUM_PREC_RADIX”));
// System.out.println(“NULLABLE” + “=" + String(“NULLABLE”));
// System.out.println(“REMARKS” + "=” + String(“REMARKS”));
// System.out.println(“COLUMN_DEF” + “=" + String(“COLUMN_DEF”));
// System.out.println(“SQL_DATA_TYPE” + "=” + String(“SQL_DATA_TYPE”));
// System.out.println(“SQL_DATETIME_SUB” + “=" + String(“SQL_DATETIME_SUB”));
// System.out.println(“CHAR_OCTET_LENGTH” + "=” + String(“CHAR_OCTET_LENGTH”)); // System.out.println(“ORDINAL_POSITION” + “=" + String(“ORDINAL_POSITION”));
// System.out.println(“IS_NULLABLE” + "=” + String(“IS_NULLABLE”));
}
// ()){
// System.out.print(“列名:”+String(“COLUMN_NAME”));
// System.out.print(" 数据类型是:"+String(“DATA_TYPE”));
// System.out.print(" 类型名称是:"+String(“TYPE_NAME”));
// System.out.print(" 列⼤⼩是:"+String(“COLUMN_SIZE”));
// System.out.println(" 注释是:"+String(“REMARKS”));
// }
} catch (Exception e) {
e.printStackTrace();
}finally{
con.close();
}
return list;
}
/**
*  把Oracle字段类型转化为 java类型
* @param sqlType  字段类型
* @param size  字段⼤⼩
* @param scale  默认=0
* @return
*/
public static String oracleSqlType2JavaType(String sqlType,int size,int scale){
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("nvarchar")
|| sqlType.equals("nchar")) {
return "String";
} else if (sqlType.equals("datetime")
|| sqlType.equals("date")
|| sqlType.equals("timestamp")) {
return "Date";
}
return "String";
}
public static String getItems(List<Map> map,String tablename){
//记得转化成⼩写
StringBuffer sb = new  StringBuffer();
sb.append("package com.databi.bean;");
sb.append("\r\n");
sb.append("import java.util.Date;\r\n");
sb.append("/** \r\n "
+" *  \r\n"
+" * @author lsp  \r\n"
+" *\r\n"
+ "*/\r\n"  );
sb.append("\r\n");
sb.append("public class "+ LowerCase()) + "  implements java.io.Serializable  {\r\n");
//得到私有属性
for (Map map0 : map) {
String columnname = ("columnName").toString();
String columntype = ("columnType").toString();
String columnsize = ("datasize").toString();
String remarks = ("remarks")==null?"":("remarks").toString();
String javaType = LowerCase(),Integer.parseInt(columnsize),0);
String temp = "\tprivate "+javaType+" "+LowerCase()+"; //"+remarks+"\r\n";
sb.append(temp);
}
//得到getter和setter
for (Map map0 : map) {
String columnname = ("columnName").toString();
String columntype = ("columnType").toString();
String columnsize = ("datasize").toString();
String javaType = LowerCase(),Integer.parseInt(columnsize),0);
String temp = "\tpublic "+javaType+" "+"get"+LowerCase())+"(){\r\n";
String temp1 = "\t\treturn "+LowerCase()+";\r\n";
String temp2 = "\t}\r\n";
sb.append(temp+temp1+temp2);
temp = "\tpublic void "+"set"+LowerCase())+"("+javaType+" "+LowerCase()+"){\r\n";        temp1 = "\t\tthis."+LowerCase()+" = "+LowerCase()+";\r\n";
temp2 = "\t}\r\n";
sb.append(temp+temp1+temp2);
}
sb.append("}");
String();
}
/**
* 把输⼊字符串的⾸字母改成⼤写
* @param str
* @return
*/
public static String getUpperOne(String str){
char[] ch = CharArray();
if (ch[0] >= 'a' && ch[0] <= 'z') {
ch[0] = (char) (ch[0] - 32);
}
return new String(ch);
}
}
public static void main(String[] args) throws IOException {
//JavaBeanUtils.sysoutOracleTCloumns("pexam_items_title", "his_yhkf");
try {
String tables  = "bi_bas_dept";
String[] arr = tables.split(",");
for (String string : arr) {
String name = LowerCase());
List<Map> map = getOracleTable(string);
String a = getItems(map,string);
File file = new File("D:\\bbb\\"+name+".java");
if (!ists()) {
}
FileOutputStream fos = new FileOutputStream(file,true);//true表⽰在⽂件末尾追加            fos.Bytes());
fos.close();//流要及时关闭
}
System.out.println("⽣成java完成");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。