oracle數據庫

1.安裝。沒有什麼難的,記住自己安裝設置的密碼就行,此處略去不提。

image

前兩個是服務器的下載(第一個是隻能用英語,第二個是多國語言),第三個是客戶端,下面是安裝嚮導什麼的相關鏈接。(英語好的人基本上不用我介紹)

這裏我想說爲什麼選擇這個版本,因爲它很小,非常適合做實驗,這也是oracle在看到mysql的流行而及時跟進的一個發展,大公司就是好啊,與時俱進!!

2.

1 Logging in as the Database Administrator

The first thing you need to do is to log in as the Oracle Database XE Administrator. Follow these steps:

  1. Open the Database Home Page login window:

    • On Windows, from the Start menu, select Programs (or All Programs), then Oracle Database 10g Express Edition, and then Go To Database Home Page.

    • On Linux, click the Application menu (on Gnome) or the K menu (on KDE), then point to Oracle Database 10g Express Edition, and then Go To Database Home Page.

  2. At the Database Home Page login window, enter the following information:

    • Username: Enter system for the user name.

    • Password: Enter the password that was specified when Oracle Database XE was installed.

  3. Click Login.

    The Oracle Database XE home page appears.

    Description of gs_home_page.gif follows

2 Unlocking the Sample User Account

To create your application, you need to log in as a database user. Oracle Database XE comes with a sample database user called HR. This user owns a number of database tables in a sample schema that can be used to create applications for a fictional Human Resources department. However, for security reasons, this user's account is locked. You need to unlock this account before you can build a sample application.

To unlock the sample user account:

  1. Make sure you are still logged on as the database administrator, as described in the previous section.

  2. Click the Administration icon, and then click Database Users.

  3. Click the HR schema icon to display the user information for HR.

    HR user icon
    Under Manage Database User, enter the following settings:

    • Password and Confirm Password: Enter hr for the password.

    • Account Status: Select Unlocked.

    • Roles: Ensure that both CONNECT and RESOURCE are enabled.

  4. Click Alter User.

Now you are ready to create your first application.

3 Logging in as the Sample User Account

To log in as the sample user account:

  1. Log out from the database administrator account by clicking Logout in the upper right corner of the Database Home Page.

  2. In the window, click Login.

  3. In the Login window, enter hr for both the user name and password.

  4. Click Login.

  5. 4.Specifying Database URLs.

  6. These are a few simple things that you should do in your JDBC program:

    1. Import the necessary JDBC classes in your programs that use JDBC.
        For example:

          import java.sql.*;
          import java.math.*; // if needed

        To use OracleDataSource, you need to do:
          import oracle.jdbc.pool.OracleDataSource;

    2. Create an OracleDataSource instance.

          OracleDataSource ods = new OracleDataSource();

    3. set the desired properties if you don't want to use the
        default properties. Different connection URLs should be
        used for different JDBC drivers.

          ods.setUser("my_user");
          ods.setPassword("my_password");

        For the JDBC OCI Driver:
          To make a bequeath connection, set URL as:
          ods.setURL("jdbc:oracle:oci:@");

          To make a remote connection, set URL as:
          ods.setURL("jdbc:oracle:oci:@<database>");

  7. 此處的databa是由l“ocalhost(ip):1521(port)”組成。

          where <database> is either a TNSEntryName
          or a SQL*net name-value pair defined in tnsnames.ora.
        For the JDBC Thin Driver, or Server-side Thin Driver:
          ods.setURL("jdbc:oracle:thin:@<database>");

          where <database> is either a string of the form
          //<host>:<port>/<service_name>, or a SQL*net name-value pair,
          or a TNSEntryName.

        For the JDBC Server-side Internal Driver:
          ods.setURL("jdbc:oracle:kprb:");

          Note that the trailing ':' is necessary. When you use the
          Server-side Internal Driver, you always connect to the
          database you are executing in. You can also do this:

          Connection conn =
            new oracle.jdbc.OracleDriver().defaultConnection();

    4. Open a connection to the database with getConnection()
        methods defined in OracleDataSource class.

          Connection conn = ods.getConnection();

    The Old oracle.jdbc.driver Package Will Go Away !!!
    ---------------------------------------------------

    Beginning in Oracle 9i, Oracle extensions to JDBC are captured in
    the package oracle.jdbc.  This package contains classes and
    interfaces that specify the Oracle extensions in a manner similar
    to the way the classes and interfaces in java.sql specify the
    public JDBC API.

    The use of the package oracle.jdbc.driver has been deprecated
    since the initial version of 9i.  Your code should use the package
    oracle.jdbc instead.  New features since Oracle 9i are incompatible
    with use of the package oracle.jdbc.driver.  Although we continue
    to support the old package oracle.jdbc.driver in this release to
    provide backwards compatibility, the package will definitely be
    removed in the next major release.  If you still have existing
    applications that use the old oracle.jdbc.driver package, now is the
    time to convert your code.

    All that is required to covert your code is to replace
    "oracle.jdbc.driver" with "oracle.jdbc" in the source and recompile.
    This cannot be done piecewise.  You must convert all classes
    and interfaces that are referenced by an application.

    代碼如下:

import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
public class TestNewMethodJDBC {
    public static void main(String[] args) throws Exception {
        OracleDataSource ods = new OracleDataSource();
        ods.setUser("panyu");
        ods.setPassword("123");
        ods.setURL("jdbc:oracle:thin:@localhost:1521");
        Connection conn = ods.getConnection();
    }
}

 

數據庫開發-jdbc

最簡單的例子:

import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
public class TestNewMethodJDBC {
    public static void main(String[] args)  {
        OracleDataSource ods;
        ResultSet rs = null;
        Statement stmt = null;
        Connection conn = null;
        try {
            ods = new OracleDataSource();
            ods.setUser("panyu");
            ods.setPassword("123");
            ods.setURL("jdbc:oracle:thin:@localhost:1521");
            conn = ods.getConnection();
            stmt = conn.createStatement();//創建statement
            rs = stmt.executeQuery("select * from dept");//執行sql語句返回結果集
            while(rs.next()) {
                System.out.println(rs.getString("id"));//遍歷結果集打印值
                }
            rs.close();//關閉數據庫
            conn.close();
            stmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ;
    }
}

最後將代碼完善

package com.myjdbc;

import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;

public class TestNewMethodJDBC {
    public static void main(String[] args) {
        OracleDataSource ods;
        ResultSet rs = null;
        Statement stmt = null;
        Connection conn = null;
        try {
            ods = new OracleDataSource();
            ods.setUser("panyu");
            ods.setPassword("123");
            ods.setURL("jdbc:oracle:thin:@localhost:1521");
            conn = ods.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from dept");
            while (rs.next()) {
                System.out.println(rs.getString("id"));

            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

可能是很小的改變,但是對你的面試和以後代碼的優美程度很有幫助。(雖然它沒什麼太大用)

其他的一些簡單的插入刪除什麼的語句基本上一直都沒變,有需要的自己上網上查(sql語言也是一樣)。

 

myeclipse和oracle的結合。我們在myeclipse上編程還得打開oracle去查詢結果很麻煩,於是我們讓myeclipse就可以查詢oracle的結果。這裏用db browser吧,其他的好像都不好使了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章