zoukankan      html  css  js  c++  java
  • Java-jdbc-oracle

    JDBC编程步骤:
    ----java与Oracle数据的连接(lomboz_eclipse环境下)
      1.在Oracle数据库安装文件夹中找到jdbc文件夹→lib文件夹→classesl2.jar
      2.lomboz_eclipse中导入此Jar包
        导入方法:
               建立一个项目,在项目名称上右键鼠标选择Build Path→Add External Archives→
               选择classesl2.jar进行导入

      3.新建一程序编写与Oracle连接的代码
        步骤如下:
                1.实例话驱动类
              class.forName("Oracle.jdbc.driver.OracleDriver");
                2.建立到数据库的连接
              Connection conn = DriverManager.getConnection("jdbc:oracle:thin:
                                @192.168.8.1:1521:yuewei","scott","tiger");
                3.将数据发送到数据库中
              Statement stm = conn.CreatStatement();
                4.执行语句(select语句)
              ResultSet rs = stm.executeQuery(select * from dept);
                5.显示语句
              rs.getString("deptno");

    完整代码如下:

    import java.sql.*;
    
    public class TestJDBC {
    
     public static void main(String[] args) {
      ResultSet rs = null;
      Statement stmt = null;
      Connection conn = null;
      try {
       Class.forName("oracle.jdbc.driver.OracleDriver");
       //new oracle.jdbc.driver.OracleDriver();
       conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:yuewei", "scott", "tiger");
       stmt = conn.createStatement();
       rs = stmt.executeQuery("select * from dept");
       while(rs.next()) {
        System.out.println(rs.getString("deptno"));
        //System.out.println(rs.getInt("deptno"));
       }
      } catch (ClassNotFoundException e) {
       e.printStackTrace();
      } catch (SQLException e) {
       e.printStackTrace();
      } finally {
       try {
        if(rs != null) {
         rs.close();
         rs = null;
        }
        if(stmt != null) {
         stmt.close();
         stmt = null;
        }
        if(conn != null) {
         conn.close();
         conn = null;
        }
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
     }
    
    }
  • 相关阅读:
    ORACLE之常用FAQ V1.0二(构架系统) (1)
    如何快速杀去世占用过多本钱(CPU,内存)的数据库历程
    Eclipse快捷键大全
    (收藏)STL MAP 详解
    怎么在安装目录下创建一个文件
    MFC自绘属性的总结
    文件夹总结
    公元纪年转换为干支纪年
    重载运算符operator
    VS2005快捷键大全
  • 原文地址:https://www.cnblogs.com/lzhat/p/4357237.html
Copyright © 2011-2022 走看看