能夠直接Copy編譯使用的,不過別忘了數據庫驅動! java
另外注意的:數據庫,庫中的表字段要有註釋信息,要否則是不可能拿到註釋信息,而後注意下面那個if判斷我那是判斷有"user"這個表就輸出這個表的信息! mysql
javac不須要驅動jar,但java須要對應的數據驅動jar文件哦固然你複製到MyEclipse 或者Eclipse中了就很少解了! sql
import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import javax.swing.filechooser.FileSystemView; public class DBHelpInfo { /** * 這裏是Oracle鏈接方法 *private static final String driver = "oracle.jdbc.driver.OracleDriver"; *private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl"; *private static final String uid = "system"; *private static final String pwd = "sys"; *這裏是SQL Server鏈接方法 *private static final String url = "jdbc:sqlserver://localhost:1433;DateBaseName=數據庫名"; *private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver" *private static final String uid = "sa"; *private static final String pwd = "sa"; * * * 這裏是MySQL鏈接方法 */ private static final String driver="com.mysql.jdbc.Driver"; private static final String pwd="root"; private static final String user="root"; private static final String url = "jdbc:mysql://localhost/test" + "?user=" + user + "&password=" + pwd + "&useUnicode=true&characterEncoding=UTF-8"; private static Connection getConnection=null; public static void main(String[] args) { FileSystemView fsv=FileSystemView.getFileSystemView(); String path=fsv.getHomeDirectory().toString();//獲取當前用戶桌面路徑 getConnection=getConnections(); try { DatabaseMetaData dbmd=getConnection.getMetaData(); ResultSet resultSet = dbmd.getTables(null, "%", "%", new String[] { "TABLE" }); while (resultSet.next()) { String tableName=resultSet.getString("TABLE_NAME"); //System.out.println(tableName); if(tableName.equals("user")){ //ResultSet rs =getConnection.getMetaData().getColumns(null, getXMLConfig.getSchema(),tableName.toUpperCase(), "%");//其餘數據庫不須要這個方法的,直接傳null,這個是oracle和db2這麼用 ResultSet rs = dbmd.getColumns(null, "%", tableName, "%"); File directory = new File(path); FileWriter fw = new FileWriter(directory+ "\\"+tableName.toUpperCase()+".xml"); PrintWriter pw = new PrintWriter(fw); System.out.println("表名:"+tableName+"\t\n表字段信息:"); pw.write("<p filid=\"xx\" table=\""+tableName.toUpperCase()+"\" zj=\"xx\"/>\n"); while(rs.next()){ pw.write("\t<p code=\""+rs.getString("COLUMN_NAME").toUpperCase()+"\" name=\""+rs.getString("REMARKS")+"\"/>\n"); System.out.println("字段名:"+rs.getString("COLUMN_NAME")+"\t字段註釋:"+rs.getString("REMARKS")+"\t字段數據類型:"+rs.getString("TYPE_NAME")); } pw.write("</p>"); pw.flush(); pw.close(); System.out.println("生成成功!"); } } } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnections(){ try { //Properties props =new Properties(); //props.put("remarksReporting","true"); Class.forName(driver); getConnection=DriverManager.getConnection(url); } catch (Exception e) { e.printStackTrace(); } return getConnection; } ////其餘數據庫不須要這個方法 oracle和db2須要 public static String getSchema() throws Exception { String schema; schema =getConnection.getMetaData().getUserName(); if ((schema == null) || (schema.length() == 0)) { throw new Exception("ORACLE數據庫模式不容許爲空"); } return schema.toUpperCase().toString(); } }
rs.getString("這裏放入下面列出的常量") 數據庫
例如: