为什么我的 JDBC 连接中出现 ArrayIndexOutOfBoundsException?

发布于 2024-09-16 16:52:52 字数 3726 浏览 5 评论 0原文

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

> 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");
}

}

I am new to Java and I am attempting to use JDBC to connect to an UniVerse database. I'm using Sun Java 6 JDK to using NetBeans to build the project. My simple test below builds however it gives the errors below:

> 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)

My Test Code:

/*
* 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");
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-09-23 16:52:52

这似乎与 Netbeans 相关。另请参阅论坛中的此主题。它似乎可以在 Eclipse(也可能是所有其他环境)中工作。

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

我会向 IBM/UniVerse 报告此错误。

This appears to be Netbeans related. See also this topic at their forum. It appears to work in Eclipse (and possibly all other environments).

This is clearly a bug in the UniVerse JDBC driver. It is apparently inside the static initializer relying on some specific environmental condition, which is different in Netbeans. If it wasn't a bug, it would have thrown a much more self-explaining exception, not such a silly runtime exception.

I'd report this bug to IBM/UniVerse.

酷到爆炸 2024-09-23 16:52:52

如果所提供的代码是实际导致异常的代码,那么我猜它会失败,因为在您尝试获取连接时 urlnull

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


您并不孤单:同样的问题

If the presented code is the one that actually caused the exception, then I guess, it fails because url is null at the time you try to get the connection.

But the error stacktrace shows an error at line 36 which is comment line. So if my guess is wrong, please edit your question and present matching code and error message and mark the line of code that throws the exception.


You're not alone: same problem

深海夜未眠 2024-09-23 16:52:52

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

I experienced this problem too. UniTokens is assuming that there is a one-for-one conversion of bytes to chars, but it is not true for all platforms. Passing an argument to the JVM avoids this issue. Try -Dfile.encoding="windows-1252" or -Dfile.encoding="US-ASCII"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文