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吧,其他的好像都不好使了。

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