A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 霍振鹏 中级黑马   /  2014-4-9 12:20  /  547 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 霍振鹏 于 2014-4-12 20:55 编辑

为什么会出现异常:
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;

  6. public final class JDBCUtils {
  7.         private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  8.         private static final String user = "scott";
  9.         private static final String pass = "tiger";
  10.         static {
  11.                 try {
  12.                         Class.forName("oracle.jdbc.driver.OracleDriver");
  13.                 } catch (ClassNotFoundException e) {
  14.                         // TODO Auto-generated catch block
  15.                         e.printStackTrace();
  16.                 }
  17.         }

  18.         private JDBCUtils() {
  19.         }

  20.         public static Connection getConnection() throws SQLException {

  21.                 return DriverManager.getConnection(url, user, pass);

  22.         }

  23.         public static void free(ResultSet rs, Statement sm, Connection conn) {
  24.                 try {
  25.                         if (rs != null) {
  26.                                 rs.close();
  27.                         }
  28.                 } catch (SQLException e) {

  29.                 } finally {
  30.                         try {
  31.                                 if (sm != null) {
  32.                                         sm.close();
  33.                                 }
  34.                         } catch (SQLException e) {

  35.                         } finally {
  36.                                 try {
  37.                                         if (conn != null) {
  38.                                                 conn.close();
  39.                                         }
  40.                                 } catch (SQLException e) {

  41.                                 }
  42.                         }
  43.                 }
  44.         }

  45. }
复制代码

  1. public class ConnectionOracle {

  2.         /**
  3.          * @param args
  4.          */

  5.         public static void main(String[] args) throws SQLException {
  6.                 // TODO Auto-generated method stub
  7.                 //selectStatement();
  8.                 createa();

  9.         }
  10.         static void createa() throws SQLException
  11.         {
  12.                 Connection conn = null;
  13.                 Statement sm = null;
  14.                 ResultSet rs = null;
  15.                 try {

  16.                         conn = JDBCUtils.getConnection();
  17.                         // 创建语句
  18.                         sm = conn.createStatement();
  19.                         // 执行语句
  20.                         rs = sm.executeQuery("select name,age,address from student");//////这里最好写上列名,有时候数据库中的列有很多
  21.                

  22.                         // 输出结果

  23.                         while (rs.next()) {

  24.                         //        System.out.println(rs.getObject(1) + "\t" + rs.getObject(2)
  25.                                 //                + "\t" + rs.getObject(3));//////getObject方法还有另一种重载形式,传递的是一个String类型,就是要列的名字,这种方式更加易读
  26.          System.out.println(rs.getObject("name")+"\t"+rs.getObject("age"));

  27.                         }
  28.                 } finally {
  29.                         JDBCUtils.free(rs, sm, conn);
  30.                 }
  31.         }
复制代码

C:\Users\hupzhenpeng\Desktop\123654.png
C:\Users\hupzhenpeng\Desktop\456789.png

123654.png (75.26 KB, 下载次数: 9)

123654.png

456789.png (5.44 KB, 下载次数: 4)

456789.png

1 个回复

正序浏览
求解。。。。。。。。。。。在线等大神降临!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马