日期:2014-05-17 浏览次数:20643 次
import java.sql.*; public class ConnectOracle { public static void main(String[] args) { Connection conn = null; PreparedStatement prstm = null; ResultSet rs = null; try { //------------------------------------------ Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@127.0.0.1:1521:xu"; // 数据库连接字符串 String user = "scott"; String password = "tiger"; conn = DriverManager.getConnection(url, user, password); // 获取连接 //------------------------------------------- prstm = conn.prepareStatement("SELECT * FROM BUDGET_BASE_DATA"); rs = prstm.executeQuery(); System.out.println("查询结果为:" ); while (rs.next()) { System.out.println(rs.getString("ID") +" "+ rs.getString("TYPE")); } prstm = conn.prepareStatement("create table zltest(aa number,bb varchar2(10))"); System.out.print("done"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 使用完毕进行相关内容的关闭,注意相关顺序 if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (prstm != null) { try { prstm.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }