【发布时间】:2017-05-30 05:40:58
【问题描述】:
我无法将ResultSet 存储在对象数组中。我想将它存储在一个Object数组中并返回它,我该怎么做?
import java.sql.*;
public class TestSQLwithFunction {
private static Object[] returnObjectArray() {
String connnectionURL = "jdbc:sqlserver://localhost\\FAISAL-
FAS:1433;username=sa;password=password";
Object ob[] = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(connnectionURL);
ResultSet rs = conn.getMetaData().getSchemas();
while(rs.next()) {
ob = (Object[]) rs.getObject(1);
}
}
catch(Exception e){
e.printStackTrace();
}
return ob;
}
public static void main (String[] args) {
Object[] checkrs = returnObjectArray();
for(int i = 0; i <= checkrs.length; i++){
System.out.println(checkrs);
}
}
}
我得到了这个例外:
java.lang.ClassCastException: java.lang.String 无法转换为 [Ljava.lang.Object;在 TestSQLwithFunction.returnObjectArray(TestSQLwithFunction.java:13) 在 TestSQLwithFunction.main(TestSQLwithFunction.java:24) 中的异常 线程“主”java.lang.NullPointerException 在 TestSQLwithFunction.main(TestSQLwithFunction.java:25)
【问题讨论】:
-
@Ezio 是对的……用他的方法……会奏效的