现在的位置: 首页 > 综合 > 正文

oracle10g中获得可更新的(修改、增加等) ResultSet

2012年07月04日 ⁄ 综合 ⁄ 共 3815字 ⁄ 字号 评论关闭
在试过多个版本的ojdbc14.jar及class12.jar等oracle驱动后,还是不能获得可更新的ResultSet结果集。
根据api所述,下列使用代码应该没错。
-------------code------------------
new oracle.jdbc.driver.OracleDriver();
String url = "jdbc:oracle:thin:@192.168.1.98:1521:orcl";
Connection conn = DriverManager.getConnection(url, "lc", "lc");
 
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);//获得可更新的、实时的数据集
ResultSet rs = stmt.executeQuery("select * from dept");
rs.moveToInsertRow();
rs.updateInt(1, 700);
rs.updateString(2, "销售部");
rs.updateInt(3, 100);
rs.updateString(4, "XX公司");
rs.insertRow();
rs.moveToCurrentRow();
-------------code------------------

总是报错误:
后来终于在一篇很古老的帖子找到解决方法:
解决的方法是:
不要用select   *   ...作查询,
而要用select   <直接写出所有字段> 的方式.
 
另附测试数据库对获取只读、更新等数据集的代码:
package test;
* A simple sample to check the availability of scrollable result sets.
import java.sql.*;
public class ResultSet1
{
public static void main(String[] args) throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
String url = "jdbc:oracle:thin:@192.168.1.98:1521:orcl";
try {
String url1 = System.getProperty( "JDBC_URL ");
if (url1 != null)
url = url1;
} catch (Exception e) {
// If there is any security exception, ignore it
// and use the default
}
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection (url, "lc ", "lc ");
// Get the metadata regarding this connection 's database
DatabaseMetaData dbmd = conn.getMetaData();
// List all the possible result set types
int resultset_types[] =
{
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.TYPE_SCROLL_SENSITIVE
};
// List all the possible result set concurrency types
int concurrency_types[] =
{
ResultSet.CONCUR_READ_ONLY,
ResultSet.CONCUR_UPDATABLE
};
// List the result set type names
String resultset_types_msg [] =
{
"Forward only ",
"Scroll insensitive ",
"Scroll sensitive "
};
// List the concurrency type names
String concurrency_types_msg[] =
{
"Read only ",
"Updatable "
};
// Check the availability of the result type and concurrency type
for (int i=0; i <resultset_types.length; i++)
{
for (int j=0; j <concurrency_types.length; j++)
{
int type = resultset_types[i];
int concurrency = concurrency_types[j];
System.out.println ( "Type: "+resultset_types_msg[i]+ " "+
"Concurrency: "+concurrency_types_msg[j]);
System.out.println
( "---------------------------------------------------- ");
// Return true if the result set type is supported
System.out.println ( "supportResultSetType: "+
dbmd.supportsResultSetType(type));
// Return true if the result set type and concurrency type is supported
System.out.println ( "supportsResultSetConcurrency: "+
dbmd.supportsResultSetConcurrency(type, concurrency));
// Return true if the result set 's updates are visible
System.out.println ( "ownUpdatesAreVisible: "+
dbmd.ownUpdatesAreVisible(type));
// Return true if the result set 's deletions are visible
System.out.println ( "ownDeletesAreVisible: "+
dbmd.ownDeletesAreVisible(type));
// Return true if the result set 's insertions are visible
System.out.println ( "ownInsertAreVisible: "+
dbmd.ownInsertsAreVisible(type));
// Return true if other 's changes are visible
System.out.println ( "othersUpdatesAreVisible: "+
dbmd.othersUpdatesAreVisible(type));
// Return true if other 's deletions are visible
System.out.println ( "othersDeletesAreVisible: "+
dbmd.othersDeletesAreVisible(type));
// Return true if other 's insertions are visible
System.out.println ( "othersInsertsAreVisible: "+
dbmd.othersInsertsAreVisible(type));
// Return true if ResultSet.rowUpdated() is supported
System.out.println ( "updatesAreDetected: "+
dbmd.updatesAreDetected(type));
// Return true if ResultSet.rowDeleted() is supported
System.out.println ( "deletesAreDetected: "+
dbmd.deletesAreDetected(type));
// Return true if ResultSet.rowInserted() is supported
System.out.println ( "insertsAreDetected: "+
dbmd.insertsAreDetected(type));
System.out.println ();
}
}
// Close the connection
conn.close();
}

}

JDBC中Class12.jar和ojdbc14.jar的区别

1.oracle10g中将中文字符解析为3个字节,oracle9i中保留为2个字节,在插入时常常会因为这个问题而造成数据插入失败、超常。
2.在处理大字符集的时候,同样存在字符限制,class12只能支持到3000左右吧,ojdbc14可以支持到更长。

抱歉!评论已关闭.