Topic: 如何更新结果集resultset到数据库?

  Print this page

1.如何更新结果集resultset到数据库? Copy to clipboard
Posted by: shsen
Posted on: 2003-04-15 16:00

我从access数据库文件中查询得到一个结果集resultset1,
用resultset1.next();使指针到第一个,可以保证有记录。
然后用resultset1.updateString("name","111");语句更新其中的莫个字段,
最后用resultset1.updateRow();把该行写到数据库文件。编译能通过,
可是运行时会出错,信息如附件的图片。
我是用如下代码获得数据库连接的:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student");
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery("SELECT * FROM student");

2.Re: [Re: shsen] Copy to clipboard
Posted by: automan
Posted on: 2003-04-15 16:31

把程序贴齐咯。

3.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-15 17:48

import java.sql.*;

public class testjdbc
{
  public static void main(String args[]) throws SQLException
  {
    try
    {
      String id,name,sex,age,chinese,maths,output;
      //这种连接方式是单线程方式,最后尽量避免
      /**
      * 另一种连接方式:
      *
      * Context ctx = new InitialContext();
      * javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("examples-dataSource-demoPool");
      * java.sql.Connection myConn = ds.getConnection();
      *
      */
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection con=DriverManager.getConnection("jdbc:odbc:student");
      //con.setAutoCommit(false);
      //Connection con = DriverManager.getConnection("jdbc:odbc:wombat", "login", "password");
      Statement sta = con.createStatement();
      sta.executeUpdate("INSERT INTO student(name,sex,age,chinese,maths) VALUES('ss','男',10,100,95)");
      sta.executeUpdate("UPDATE student SET age = 30 WHERE name = '张三'");
      sta.executeUpdate("DELETE FROM student WHERE name = 'aaaa");
      //ResultSet rs=sta.executeQuery("SELECT Name,Sex,Age,Chinese,Maths FROM student");
      ResultSet rs = sta.executeQuery("SELECT * FROM student");
      //System.out.println("Id Name Sex Age Chinese Maths");
      System.out.println("|---------------------------|");
      System.out.println(" Id 姓名 性别 年龄 语文 数学");
      System.out.println("|---------------------------|");
      //rs.next();
      //rs.updateString("name","111");
      rs.absolute(5);
      do
      {
        id=rs.getString(1);
        name=rs.getString(2);
        sex=rs.getString(3);
        age=rs.getString(4);
        chinese=rs.getString(5);
        maths=rs.getStringDevil;
        output="| " + id + " " + name + " " + sex + " " + age + " " + chinese + " " + maths + " |";
        System.out.println(output);
        System.out.println("|---------------------------|");
      }while(rs.next());
      //PreparedStatement updateAge=con.prepareStatement("UPDATE student SET age=28 WHERE name = '张三'");
      //updateAge.executeUpdate();
      //con.commit();
      //con.setAutoCommit(true);
      //sta2.executeUpdate("INSERT INTO student(name,sex,age,chinese,maths) VALUES('aaa,'男',10,100,95)");
      //sta2.executeUpdate("UPDATE student SET age = 100 WHERE name = '张三'");
    }
    catch(java.lang.Exception ex)
    {
      ex.printStackTrace();
    }
  }
}

/*
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next())
{
  int x = rs.getInt("a");
  String s = rs.getString("b");
  float f = rs.getFloat("c");
}
*/

4.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-15 18:42

你的程序修改如下,在JB8中正常运行。

import java.sql.*;

