java通过JDBC访问Oracle的2个异常

类别:Java 点击:0 评论:0 推荐:

1.  连接非常慢, 连接成功后执行select操作出现异常:

   Exception in thread "main" java.sql.SQLException: ORA-00600: 内部错误代码,参数: [ttcgcshnd-1], [0], [], [], [], [], [], []
 

   解决: 使用oracle安装目录下的jdbc\lib\classes12.jar后正常.

2. 使用PreparedStatement的setString(i, s)时出现:

可以参考帖子:http://community.csdn.net/Expert/topic/3936/3936672.xml?temp=.2879145

 java.sql.SQLException: 数据大小超出此类型的最大值: 3000

后面那个值大小不定,  感觉与s大小有关

表结构

create table test(

name char(32),

addr   varchar(3000)                       //varchar2也一样

)

解决办法: 采用setCharacterStream

import java.sql.*;
import java.io.*;
import java.util.*;

/**
 * oracle测试
 * @author kingfish
 * @version 1.0
 */
public class TestOra {
  public static void testORACLE() {
    String url = "jdbc:oracle:thin:@localhost:1521:oradb";
    String username = "system";
    String password = "manager";

    Connection conn = null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      conn = DriverManager.getConnection(url, username, password);
    }
    catch (Exception e) {
      e.printStackTrace();
      return;
    }

    char[] carray = new char[1000];
    Arrays.fill(carray, '我');
    String s = new String(carray);
    try {
      PreparedStatement pst = conn.prepareStatement(
          "insert into test(name,addr) values(?,?)");
      pst.setString(1, "kingfish");

      pst.setCharacterStream(2,
                             new InputStreamReader(new ByteArrayInputStream(s.
          getBytes())), s.length());

      //pst.setString(2,s);  //用此句则异常
      pst.execute();

      Statement st = conn.createStatement();
      ResultSet r = st.executeQuery("SELECT * from test");

      while (r.next()) {
        s = r.getString(2);
        System.out.println("len=" + s.length());
        System.out.println("value=" + s);
      }

      r.close();
      st.close();
      conn.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 测试
   * @param args String[]
   */
  public static void main(String[] args) {
    testORACLE();
  }
}

说明: 引起此问题的原因估计和OraclePreparedStatement中的setString的实现有关.

kingfish

2005.4.14

本文地址:http://com.8s8s.com/it/it10184.htm