2

I have established the database connection successfully but not when I run this code through Java , my code keeps running and nothing happens. Nothing means neither it is inserting into database nor giving me any exceptions.

I am using spatial data types from Oracle 11g (MDSYS.SDO_POINT_TYPE). I ran this query in Oracle 11g database directly and it works fine , but somehow does not insert the record when I use it through java code.

I also tried with a simple student table and was able to insert statement it using java.

Here is my snippet:

String insert = "insert into table1 values('p0', MDSYS.SDO_POINT_TYPE(228,102, null))";
    try
    {
        Statement statement = connection.createStatement();
        statement.executeUpdate(insert);
        System.out.println("Inserted record in the table");
    }catch(SQLException e)
    {
        System.out.println("Error due to SQL Exception");
        e.printStackTrace();
    }
    try
    {

        connection.close();
        System.out.println("Closing the connection");
    }catch(SQLException e)
    {
        System.out.println("Error in closing connection");
        e.printStackTrace();
    }

Connection JDBC is :

try {
        Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {
        System.out.println("Where is your Oracle JDBC Driver?");
        e.printStackTrace();
    }
    System.out.println("Oracle JDBC Driver Registered!");

    Connection connection = null;

    try {

        connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:db11g", username,
                pass);


    } catch (SQLException e) {
        System.out.println("Connection Failed!");
        e.printStackTrace();
    }
    if (connection != null) {
        System.out.println("Database connected");
    } else {
        System.out.println("Failed to connect the database");
    }
    return connection;
4

2 回答 2

1

除非您的连接处于自动提交模式*您应该commit()在调用之前调用它close()

强烈建议应用程序在调用 close 方法之前显式提交或回滚活动事务。如果调用 close 方法并且存在活动事务,则结果是实现定义的。

看起来Oracle的行为是回滚事务。以下是解决此问题的方法:

try
{
    Statement statement = connection.createStatement();
    statement.executeUpdate(insert);
    connection.commit(); // <====== Here
    System.out.println("Inserted record in the table");
}catch(SQLException e)
{
    System.out.println("Error due to SQL Exception");
    e.printStackTrace();
}

INSERT此外,在语句中列出要显式插入的列是一个非常好的主意:

String insert = "insert into table1 (col1, col2) values('p0', MDSYS.SDO_POINT_TYPE(228,102, null))";

替换col1col2使用实际列的名称。还要考虑参数化您的插入,并使用PreparedStatement.


*这很少被推荐。

于 2014-11-07T19:50:23.337 回答
1

我看到您已修复您的代码以正确提交并且它现在可以工作。

但我想指出,你所做的毫无意义。SDO_POINT_TYPE 并不打算以您的方式使用:它仅打算在 SDO_GEOMETRY 类型内部使用:这是您必须使用的真正空间类型:索引它,查询它,在各种过程中使用它(如生成一个缓冲区)等等。

因此,如果您打算使用 Oracle Spatial:

1) 在您的表中,使用 SDO_GEOMETRY 类型。例如:

create table us_cities (
   state char(2),
   name char(50),
   location sdo_geometry,
   primary key (state, name)
);

2) 要手动插入行,请执行以下操作:

insert into us_cities (state, name, location) 
values (
  'CA',
  'Los Angeles',
  sdo_geometry (2001, 4326, sdo_point_type (-118.411201,34.112101,null),null,null)
);

在现实生活中,您显然会为坐标使用绑定变量。或者,由于您使用 java,因此可以使用 Oracle Spatial 的 java API,它可以让您在 java 中操作几何图形。

3) 为该表设置空间元数据

insert into user_sdo_geom_metadata (table_name, column_name, diminfo, srid) 
values (
  'US_CITIES', 
  'LOCATION', 
  sdo_dim_array (
    sdo_dim_element('Long', -180, 180, 1), 
    sdo_dim_element('Lat', -90, 90, 1)  
  ), 
  4326 
); 
commit;

4) 创建空间索引

create index us_cities_sx on us_cities
  indextype is mdsys.spatial_index;

5) 现在执行一些查询(假设表中已经填充了一些美国城市的位置)

select name, state 
from us_cities
where sdo_within_distance (
  location,
  sdo_geometry (2001, 4326, sdo_point_type (-73.93,40.7,null),null,null),
  'distance=200 unit=km')
= 'TRUE';

这可能会返回如下内容:

NAME                 STATE
-------------------- -----
Philadelphia         PA
Allentown            PA
Elizabeth            NJ
Newark               NJ
Jersey City          NJ
Paterson             NJ
Yonkers              NY
Stamford             CT
Bridgeport           CT
New Haven            CT
Waterbury            CT
Hartford             CT
Springfield          MA

13 rows selected.

此时,您应该真正阅读 Oracle 文档中有关 Oracle Spatial 的更多信息。

于 2014-11-08T17:06:16.800 回答