zoukankan      html  css  js  c++  java
  • JDBC(Java项目使用Oracle数据库)

    Java项目中使用Oracle数据库(Eclipse)


    前言

        这学期选了Oracle数据库这门课,于是自己下载了Oracle11gR2版本的数据库。在这之前我一直用的是MySQL。虽然两者教程差不多,但我还是想留个总结。

    参考资料


    操作步骤

    1. 新建一个Java项目;
      1
    2. 新建一个文件夹;
      2
    3. 找到"ojdbc6.jar"文件并复制,该文件路径:“D:Oracle11gDatabaseBaseDirproduct11.2.0dbhome_1jdbclibojdbc6_g.jar”,其中"D:Oracle11gDatabase"是我的安装Oracle的位置,依自己情况去找。备注:不是官网下载的那个,是安装的路径。
      注:ojdbc5.jar是支持jdk1.5的,ojdbc6.jar是支持jdk1.6的。我的jdk是1.8版本的。看了那个官网jdbc的文档,我只发现了这个(英语有点渣,可能我看掉了)。但是能够连接数据库,这个以后再看吧。
      3
    4. 将刚才的文件粘贴到新建的文件夹下;
      4
    5. 添加路径,右键点击文件;
      5
    6. 生成一个文件和jar文件
      6


    以上属于导入包部分,完成上述操作即可写项目。 下面两个步骤是验证是否成功。

    1. 在src下新建一个类,运行下面这段代码;
    注意:	1、类名一样,,,基础问题
    		2、第10行的"HR"换成"scott"
    		3、"hr"换成你自己的scott用户的密码,默认密码是"tiger"
    		4、"<host>"换成"localhost"
    		5、"<port>"换成"1521",不知道其他的行不行
    		6、"<service>"换成"orcl",这个是安装的时候设置的,依情况而定。
    
    import java.sql.*;
    import oracle.jdbc.*;
    import oracle.jdbc.pool.OracleDataSource;
    
    class JDBCVersion
    {
      public static void main (String args[]) throws SQLException
      {
        OracleDataSource ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:HR/hr@<host>:<port>:<service>");
        Connection conn = ods.getConnection();
    
        // Create Oracle DatabaseMetaData object
        DatabaseMetaData meta = conn.getMetaData();
    
        // gets driver info:
        System.out.println("JDBC driver version is " + meta.getDriverVersion());
      }
    }
    

    运行之后控制台输出版本号信息
    12去dos窗口进入数据库,会发现版本号一致
    在这里插入图片描述8、运行下面代码

    注意:	1、第一行写上你的包名,不然无法编译
    		2、代码下面有截图
    
    /*
     * This sample can be used to check the JDBC installation.
     * Just run it and provide the connect information. It will select
     * "Hello World" from the database.
     */
    
    // You need to import the java.sql and JDBC packages to use JDBC
    import java.sql.*;
    import oracle.jdbc.*;
    import oracle.jdbc.pool.OracleDataSource;
    
    // We import java.io to be able to read from the command line
    import java.io.*;
    
    class JdbcCheckup
    {
      public static void main(String args[]) throws SQLException, IOException
      {
    
        // Prompt the user for connect information
        System.out.println("Please enter information to test connection to 
                              the database");
        String user;
        String password;
        String database;
    
        user = readEntry("user: ");
        int slash_index = user.indexOf('/');
        if (slash_index != -1)
        {
          password = user.substring(slash_index + 1);
          user = user.substring(0, slash_index);
        }
        else
          password = readEntry("password: ");
        database = readEntry("database(a TNSNAME entry): ");
    
        System.out.print("Connecting to the database...");
        System.out.flush();
        System.out.println("Connecting...");
        // Open an OracleDataSource and get a connection
        OracleDataSource ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:oci:@" + database);
        ods.setUser(user);
        ods.setPassword(password);
        Connection conn = ods.getConnection();
        System.out.println("connected.");
    
        // Create a statement
        Statement stmt = conn.createStatement();
    
        // Do the SQL "Hello World" thing
        ResultSet rset = stmt.executeQuery("select 'Hello World' from dual");
    
        while (rset.next())
          System.out.println(rset.getString(1));
        // close the result set, the statement and the connection
        rset.close();
        stmt.close();
        conn.close();
        System.out.println("Your JDBC installation is correct.");
      }
    
      // Utility function to read a line from standard input
      static String readEntry(String prompt)
      {
        try
        {
          StringBuffer buffer = new StringBuffer();
          System.out.print(prompt);
          System.out.flush();
          int c = System.in.read();
          while (c != '
    ' && c != -1)
          {
            buffer.append((char)c);
            c = System.in.read();
          }
          return buffer.toString().trim();
        }
        catch(IOException e)
        {
          return "";
        }
      }
    }
    

    输入用户名,密码,数据库名称
    15出现截图最后三行说明已成功从数据库读取数据,即连接成功。

    如果有安装问题欢迎留言讨论。
    小提醒:官网下载数据库非常慢,建议从网盘下载。(ps:注册账号很简单)
    另外附一个Oracle数据库安装教程:腾讯视频Win7版的教程,我的是Win10,也是这么安装的,暂时没发现问题。


    THE END


  • 相关阅读:
    证明一下拉普拉斯的《概率分析论》观点
    Android实现小圆点显示未读功能
    命名 —— 函数的命名
    node.js 之爬虫
    ubuntu安装 tensorflow GPU
    古文(诗词文)—— 结构模式与复用
    Win10安装Ubuntu16.04 双系统
    python使用wget下载网络文件
    文字检测与识别资源
    10大深度学习架构:计算机视觉优秀从业者必备
  • 原文地址:https://www.cnblogs.com/wowpH/p/11060842.html
Copyright © 2011-2022 走看看