2010-08-27 59 views
1

我是Java新手,我尝试使用JDBC连接到UniVerse数据库。我正在使用Sun Java 6 JDK来使用NetBeans来构建项目。下面我简单的测试构建但是它给了以下错误:为什么我在我的JDBC连接中得到一个ArrayIndexOutOfBoundsException?

> run: 
driver loaded 
Exception in thread "main" java.lang.ExceptionInInitializerError 
Connecting... 
     at com.ibm.u2.jdbc.UniJDBCProtocolU2Impl.initDefaultMarks(UniJDBCProtocolU2Impl.java:1239) 
     at com.ibm.u2.jdbc.UniJDBCProtocolU2Impl.<init>(UniJDBCProtocolU2Impl.java:116) 
     at com.ibm.u2.jdbc.UniJDBCConnectionImpl.<init>(UniJDBCConnectionImpl.java:137) 
     at com.ibm.u2.jdbc.UniJDBCDriver.connect(UniJDBCDriver.java:111) 
     at java.sql.DriverManager.getConnection(DriverManager.java:582) 
     at java.sql.DriverManager.getConnection(DriverManager.java:207) 
     at testjdbc.Main.main(Main.java:36) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: 3 
     at asjava.uniclientlibs.UniTokens.<clinit>(UniTokens.java:109) 
     ... 7 more 
Java Result: 1 
BUILD SUCCESSFUL (total time: 0 seconds) 

我的测试代码:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package testjdbc; 
import java.sql.*; 
import java.io.*; 

/** 
* 
* @author norm 
*/ 
public class Main { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 

    Connection con = null ; 

    try{ 
     // generate URL 

    String url = "jdbc:ibm-u2://my.uv.db:31438/DB-ACCOUNT;user=me;password=pass"; //testing, I remove user and password from here and use them below. Still FAILS!!! ARGGGG!!! 
    String user = "me"; 
    String password = "pass"; 

    String driver = "com.ibm.u2.jdbc.UniJDBCDriver"; 
    //Load driver and connect to server 
    Class.forName(driver); 
    System.out.println("driver loaded"); 
    System.out.println("Connecting..."); 
    con = DriverManager.getConnection(url); //This is line 36 
    // con = DriverManager.getConnection(url, user, password); // gives the same error 
    //con = DriverManager.getConnection("jdbc:ibm-u2://my.uv.db:31438/D:/PathTo/DB;"); //and yet the same error for this as well 
    System.out.println("Connection String sent"); 
    System.out.println("Querying..."); 
    testQuery(con) ; 

    } 
    catch (SQLException e) { 
     System.out.println("Ex-Message :" + e.getMessage()); 
     System.out.println("Ex-Code :" + e.getErrorCode()) ; 
     System.out.println("Ex-SQLState:" + e.getSQLState()); 
     System.out.println("Ex-Next :" + e.getNextException()); 
     e.printStackTrace() ; 
     System.gc(); 
    } catch (Exception e) { 
     System.out.println("Exception caught:"+e) ; 
     e.printStackTrace() ; 
    } 
} 

public static void testQuery(Connection con) 
    throws SQLException 
{ 
    Statement stmt = con.createStatement(); 
    String sql = "select FIRST.NAME from EMPCEL"; 
      //"select @ID, CITY, STATE, ZIP, PHONE from CUSTOMER"; 

    // Execute the SELECT statement 
    ResultSet rs = stmt.executeQuery(sql); 

    // Get result of first five records 
    System.out.println("\tlist selected columns for the first five records:"); 
    int i = 1; 
    while (rs.next() && i < 6) 
    { 
     System.out.println("\nRecord "+ i +" :"); 
     System.out.println("\tFirst Name : \t" + rs.getString(1)); 
//   System.out.println("\tCITY :\t" + rs.getString(2)); 
//   System.out.println("\tSTATE :\t" + rs.getString(3)); 
//   System.out.println("\tZIP : \t" + rs.getString(4)); 
//   System.out.println("\tPHONE :\t" + rs.getString(5)); 
     i++; 
     System.out.println("Finished."); 
    } 

    rs.close(); 
    stmt.close() ; 
    System.out.println("\n\t*--- QUERY test is done successful ---*\n"); 
} 

} 
+0

你可以看到,我尝试了DriverManager.getConnection几种不同的方式,包括一些没有体现在这个迭代。 – Norm 2010-08-27 10:14:50

回答

2

这似乎是Netbeans的关系。在他们的论坛中也可以看到this topic。它似乎在Eclipse(可能还有其他所有环境)中都有效。

这显然是UniVerse JDBC驱动程序中的一个错误。它显然是在静态初始化器内依赖于一些特定的环境条件,这在Netbeans中是不同的。如果它不是一个bug,它会抛出一个更自我解释的异常,而不是这样一个愚蠢的运行时异常。

我将这个错误报告给IBM/UniVerse。

+1

圣洁CRAP !!!就是这样。想想我昨天整天在桌子上撞着我的头。我很想和Rocket通电话。谢谢BalusC !!并感谢您的Stackoverflow。 我想这解决了我将使用哪个IDE的争论。 – Norm 2010-08-27 12:39:22

+0

不客气。 – BalusC 2010-08-27 12:41:23

1

如果目前的代码是实际产生异常的一个,那么我想,它失败,因为url在尝试获取连接时是null

但是,错误堆栈跟踪在注释行第36行显示错误。所以,如果我的猜测是错误的,请编辑您的问题,并提供匹配的代码和错误消息,并标记引发异常的代码行。


你并不孤单:same problem

+0

我编辑了代码,以进一步解释我在各种线路上尝试过的内容。谢谢回复。我也使用了IP地址以及域名。仍然无济于事。 – Norm 2010-08-27 12:22:15

0

我也遇到过这个问题。 UniTokens假设有一个字节到字符的一对一转换,但并非所有平台都是如此。将参数传递给JVM可避免此问题。尝试-Dfile.encoding =“windows-1252”或-Dfile.encoding =“US-ASCII”

相关问题