简体   繁体   English

Oracle 11g Express Java JDBC; 数据库工作区

[英]Oracle 11g Express Java JDBC; Database Workspace

I am experiencing some difficulty getting to the Oracle 11g Express Workspace in which resides my database. 我在访问数据库所在的Oracle 11g Express Workspace时遇到了一些困难。

I am able to register my JDBC Driver via Class.forName("jdbc.oracle.driver.OracleDriver") and to connect successfully via Connection con = DriverManager.getConnection("jdbc:oracle:thin:" + username + "/" + password + "@localhost:1521:xe") but not able to get into the workspace containing the database I created. 我可以通过Class.forName("jdbc.oracle.driver.OracleDriver")注册我的JDBC Driver ,并通过Connection con = DriverManager.getConnection("jdbc:oracle:thin:" + username + "/" + password + "@localhost:1521:xe")成功Connection con = DriverManager.getConnection("jdbc:oracle:thin:" + username + "/" + password + "@localhost:1521:xe")但无法进入包含我创建的数据库的工作区。

Any attempt to create a database via Statement smt = Connection.createStatement() yields a java.lang.UnsupportedOperationException . 任何通过Statement smt = Connection.createStatement()创建数据库的尝试都会产生java.lang.UnsupportedOperationException

I am not even sure if I am connected to the right place. 我甚至不确定我是否连接到正确的地方。 I know when I look into the Oracle Database 11.2 XE parameters it lists the db_name as xe and I can connect to it via the sys as sysdba and using the password I established for the Oracle 11g Express database but I can't get into the workspace when I had created a database and utilized ddl and dml statements to populate it. 我知道当我查看Oracle Database 11.2 XE参数时,它将db_name列为xe ,我可以通过sys as sysdba连接到它并使用我为Oracle 11g Express数据库建立的密码,但我无法进入工作空间当我创建一个数据库并使用ddldml语句来填充它时。

Perhaps I simply don't know what to do at this point because I am new to JDBC. 也许我根本不知道该做什么,因为我是JDBC的新手。 I checked my TNSNAMES.ORA file and indeed there is an XE entry representing my Oracle 11g Express as well as other entries representing databases in Oracle. 我检查了我的TNSNAMES.ORA文件,确实有一个XE条目代表我的Oracle 11g Express以及代表Oracle数据库的其他条目。 I think I am there I just don't know what to do to get to my XE database I created. 我想我在那里我只是不知道该怎么做才能进入我创建的XE数据库。

The code I am using to execute a SQL statement is as follows: 我用来执行SQL语句的代码如下:

    Statement smt = null;


    try {
        smt = connection.createStatement();

        String command = "CREATE table x(y varchar(2))";

        smt.executeLargeUpdate(command);

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage() + e.getCause());
    }

You need the ojbdc6.jar or equivalent on the classpath. 您需要类路径上的ojbdc6.jar或等效项。 It could also be a number of other problems, so I will detail a working example in the hope it will highlight the problem. 这也可能是其他一些问题,因此我将详细介绍一个有效的例子,希望能突出问题。

I am using the following configuration and components: 我使用以下配置和组件:

I perform the following steps to generate a connection to an oracle xe database from a java class: 我执行以下步骤以从java类生成与oracle xe数据库的连接:

Run the Oracle XE 32 bit for windows installer providing the password 'password'. 为Windows安装程序运行Oracle XE 32位,提供密码“password”。 Then run services.msc to confirm that the TNS listner and the XE services are both running. 然后运行services.msc以确认TNS列表器和XE服务都在运行。

在此输入图像描述

Then create a new java project in eclipse. 然后在eclipse中创建一个新的java项目。 Adding the ojbc6.jar to the build path as an external jar. 将ojbc6.jar作为外部jar添加到构建路径中。

在此输入图像描述

Then I run the following class (right click -> run as -> java application) to create the table: 然后我运行以下类(右键单击 - >运行为 - > java应用程序)来创建表:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class MyOracleTest {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("oracle.jdbc.OracleDriver");
        String username = "sys as sysdba";
        String password = "password";
        Connection conn = null;
        Statement st = null;
        try{
            conn = DriverManager.getConnection("jdbc:oracle:thin:" + username + "/" + password + "@localhost:1521:xe");
            st = conn.createStatement();
            String command = "CREATE table x(y varchar(2))";
            st.executeUpdate(command);
        }finally{
        if(st != null){
            st.close();
        }
        if(conn != null){
            conn.close();
        }
        }
    }

I then verify the existance of the table using sqlplus: 然后我使用sqlplus验证表的存在:

在此输入图像描述

To access the objects in your workspace you have to write the SQL statements specifying the object name with the workspace name like "workspacename.objectname". 要访问工作空间中的对象,必须编写指定对象名称的SQL语句,其工作空间名称如“workspacename.objectname”。

for eg 例如

If you have created your workspace and named it "XESPACE" and created a table "STUDENT" inside the workspace, then to access the STUDENT table from JDBC follow the code below. 如果您已创建工作区并将其命名为“XESPACE”并在工作区内创建了一个表“STUDENT”,那么要从JDBC访问STUDENT表,请遵循以下代码。

Step 1: Register the JDBC Driver as Class.forName("oracle.jdbc.driver.OracleDriver") . 步骤1:将JDBC驱动程序注册为Class.forName(“oracle.jdbc.driver.OracleDriver”)

Step 2: Open connection with DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", 第2步:使用DriverManager.getConnection打开连接(“jdbc:oracle:thin:@ 127.0.0.1:1521:XE”,
"Username", "Password") . “用户名”,“密码”)
where 127.0.0.1 stands for localhost. 其中127.0.0.1代表localhost。

Step 3: Now to access the table inside your workspace write the sql query as follows 第3步:现在访问工作区内的表,按如下方式编写sql查询
" select * from XESPACE.STUDENT " 从XESPACE.STUDENT中选择*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM