DatabaseMetaData:用来分析是"数据库"信息
//做一个简单的 查询执行器
package com.enhance.jdbc; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.Properties; import java.util.Vector; import javax.swing.JButton; 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; public class QueryExecutor { JFrame jf = new JFrame("查询执行器"); private JScrollPane scrollPane; private DefaultTableModel model; private JButton execBn=new JButton("查询"); private JTextField sqlField=new JTextField(40); private static Connection conn; private static Statement stmt; private static ResultSet rs; static { try { Properties prop=new Properties(); FileInputStream in=new FileInputStream("src/mysql.ini"); prop.load(in); in.close(); String driver =prop.getProperty("driver"); String url=prop.getProperty("url"); String user=prop.getProperty("user"); String pass=prop.getProperty("pass"); Class.forName(driver); conn=DriverManager.getConnection(url,user,pass); stmt=conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } } class ExecListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(scrollPane!=null){ jf.remove(scrollPane); } try { rs=stmt.executeQuery(sqlField.getText()); System.out.println(); ResultSetMetaData rsmd=rs.getMetaData(); VectorcolumnNames=new Vector (); System.out.println("cn"+rsmd.getColumnCount()); Vector data=new Vector(); for (int i = 0; i < rsmd.getColumnCount(); i++) { columnNames.add(rsmd.getColumnName(i+1)); } while(rs.next()){ Vector v=new Vector(); for (int i = 0; i < rsmd.getColumnCount(); i++) { v.add(rs.getString(i+1)); } data.add(v); } model = new DefaultTableModel(data,columnNames); JTable table=new JTable(model); scrollPane=new JScrollPane(table); jf.add(scrollPane); jf.validate(); } catch (Exception e2) { e2.printStackTrace(); } } } public void init(){ JPanel top=new JPanel(); top.add(new JLabel("输入查询语句:")); top.add(sqlField); top.add(execBn); execBn.addActionListener(new ExecListener()); sqlField.addActionListener(new ExecListener()); jf.add(top,BorderLayout.NORTH); jf.setSize(640,480); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } public static void main(String[] args) { new QueryExecutor().init(); } }
//分析数据库信息
package com.enhance.jdbc; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Properties; public class DataBaseMetaDataTest { private String driver; private String url; private String user; private String pass; private Connection conn; private ResultSet rs; public void initParam(String paramFile) throws Exception{ Properties prop=new Properties(); prop.load(new FileInputStream(paramFile)); driver=prop.getProperty("driver"); url=prop.getProperty("url"); user=prop.getProperty("user"); pass=prop.getProperty("pass"); } public void info() throws Exception{ try { Class.forName(driver); conn=DriverManager.getConnection(url,user,pass); //获取DataBaseMeteData对象 DatabaseMetaData dbmd=conn.getMetaData(); //获取MySql支持的所有类型 ResultSet rs=dbmd.getTableTypes(); System.out.println("----MySql支持的表的类型信息----"); printResultSet(rs); //获取当前数据库的全部数据表 rs=dbmd.getTables(null,null, "%", new String[]{"TABLE"}); System.out.println("----当前数据库中的所有表信息----"); printResultSet(rs); rs=dbmd.getPrimaryKeys(null, null, "my_test"); System.out.println("----my_test表中的主键信息----"); printResultSet(rs); rs=dbmd.getProcedures(null, null, "%"); System.out.println("----当前数据库中所有存储过程信息----"); printResultSet(rs); rs=dbmd.getCrossReference(null, null, "time_zone", null, null, "time_zone_name"); System.out.println("----time_zone表和time_zone_name表之间的外键约束----"); printResultSet(rs); rs=dbmd.getColumns(null, null, "my_test", "%"); System.out.println("----my_test表的全部数据列----"); printResultSet(rs); System.out.println(dbmd.getDatabaseProductName()); System.out.println(dbmd.getDatabaseProductVersion()); System.out.println(dbmd.getDriverName()); System.out.println(dbmd.getDriverVersion()); } finally{ if(rs!=null) rs.close(); if(conn!=null) conn.close(); } } public void printResultSet(ResultSet rs) throws SQLException{ ResultSetMetaData rsmd=rs.getMetaData(); for (int i = 0; i < rsmd.getColumnCount(); i++) { System.out.print(rsmd.getColumnName(i+1)+"\t"); } System.out.println(); while(rs.next()){ for (int i = 0; i < rsmd.getColumnCount(); i++) { System.out.print(rs.getString(i+1)+"\t"); } System.out.println(); } rs.close(); } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub DataBaseMetaDataTest dbmdt=new DataBaseMetaDataTest(); dbmdt.initParam("src/mysql.ini"); dbmdt.info(); } }