日期:2014-05-16 浏览次数:20568 次
package com; 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.sql.Statement; public class TestJDBC3 { public static void main(String[] args) { Connection conn = null; Statement stmt=null; ResultSet rs=null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url="jdbc:oracle:thin:@10.12.36.201:1521:xxxxxx"; String dbUsername="openlab"; String dbPassword="open123"; conn=DriverManager.getConnection(url, dbUsername, dbPassword); DatabaseMetaData dmd=conn.getMetaData(); System.out.println("数据库产品名称\t"+dmd.getDatabaseProductName()); System.out.println(dmd.getDatabaseProductVersion()); System.out.println(dmd.getDriverName()); System.out.println(dmd.getURL()); System.out.println(dmd.getUserName()); System.out.println(dmd.getStringFunctions()); //--------------------------------------------// String sql="select * from emp"; stmt=conn.createStatement(); rs=stmt.executeQuery(sql); ResultSetMetaData rsm=rs.getMetaData(); //结果集元数据获得 int count=rsm.getColumnCount(); //一共有多少列 stmt.setMaxFieldSize(8); for (int i = 1; i <count; i++) { System.out.print(rsm.getColumnName(i)+"\t"+"\t"); } System.out.println(); while(rs.next()){ System.out.print(rs.getInt("empno")+"\t\t"); System.out.print(rs.getString("ename")+"\t\t"); System.out.print(rs.getString("job")+"\t\t"); System.out.print(rs.getString("mgr")+"\t\t"); System.out.print(rs.getDate("hiredate")+"\t\t"); System.out.print(rs.getDouble("sal")+"\t\t"); System.out.println(rs.getDouble("comm")+"\t\t"); System.out.println(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally{ try { stmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { rs.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace();} } } }