`

mysql,sqlserver,Oracle 数据库生成javabean小工具

阅读更多

mysql ,sqlserver 增加Oracle生成javabean

 

1.BeanRobot.java 代码

package dbRobot;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class BeanRobot extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField ipFiled;
	private JTextField dbFiled;
	private JTextField dbNameFiled;
	private JTextField tabField;
	private JTextField packField;
	private JTextField catField;
	private JCheckBox checkBox;
	private JTextField userField;
	private JTextField pwdField;
	private JComboBox dbBox;
	DbUtil dbutil = new DbUtil() ;
	BeanUtil butil = new BeanUtil();
	JLabel labelInfo ;
	private JTable  jtable;
	private MyTableModel  tableModel ;
	HashMap dbInfoMap;
	String[] titles = {"选择","表格名称"} ;
	
	//配置文件信息
	Map<String,HashMap<String,String>> dbMap;
	
	public BeanRobot() {
		
		setTitle("数据库生成javabean小工具");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 544, 374);

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lblIp = new JLabel("IP:");
		lblIp.setBounds(110, 13, 30, 15);
		panel.add(lblIp);

		ipFiled = new JTextField();
		ipFiled.setText("localhost");
		ipFiled.setBounds(146, 10, 147, 21);
		panel.add(ipFiled);
		ipFiled.setColumns(10);

		JLabel label = new JLabel("数据库:");
		label.setBounds(80, 41, 54, 15);
		panel.add(label);

//		String dbStyles[] = { };
//		dbBox = new JComboBox(dbStyles);
		dbBox = new JComboBox();
		dbBox.setBounds(146, 39, 147, 21);
		dbBox.setVisible(true);
		dbBox.setMaximumRowCount(3);
		panel.add(dbBox);

		JLabel dbNamelabel = new JLabel("数据库名:");
		dbNamelabel.setBounds(70, 67, 60, 20);
		panel.add(dbNamelabel);

		dbNameFiled = new JTextField();
		dbNameFiled.setBounds(146, 68, 147, 21);
		dbNameFiled.setText("test");
		panel.add(dbNameFiled);
		dbNameFiled.setColumns(10);

		JLabel userLabel = new JLabel("用户名:");
		userLabel.setBounds(80, 98, 54, 15);
		panel.add(userLabel);

		userField = new JTextField();
		userField.setText("root");
		userField.setBounds(145, 97, 148, 21);
		panel.add(userField);
		userField.setColumns(10);

		JLabel pwdLabel = new JLabel("密码:");
		pwdLabel.setBounds(95, 129, 54, 15);
		panel.add(pwdLabel);

		pwdField = new JTextField();
		pwdField.setText("root");
		pwdField.setBounds(145, 126, 147, 21);
		panel.add(pwdField);
		pwdField.setColumns(10);

		JLabel packLabel = new JLabel("包名:");
		packLabel.setBounds(95, 160, 54, 15);
		panel.add(packLabel);

		packField = new JTextField();
		packField.setText("");
		packField.setBounds(146, 155, 147, 21);
		panel.add(packField);
		packField.setColumns(10);

		JLabel catlogLabel = new JLabel("输出目录:");
		catlogLabel.setBounds(70, 193, 65, 15);
		panel.add(catlogLabel);

		catField = new JTextField();
		catField.setBounds(146, 190, 147, 21);
		panel.add(catField);
		catField.setColumns(10);

		checkBox = new JCheckBox("生成包结构目录");
		checkBox.setSelected(true);
		checkBox.setBounds(145, 220, 147, 23);
		panel.add(checkBox);


		JLabel mustdbLabel = new JLabel("* 选择数据库");
		mustdbLabel.setForeground(Color.RED);
		mustdbLabel.setBounds(303, 39, 176, 15);
		panel.add(mustdbLabel);
		JLabel mustIPLabel = new JLabel("* IP地址及端口号");
		mustIPLabel.setForeground(Color.RED);
		mustIPLabel.setBounds(303, 13, 176, 15);
		panel.add(mustIPLabel);

		JLabel mustPacklabel = new JLabel("* 包结构");
		mustPacklabel.setForeground(Color.RED);
		mustPacklabel.setBounds(303, 155, 79, 15);
		panel.add(mustPacklabel);

		JLabel catlabel = new JLabel("默认D:// ;注意格式");
		catlabel.setForeground(Color.RED);
		catlabel.setBounds(303, 193, 179, 15);
		panel.add(catlabel);

		
		JButton button = new JButton("查询");
		// 按钮增加动作执行go()方法
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				go();
				
			}
		});
		button.setBounds(145, 267, 93, 23);
		panel.add(button);

		JButton crButton = new JButton("生成Bean");
		// 按钮增加动作执行go()方法
		crButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				create();
				
			}
		});
		crButton.setBounds(280, 267, 93, 23);
		panel.add(crButton);

		
		// 增加关闭事件监听,关闭相关操作
		this.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				close();
				System.exit(0);
			}

		});
		//设置表格
		
		Object[][] tableData = {};
		tableModel = new MyTableModel(tableData,titles); 
		jtable = new JTable(this.tableModel) ;
		TableColumn column=jtable.getColumnModel().getColumn(0);
		column.setPreferredWidth(30);							//设置第一列的宽度30
		JScrollPane scr = new JScrollPane(this.jtable) ;
		scr.setBounds(430, 10, 200, 290);
		panel.add(scr);
		//添加标格监听事件
		jtable.addMouseListener(new MouseAdapter(){
		    public void mouseClicked(MouseEvent e){
		    	  int row=jtable.getSelectedRow();  
		            if(jtable.getSelectedColumn()==0)//如果是第一列的单元格,则返回,不响应点击
		                return;
		         //列响应操作
		    }
		});
		
		//显示操作信息label
		labelInfo = new JLabel("");  
		labelInfo.setForeground(Color.RED);  
		labelInfo.setBounds(20, 317, 600, 60);  
        panel.add(labelInfo); 
        
		//初始化配置信息和数据库下拉列表
		dbMap = dbutil.getDbConfigMap();
		for(String key : dbMap.keySet()){
			this.getDbBox().addItem(key);
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 创建对象
		BeanRobot dtb = new BeanRobot();
		// 设置可见
		dtb.setVisible(true);
		// 点击X关闭窗口
		dtb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 调用设置居中显示
		dtb.setSizeAndCentralizeMe(680, 440);

	}

	// 设置居中
	private void setSizeAndCentralizeMe(int width, int height) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(width, height);
		this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
				/ 2 - height / 2);
	}

	public void create(){
		String sucessName = "";
		this.getLabelInfo().setText("");
		initInfo();
		//是否勾选生成目录标识
		boolean createPackage = this.getCheckBox().getSelectedObjects() != null;

		//判断目录是否存在
		if (dbInfoMap.get("catName") != null ) {
			if(dbInfoMap.get("catName").toString().equals("")){
				dbInfoMap.put("catName", "D://");
			}
			if((new File(dbInfoMap.get("catName").toString()).isDirectory())!=true){
				this.getLabelInfo().setText("目录不存在,请重新输入");
			}else{
				if(createPackage){
					if (dbInfoMap.get("packName") != null && !dbInfoMap.get("packName").toString().equals("")) {
					    String catPack = dbInfoMap.get("catName").toString()+dbInfoMap.get("packName").toString();
					    catPack = catPack.replace(".", "/");
						new File(catPack).mkdirs();
						dbInfoMap.put("catName", catPack);
					}
					//勾选
					int rowCount = this.jtable.getRowCount();
					for(int i = 0 ; i< rowCount ; i++){
						if(this.jtable.getValueAt(i,0).toString().equals("true")){
							String tabName = this.jtable.getValueAt(i,1).toString();
							List<Map<String, String>> tabFileds = dbutil.getColumnNames(dbInfoMap, tabName);
							butil.createBean(tabName, tabFileds,dbInfoMap);
							sucessName+=tabName+",";
						}
					}
				}else{
					int rowCount = this.jtable.getRowCount();
					for(int i = 0 ; i< rowCount ; i++){
						if(this.jtable.getValueAt(i,0).toString().equals("true")){
							String tabName = this.jtable.getValueAt(i,1).toString();
							List<Map<String, String>> tabFileds = dbutil.getColumnNames(dbInfoMap, tabName);
							butil.createBean(tabName, tabFileds,dbInfoMap);
							sucessName+=tabName+",";
						}
					}
					
				}
				this.getLabelInfo().setText("表"+sucessName+"生成成功");
			}
		}
	}
	
	public void go() {
		this.getLabelInfo().setText("");
		initInfo();
		String selTableStr = dbInfoMap.get("showTable").toString();
		//获取表名
		List<String> tableList = dbutil.getTableNames(dbInfoMap,dbInfoMap.get("dbName").toString());
		if(tableList == null){
			int rowCount = this.getTableModel().getRowCount();
			int delInd = 0;
			while(delInd < rowCount){
				this.getTableModel().removeRow(0);
				delInd++;
			}
			this.getLabelInfo().setText("数据库连接异常");
		}else{
			int rowCount = this.getTableModel().getRowCount();
			int delInd = 0;
			while(delInd < rowCount){
				this.getTableModel().removeRow(0);
				delInd++;
			}
			for(String tName : tableList){
				Object[] rowData = {new Boolean(false),tName};
				this.getTableModel().addRow(rowData);
			}

			
		}

	}

	public void initInfo(){
		//读取配置文件数据库配置
		String user = this.getUserField().getText();
		String pass = this.getPwdField().getText();
		String ip = this.getIpFiled().getText();
		String database = this.getDbNameFiled().getText();
		String dbName = this.getDbBox().getSelectedItem().toString();
		String packName =this.getPackField().getText();
		String catName =this.getCatField().getText();
		//处理界面数据
		dbInfoMap = new HashMap();
		dbInfoMap = dbMap.get(dbName);
		dbInfoMap.put("userName", user);
		dbInfoMap.put("userpwd", pass);
		dbInfoMap.put("jdbc", dbMap.get(dbName).get("JdbcURL")+ip+dbMap.get(dbName).get("dbStr")+database);
		dbInfoMap.put("driver", dbMap.get(dbName).get("driverClassName"));
		dbInfoMap.put("dbName", database);
		dbInfoMap.put("packName", packName);
		dbInfoMap.put("catName", catName);

		//Edit By HuangJun 2014-07-07
		System.out.println(dbInfoMap);
		
		/*
		 * MYSQL
		 * dataSource.driverClassName=com.mysql.jdbc.Driver
		 * dataSource.url=jdbc:mysql://192.168.12.55/fzrating?characterEncoding=utf8
		 * dataSource.username= fzratingtest
		 * dataSource.password= fzratingtest
		 * {userpwd=fzratingtest, dbName=fzrating, JdbcURL=jdbc:mysql://, catName=f:\, userName=fzratingtest, driverClassName=com.mysql.jdbc.Driver, driver=com.mysql.jdbc.Driver, dbStr=/, showTable=select table_name from information_schema.tables where table_schema = '%', packName=davaDemo, showColumns=show columns from %, jdbc=jdbc:mysql://192.168.12.55:3306/fzrating}
		 */
		
		
		/*
		 * SQlServer
		 * dataSource.driverClassName=net.sourceforge.jtds.jdbc.Driver
		 * dataSource.url=jdbc:jtds:sqlserver://192.168.12.54:1433/FZ_DEV
		 * dataSource.username= fz_dev
		 * dataSource.password= fz_dev
		 * {userpwd=fz_dev, dbName=fz_dev, JdbcURL=jdbc:sqlserver://, catName=f:\, userName=fz_dev, driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver, driver=com.microsoft.sqlserver.jdbc.SQLServerDriver, dbStr=;databaseName=, showTable=select Name from sysobjects where xtype='u' and status>=0, packName=javaDemo, showColumns=select column_name,data_type from information_schema.columns where table_name = '%', jdbc=jdbc:sqlserver://192.168.12.54:1433;databaseName=fz_dev}
		 */
		
		/* Oracle
		 * dataSourceforSe.driverClassName=oracle.jdbc.driver.OracleDriver
		 * dataSourceforSe.url=jdbc:oracle:thin:@192.168.12.230:1521:demo230
		 * dataSourceforSe.username=ccxe
		 * dataSourceforSe.password=ccxe
		 * {userpwd=ccxe, dbName=demo230, JdbcURL=jdbc:oracle:thin:@, catName=f:\, userName=ccxe, driverClassName=oracle.jdbc.driver.OracleDriver, driver=oracle.jdbc.driver.OracleDriver, dbStr=:, showTable=SELECT TABLE_NAME FROM USER_TABLES, packName=javaDemo, showColumns=select table_name from dba_tables where owner='CCXE', jdbc=jdbc:oracle:thin:@192.168.12.230:1521:demo230}
		 */	
		
	}
	
	private void close() {
		System.out.println("关闭事件");
	}

	public JTextField getIpFiled() {
		return ipFiled;
	}

	public void setIpFiled(JTextField ipFiled) {
		this.ipFiled = ipFiled;
	}

	public JTextField getDbFiled() {
		return dbFiled;
	}

	public void setDbFiled(JTextField dbFiled) {
		this.dbFiled = dbFiled;
	}

	public JTextField getTabField() {
		return tabField;
	}

	public void setTabField(JTextField tabField) {
		this.tabField = tabField;
	}

	public JTextField getPackField() {
		return packField;
	}

	public void setPackField(JTextField packField) {
		this.packField = packField;
	}

	public JTextField getCatField() {
		return catField;
	}

	public void setCatField(JTextField catField) {
		this.catField = catField;
	}

	public JCheckBox getCheckBox() {
		return checkBox;
	}

	public void setCheckBox(JCheckBox checkBox) {
		this.checkBox = checkBox;
	}

	public JTextField getUserField() {
		return userField;
	}

	public void setUserField(JTextField userField) {
		this.userField = userField;
	}

	public JTextField getPwdField() {
		return pwdField;
	}

	public void setPwdField(JTextField pwdField) {
		this.pwdField = pwdField;
	}

	public JTextField getDbNameFiled() {
		return dbNameFiled;
	}

	public void setDbNameFiled(JTextField dbNameFiled) {
		this.dbNameFiled = dbNameFiled;
	}

	public JComboBox getDbBox() {
		return dbBox;
	}

	public void setDbBox(JComboBox dbBox) {
		this.dbBox = dbBox;
	}

	public JLabel getLabelInfo() {
		return labelInfo;
	}

	public void setLabelInfo(JLabel labelInfo) {
		this.labelInfo = labelInfo;
	}

	public JTable getJtable() {
		return jtable;
	}

	public void setJtable(JTable jtable) {
		this.jtable = jtable;
	}

	public MyTableModel getTableModel() {
		return tableModel;
	}

	public void setTableModel(MyTableModel tableModel) {
		this.tableModel = tableModel;
	}

	class MyTableModel extends DefaultTableModel{ 
		  public MyTableModel(Object[][] data,String[] columns)
	        { 
			  super(data,columns); 
	        } 
	        public boolean isCellEditable(int row,int column){ //设置Table单元格是否可编辑
	            if(column==0)return true;        
	            return false; 
	        } 
	        public Class<?> getColumnClass(int columnIndex) 
	        { 
	            if(columnIndex==0)
	            { return Boolean.class; 
	            } 
	            return Object.class; 
	        } 
	  
	  }
}

 

2.BeanUtil.java代码

package dbRobot;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanUtil {


	public BeanUtil() {
		// TODO Auto-generated constructor stub
	}
	//创建JavaBean文件
	public String createBean(String tbName, List<Map<String, String>> collist,
			Map<String, String> infoMap) {
		
		StringBuilder fields = new StringBuilder();
		StringBuilder methods = new StringBuilder();

		StringBuilder classInfo = new StringBuilder("\t/**\r\n\t*");
		for (Map<String, String> colmap : collist) {
			String field = colmap.get("filed").toString();
			String type = typeTrans(colmap.get("type").toString());
			fields.append(getFieldStr(field, type));
			methods.append(getMethodStr(field, type));

		}
		classInfo.append("\t*@author yangsj");
		classInfo.append("\r\n\t*/\r\n\r\n");
		classInfo.append("\tpublic class ").append(upperFirestChar(tbName))
				.append("{\r\n");
		classInfo.append(fields);
		classInfo.append("\r\n");
		classInfo.append(methods);
		classInfo.append("\r\n");
		classInfo.append("}");
		File file = new File(infoMap.get("catName"), upperFirestChar(tbName) + ".java");
		try {
			FileWriter fw = new FileWriter(file);
			if (infoMap.get("packName") == null || infoMap.get("packName").toString().equals("")) {
				
			} else {
				String packageinfo =  "package " + infoMap.get("packName").toString() + ";\r\n\r\n";
				fw.write(packageinfo);
			}
			
			fw.write(classInfo.toString());
			fw.flush();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	//数据库字段类型与JAVA类型转换
	public String typeTrans(String type) {
		if (type.contains("tinyint")) {
			return "boolean";
		} else if (type.contains("int")) {
			return "int";
		} else if (type.contains("datetime")) {
			return "Date";
		} else if (type.contains("BIGINT")) {
			return "Long";
		} else if (type.contains("varchar") || type.contains("date")
				|| type.contains("time") || type.contains("timestamp")
				|| type.contains("text") || type.contains("enum")
				|| type.contains("set")) {
			return "String";
		} else if (type.contains("binary") || type.contains("blob")) {
			return "byte[]";
		} else {
			return "String";
		}
	}
	//获取方法字符串 
	private String getMethodStr(String field, String type) {
		StringBuilder get = new StringBuilder("\tpublic ");
		get.append(type).append(" ");
		if (type.equals("boolean")) {
			get.append(field);
		} else {
			get.append("get");
			get.append(upperFirestChar(field));
		}
		get.append("(){").append("\r\n\t\treturn this.").append(field)
				.append(";\r\n\t}\r\n");
		StringBuilder set = new StringBuilder("\tpublic void ");

		if (type.equals("boolean")) {
			set.append(field);
		} else {
			set.append("set");
			set.append(upperFirestChar(field));
		}
		set.append("(").append(type).append(" ").append(field)
				.append("){\r\n\t\tthis.").append(field).append("=")
				.append(field).append(";\r\n\t}\r\n");
		get.append(set);
		return get.toString();
	}

	//首字母大写
	public String upperFirestChar(String src) {
		return src.substring(0, 1).toUpperCase().concat(src.substring(1));
	}

	//获取字段
	private String getFieldStr(String field, String type) {
		StringBuilder sb = new StringBuilder();
		sb.append("\t").append("private ").append(type).append(" ")
				.append(field).append(";");
		sb.append("\r\n");
		return sb.toString();
	}

}

 

3.DbUtil.java

package dbRobot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;


public class DbUtil {
	public Statement st ;
	
	public DbUtil(){
		
	}
	/**
	 * paraMap参数
	 * driverClassName 驱动
	 * JdbcURL  jdbc    该字符串为:  config.ini 的JdbcURL+IP地址(含端口号)+dbStr+数据库名称
	 * userName 数据库名称
	 * userpwd 数据库密码
	 * */
	public  Statement getStatement(Map<String,String> paraMap) {
		//加载数据库驱动  
		try {
			Class.forName(paraMap.get("driver"));
			Connection conn = DriverManager.getConnection(paraMap.get("jdbc"), paraMap.get("userName"), paraMap.get("userpwd"));
				if(!conn.isClosed()){
				}else{
					return null;
				}
				// statement用来执行SQL语句
				Statement statement = conn.createStatement();
				return statement;
				// 要执行的SQL语句
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return null;
		}  
	}
	//读取数据库表信息
	public List<String> getTableNames(Map<String,String> dbInfoMap,String dbName){
		List<String> tableList = new ArrayList<String>(); //存储表名
		try {
		st = this.getStatement(dbInfoMap);
		if(st == null){
			return null;
		}
		//替换数据库名字占位符
		String selTableSql = dbInfoMap.get("showTable").toString().replace("%", dbName);
		ResultSet tabRs = st.executeQuery(selTableSql);
			//保存表名
			while(tabRs.next()){
				tableList.add(tabRs.getString(1));
			}
		}catch (SQLException e) {
				// TODO Auto-generated catch block
			return null;
		}
		return tableList;
	}
	//读取表字段信息
	public List<Map<String,String>> getColumnNames(Map<String,String> dbInfoMap,String tabName){
		List<Map<String,String>> colList = new ArrayList<Map<String,String>>(); //存储字段信息
		try {
		st = this.getStatement(dbInfoMap);
		if(st == null){
			return null;
		}
		//替换表名占位符
		String selColumnSql = dbInfoMap.get("showColumns").toString().replace("%", tabName);
		ResultSet columnRs = st.executeQuery(selColumnSql);
			while(columnRs.next()){
				Map<String,String> colMap =  new HashMap<String,String>();
				colMap.put("filed", columnRs.getString(1));
				colMap.put("type", columnRs.getString(2));
				colList.add(colMap);
			}
		}catch (SQLException e) {
				// TODO Auto-generated catch block
			return null;
		}
		return colList;
	}
	
	// 读取配置
	public Map<String,  HashMap<String, String>> getDbConfigMap() {
	    Map<String, HashMap<String, String>> sectionsMap = new HashMap<String, HashMap<String, String>>();  
	    HashMap<String, String> itemsMap = new HashMap<String, String>();  
	    String currentSection = "";
	    BufferedReader reader = null;
		try {
			//读取当前文件路径下的ini文件
			System.out.println(this.getClass().getResourceAsStream("config.ini"));
			 InputStream configStream = this.getClass().getResourceAsStream("config.ini");
			 reader = new BufferedReader(new InputStreamReader(configStream, "gbk"));


			String line = null;
			while ((line = reader.readLine()) != null) {
				 line = line.trim();  
	                if ("".equals(line))  
	                    continue;  
	                if (line.startsWith("[") && line.endsWith("]")) { 
	                	 itemsMap = new HashMap<String, String>();
	                	 currentSection = line.substring(1, line.length() - 1);  
	                     sectionsMap.put(currentSection, itemsMap);  
	                     currentSection = "";  
	                }else{
	                	   int index = line.indexOf("=");  
	                       if (index != -1) {  
	                           String key = line.substring(0, index);  
	                           String value = line.substring(index + 1, line.length());  
	                           itemsMap.put(key, value.trim());  
	                       }  
	                }
			}
			 reader.close();  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }
             return sectionsMap;
            }
	}
}

 

4.数据库连接字符串

/*
 * MYSQL
 * dataSource.driverClassName=com.mysql.jdbc.Driver
 * dataSource.url=jdbc:mysql://192.168.12.55/fzrating?characterEncoding=utf8
 * dataSource.username= fzratingtest
 * dataSource.password= fzratingtest
 * {userpwd=fzratingtest, dbName=fzrating, JdbcURL=jdbc:mysql://, catName=f:\, userName=fzratingtest, driverClassName=com.mysql.jdbc.Driver, driver=com.mysql.jdbc.Driver, dbStr=/, showTable=select table_name from information_schema.tables where table_schema = '%', packName=davaDemo, showColumns=show columns from %, jdbc=jdbc:mysql://192.168.12.55:3306/fzrating}
*/
		
/*
 * SQlServer
 * dataSource.driverClassName=net.sourceforge.jtds.jdbc.Driver
 * dataSource.url=jdbc:jtds:sqlserver://192.168.12.54:1433/FZ_DEV
 * dataSource.username= fz_dev
 * dataSource.password= fz_dev
 * {userpwd=fz_dev, dbName=fz_dev, JdbcURL=jdbc:sqlserver://, catName=f:\, userName=fz_dev, driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver, driver=com.microsoft.sqlserver.jdbc.SQLServerDriver, dbStr=;databaseName=, showTable=select Name from sysobjects where xtype='u' and status>=0, packName=javaDemo, showColumns=select column_name,data_type from information_schema.columns where table_name = '%', jdbc=jdbc:sqlserver://192.168.12.54:1433;databaseName=fz_dev}
*/

/* Oracle
 * dataSourceforSe.driverClassName=oracle.jdbc.driver.OracleDriver
 * dataSourceforSe.url=jdbc:oracle:thin:@192.168.12.230:1521:demo230
 * dataSourceforSe.username=ccxe
 * dataSourceforSe.password=ccxe
 * {userpwd=ccxe, dbName=demo230, JdbcURL=jdbc:oracle:thin:@, catName=f:\, userName=ccxe, driverClassName=oracle.jdbc.driver.OracleDriver, driver=oracle.jdbc.driver.OracleDriver, dbStr=:, showTable=SELECT TABLE_NAME FROM USER_TABLES, packName=javaDemo, showColumns=select table_name from dba_tables where owner='CCXE', jdbc=jdbc:oracle:thin:@192.168.12.230:1521:demo230}
*/

 

5.config.ini 数据库配置

[MySQL]  
driverClassName=com.mysql.jdbc.Driver 
JdbcURL=jdbc:mysql://
dbStr=/
showTable=select table_name from information_schema.tables where table_schema = '%'
showColumns=show columns from %
[SQL_Server]
driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
JdbcURL=jdbc:sqlserver://
dbStr=;databaseName=
showTable=select Name from sysobjects where xtype='u' and status>=0
showColumns= select column_name,data_type from information_schema.columns where table_name = '%'
[ORACLE]
driverClassName=oracle.jdbc.driver.OracleDriver
JdbcURL=jdbc:oracle:thin:@
dbStr=:
showTable=SELECT TABLE_NAME FROM USER_TABLES
showColumns= select column_name,data_type from user_tab_columns where Table_Name='%' 

 

6.截图

MySQL.jpg

 

SQL_Server.jpg

 

Oracle.jpg

 

 

 参考了:

http://www.oschina.net/code/snippet_128886_26726

http://marsvaadin.iteye.com/blog/1465592  相关内容。

  • 大小: 71.5 KB
  • 大小: 71.8 KB
  • 大小: 70.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics