第一步:连接数据库
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 | import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; public class DBConnection { private static final String DBDRIVER = "com.mysql.jdbc.Driver" ; //驱动类类名 private static final String DBURL = "jdbc:mysql://localhost:3306/ibatis" ; //连接URL private static final String DBUSER = "root" ; //数据库用户名 private static final String DBPASSWORD = "admin" ; //数据库密码 static { //将加载驱动放到静态块中 try { Class.forName(DBDRIVER); //加载驱动 } catch (ClassNotFoundException e1) { //发生加载驱动异常 JOptionPane.showMessageDialog( null , "加载驱动失败!!!" , "提示信息" , JOptionPane.INFORMATION_MESSAGE); //提示加载驱动失败 } } public static Connection getConnection() { Connection conn = null ; //建立Connection接口引用 try { conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); //建立连接 } catch (SQLException e) { // 发生连接异常 JOptionPane.showMessageDialog( null , "连接MySQL连接失败!!!" , "提示信息" , JOptionPane.INFORMATION_MESSAGE); //提示连接MySQL连接失败 } return conn; } public static void close(Connection conn) { if (conn != null ) { //判断Connection对象是否为空 try { conn.close(); // 关闭连接数据库资源 } catch (SQLException e){ //判断关闭Connection对象时是否发生异常 System.out.println( "关闭数据库连接发生异常" ); } } } public static void close(Statement stmt) { if (stmt != null ){ //判断Statement对象是否为空 try { stmt.close(); //关闭操作数据库资源 } catch (SQLException e){ //判断关闭Statement对象时是否发生异常 JOptionPane.showMessageDialog( null , "关闭数据库操作资源发生异常!!!" , "提示信息" , JOptionPane.INFORMATION_MESSAGE); //提示关闭数据库操作资源发生异常 } } } public static void close(ResultSet rs) { if (rs != null ) { //判断结果集是否为空 try { rs.close(); //关闭结果集 } catch (SQLException e){ //判断结果集是否发生异常 JOptionPane.showMessageDialog( null , "关闭结果集发生异常!!!" , "提示信息" , JOptionPane.INFORMATION_MESSAGE); //提示关闭结果集发生异常 } } } } |
第二步:执行转换:
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 | import java.io.File; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Types; import org.apache.commons.dbutils.DbUtils; public class BuildDTO { public void build(String tableName, String packageName, String className) { Connection conn = null ; PreparedStatement pment = null ; ResultSet rs = null ; PrintWriter pw = null ; String pack = packageName.replace( "." , "/" ); // 包名 System.out.println( "src/" + pack + "/" + className + ".java" ); try { pw = new PrintWriter( new File( "src/" + pack + "/" + className + ".java" )); pw.println( "package " + packageName + ";\n\n" ); pw.println( "" ); pw.println( "public class " + className + " \n{\t" ); // 创建连接 conn = DBConnection.getConnection(); // 构建预处理器 pment = conn.prepareStatement( "select * from " + tableName + " where 1 = 2;" ); rs = pment.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); // 获取表单的列数 int colum = metaData.getColumnCount(); for ( int i = 1 ; i <= colum; i++) { String pStr = "" ; // setXxxx String typeStr = "" ; // 类型 // 获取列名 String columName = metaData.getColumnName(i); // 获取每一列的数据类型 int type = metaData.getColumnType(i); // System.out.println(i+"---"+type); // 判断 typeStr = type(type); // 组装 private 的语句 pStr += "private " + typeStr + " " + columName + ";" ; // 输出 private 的字段 pw.println( "\t" + pStr + "" ); } String constructStr = "" ; // 构造 // 组装空参构造 constructStr += "public " + className + "()\n\t{\n\n\t}" ; // 输出空参构造 pw.println( "\n\t" + constructStr + "\n" ); for ( int i = 1 ; i <= colum; i++) { String getStr = "" ; String setStr = "" ; String typeStr = "" ; // 获取列名 String columName = metaData.getColumnName(i); // 获取每一列的数据类型 int type = metaData.getColumnType(i); // 判断 typeStr = type(type); // 组装 set 的语句 setStr += "public void set" + columName.substring( 0 , 1 ).toUpperCase() + "" + columName.substring( 1 ) + "(" + typeStr + " " + columName + ")\n\t{\n" ; setStr += "\t\tthis." + columName + " = " + columName + ";\n\t}" ; // 组装get语句 getStr += "public " + typeStr + " get" + columName.substring( 0 , 1 ).toUpperCase() + "" + columName.substring( 1 ) + "()\n\t{\n\t" ; getStr += "\treturn this." + columName + ";\n\t}" ; // 输出 set pw.println( "\t" + setStr); // 输出 get pw.println( "\t" + getStr); } pw.println( "}" ); // 缓冲 pw.flush(); pw.close(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭连接 DbUtils.closeQuietly(conn, pment, rs); } } public String type( int type) { String typeStr = null ; if (Types.INTEGER == type) { typeStr = "Integer" ; } else if (Types.VARCHAR == type) { typeStr = "String" ; } else if (Types.CHAR == type) { typeStr = "CHAR" ; } else if (Types.TIMESTAMP == type) { typeStr = "Date" ; } else if (Types.INTEGER == type) { typeStr = "Integer" ; } else if (Types.LONGVARCHAR == type) { typeStr = "String" ; } return typeStr; } //获取数据库中的所有表 public void getTableNameByCon(Connection con) { try { DatabaseMetaData meta = con.getMetaData(); ResultSet rs = meta.getTables( null , null , null , new String[] { "TABLE" }); while (rs.next()) { System.out.println( "表名:" + rs.getString( 3 )); System.out.println( "表所属用户名:" + rs.getString( 2 )); System.out.println( "------------------------------" ); } con.close(); } catch (Exception e) { try { con.close(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { BuildDTO dto = new BuildDTO(); dto.build( "User" , "com.mzsx.sql2bean" , "User" ); dto.getTableNameByCon(DBConnection.getConnection()); } } |