![](/img/trans.png)
[英]Why do I get an ArrayIndexOutOfBoundsException with this?
[英]Why do I get an ArrayIndexOutOfBoundsException in my JDBC connection?
我是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");
}
}
這似乎與Netbeans有關。 在他們的論壇上另請參閱此主題 。 它似乎可以在Eclipse(可能還有所有其他環境)中工作。
這顯然是UniVerse JDBC驅動程序中的錯誤。 顯然,它是在靜態初始化程序內部依賴於某些特定的環境條件,這在Netbeans中是不同的。 如果不是錯誤,那就拋出了一個不言自明的異常,而不是一個愚蠢的運行時異常。
我會將此錯誤報告給IBM / UniVerse。
如果提供的代碼實際上是導致異常的代碼,那么我猜它會失敗,因為在您嘗試獲得連接時url
為null
。
但是錯誤stacktrace在第36行(注釋行)顯示了錯誤。 因此,如果我的猜測是錯誤的,請編輯您的問題,並提供匹配的代碼和錯誤消息,並標記引發異常的代碼行。
你並不孤單: 同樣的問題
我也遇到了這個問題。 UniTokens假定字節到char的一對一轉換,但並非所有平台都適用。 將參數傳遞給JVM可以避免此問題。 嘗試-Dfile.encoding =“ windows-1252”或-Dfile.encoding =“ US-ASCII”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.