public class testjdbc
{
public static void main(String args[]) throws SQLException
{

try
{
String id,name,sex,age,chinese,maths,output;
//这种连接方式是单线程方式,最后尽量避免
/**
* 另一种连接方式:
*
* Context ctx = new InitialContext();
* javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("examples-dataSource-demoPool");
* java.sql.Connection myConn = ds.getConnection();
*
*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student");
//con.setAutoCommit(false);
//Connection con = DriverManager.getConnection("jdbc:odbc:wombat", "login", "password");
Statement sta = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY );
//Statement sta = con.createStatement( );
sta.executeUpdate("INSERT INTO student(name,sex,age,chinese,maths) VALUES('ss','男',10,100,95)");
sta.executeUpdate("UPDATE student SET age = 30 WHERE name = '张三'");
sta.executeUpdate("DELETE FROM student WHERE name = 'aaaa'");
//ResultSet rs=sta.executeQuery("SELECT Name,Sex,Age,Chinese,Maths FROM student");
ResultSet rs = sta.executeQuery("SELECT * FROM student");
//System.out.println("Id Name Sex Age Chinese Maths");
System.out.println("|---------------------------|");
System.out.println(" Id 姓名 性别 年龄 语文 数学");
System.out.println("|---------------------------|");
//rs.next();
//rs.updateString("name","111");

rs.absolute(5);

do
{
id=rs.getString(1);
name=rs.getString(2);
sex=rs.getString(3);
age=rs.getString(4);
chinese=rs.getString(5);
maths=rs.getStringDevil;
output="| " + id + " " + name + " " + sex + " " + age + " " + chinese + " " + maths + " |";
System.out.println(output);
System.out.println("|---------------------------|");
}while(rs.next());
//PreparedStatement updateAge=con.prepareStatement("UPDATE student SET age=28 WHERE name = '张三'");
//updateAge.executeUpdate();
//con.commit();
//con.setAutoCommit(true);
//sta2.executeUpdate("INSERT INTO student(name,sex,age,chinese,maths) VALUES('aaa,'男',10,100,95)");
//sta2.executeUpdate("UPDATE student SET age = 100 WHERE name = '张三'");
}
catch(java.lang.Exception ex)
{
(System.out.print(ex));
}

}
}

5.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-15 19:15

错误原因是:ResultType的Type是ResultSet.TYPE_FORWARD_ONLY导致。

6.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-16 10:04

十分感谢,我改成Statement sta = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);可以更新了,也能提交到数据库,可是我没更新的字段变成空的,不知道是什么原因,是不是执行了rs.getString()后那个字段的值就没有了?

7.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-16 11:43

没有呀。在我JB8中更新(UPDATE student SET age = 30 WHERE name = '张三')与删除(DELETE FROM student WHERE name = 'aaaa')都正常,没有你说的:没更新的字段变成空的 这种情况。一切很好呀。

8.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-16 13:00

不是那个更新,是这个:rs.updateString("name","111");
要不是试看看执行两次:rs.getString(1);再rs.next();会出错,不知何原因?

9.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-16 16:04

是的,
rs.updateString("name","111");
rs.getString(3);
rs.updateRow() ;后,当前行的第三个字段值变为空了。

10.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-17 10:05

对阿,为什么会这样,难道是只要get过一次,记录集里的相应字段就变成空了?我测试过对同一条记录get两次,结果第二次的就出错。

11.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-18 00:21

这个问题好象只发生在rs.getString()上。以下两种测试代码:
rs.first() ;
rs.updateString("sex","AAA");
System.out .println( rs.getString(2));
System.out .println( rs.getString(2));//此时输出值正常,两者相同
rs.updateRow() ;//该语句一执行,以下两种输出全为空。
System.out .println( rs.getString(2));
System.out .println( rs.getString(2));

rs.next();
rs.updateString("sex","AAA");
System.out .println( rs.getIntDevil);
System.out .println( rs.getIntDevil);
rs.updateRow() ;//该语句执行前与后输出结果全相同
System.out .println( rs.getIntDevil);
System.out .println( rs.getIntDevil);
原因待查。

12.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-18 09:12

谢谢你的帮助,一起努力:)

13.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: jeez
Posted on: 2003-04-18 09:40

我想可能和Transaction有关,以下摘自JDBC Guide,请看:

2.1.8 Transaction Isolation Levels
If a DBMS supports transaction processing, it will have some way of managing potential conflicts that can arise when two transactions are operating on a database at the same time. A user can specify a transaction isolation level to indicate what level of care the DBMS should exercise in resolving potential conflicts. For example, what happens when one transaction changes a value and a second transaction reads that value before the change has been committed or rolled back? Should that be allowed, given that the changed value read by the second transaction will be invalid if the first transaction is rolled back? A JDBC user can instruct the DBMS to allow a value to be read before it has been committed (a "dirty read") with the following code, where con is the current connection:

con.setTransactionIsolation(TRANSACTION_READ_UNCOMMITTED);

The higher the transaction isolation level, the more care is taken to avoid conflicts. The Connection interface defines five levels, with the lowest specifying that transactions are not supported at all and the highest specifying that while one transaction is operating on a database, no other transactions may make any changes to the data read by that transaction. TRANSACTION_READ_UNCOMMITTED, used in the previous example, is one level up from the lowest level. Typically, the higher the level of isolation, the slower the application executes (due to increased locking overhead and decreased concurrency between users). The developer must balance the need for performance with the need for data consistency when making a decision about what isolation level to use. Of course, the level that can actually be supported depends on the capabilities of the underlying DBMS.

When a new Connection object is created, its transaction isolation level depends on the driver, but normally it is the default for the underlying data source. A user may call the method setIsolationLevel to change the transaction isolation level, and the new level will be in effect for the rest of the connection session. To change the transaction isolation level for just one transaction, one needs to set it before executing any statements in the transaction and then reset it after the transaction terminates. Changing the transaction isolation level during a transaction is not recommended, for it will trigger an immediate call to the method commit, causing any changes up to that point to be made permanent.

14.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: jiangns3000
Posted on: 2003-04-19 13:32

经过验证及比较,我几乎肯定这个更新怪现象是由JDBC-ODBC桥中调用Access(.mdb)的ODBC driver引起的。其中一个最明显的例证是:一切代码未变,在SQL SERVER 2000中(WIN2K 专业版,SQL SERVER 2000 个人版 , MS的SQL Server 2000 Driver for JDBC )中无任何异常。结果与我们预料的正确结果一致。

15.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: fengyifan
Posted on: 2003-04-19 18:01

JDBC-ODBC桥实际项目不能用,例如get一次后不能再get等问题.

16.Re:如何更新结果集resultset到数据库? [Re: shsen] Copy to clipboard
Posted by: shsen
Posted on: 2003-04-21 11:15

真的吗?那我不是得重写啊。。。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923