Topic: 本机器修改查询数据库的结果,做与数据库的同步时候出错!

  Print this page

1.本机器修改查询数据库的结果,做与数据库的同步时候出错! Copy to clipboard
Posted by: liwei2018
Posted on: 2005-07-02 14:03

/*
* 创建日期 2005-7-2
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.laixi.mysql;

import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @author laixi
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class MySqlTest {
  
  public static void main(String agrs[])throws SQLException{
    
    ResultSet rs = null;
    
    PreparedStatement ps = null;

    String url = "jdbc:mysql://localhost:3306/test";
    
    Connection con;
    
    Statement stmt;
    try {

      Class.forName("com.mysql.jdbc.Driver");

    } catch(java.lang.ClassNotFoundException e) {
      System.err.print("ClassNotFoundException: ");
      System.err.println(e.getMessage());
    }
    
    try {

      con = DriverManager.getConnection(url, "root", "");
      
      con.setAutoCommit(false);

      stmt = con.createStatement();
      
      rs = stmt.executeQuery("SELECT * FROM COFFEES");
      
      while (rs.next()) {
        String name = rs.getString("COF_NAME");
        int id = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        //System.out.print(name + " " + id + " " + price);
        //System.out.println(" " + sales + " " + total);
      }
      
      rs.updateObject("SUP_ID", "name");
      
      rs.updateRow();

  } catch(BatchUpdateException b) {
    System.err.println("-----BatchUpdateException-----");
    System.err.println("SQLState: " + b.getSQLState());
    System.err.println("Message: " + b.getMessage());
    System.err.println("Vendor: " + b.getErrorCode());
    System.err.print("Update counts: ");
    int [] updateCounts = b.getUpdateCounts();
    for (int i = 0; i < updateCounts.length; i++) {
      System.err.print(updateCounts[i] + " ");
    }
    System.err.println("");

  } catch(SQLException ex) {
    System.err.println("-----SQLException-----");
    System.err.println("SQLState: " + ex.getSQLState());
    System.err.println("Message: " + ex.getMessage());
    System.err.println("Vendor: " + ex.getErrorCode());
  }

  }
}

2.Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! [Re: liwei2018] Copy to clipboard
Posted by: liwei2018
Posted on: 2005-07-02 14:04

-----SQLException-----
SQLState: S1000
Message: Result Set not updatable.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.
Vendor: 0
(上述为错误信息)

3.Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! [Re: liwei2018] Copy to clipboard
Posted by: ljy0000
Posted on: 2005-07-02 14:40

stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

4.Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! [Re: ljy0000] Copy to clipboard
Posted by: liwei2018
Posted on: 2005-07-02 22:38

谢谢楼上!
还是有错
-----SQLException-----
SQLState: 01004
Message: Data truncation: Data truncated for column 'SUP_ID' at row 1
Vendor: 0

5.Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! [Re: liwei2018] Copy to clipboard
Posted by: liwei2018
Posted on: 2005-08-01 18:24

已经搞定了


   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