代码如下:
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:lwborclsid";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from dept");
while(rs.next()) {
System.out.print("DeptNo:" + rs.getInt(1));
System.out.print("/tDeptName:" + rs.getString(2));
System.out.println("/tLOC:" + rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驱动程序类");
}catch(SQLException e){
e.printStackTrace();
}
}
}问题提示如下:
java.lang.ArrayIndexOutOfBoundsException: 0
at oracle.jdbc.driver.OracleSql.main(OracleSql.java:1661)
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:lwborclsid";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from dept");
while(rs.next()) {
System.out.print("DeptNo:" + rs.getInt(1));
System.out.print("/tDeptName:" + rs.getString(2));
System.out.println("/tLOC:" + rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
System.out.println("找不到指定的驱动程序类");
}catch(SQLException e){
e.printStackTrace();
}
}
}问题提示如下:
java.lang.ArrayIndexOutOfBoundsException: 0
at oracle.jdbc.driver.OracleSql.main(OracleSql.java:1661)
是不是你的结果集 只有2列数据?
System.out.print("DeptNo:" + rs.getInt(1));
System.out.print("/tDeptName:" + rs.getString(2));
System.out.println("/tLOC:" + rs.getString(3));应该是:
rs.getInt(0),rs.getString(1),rs.getString(2);
楼主应该要学会自己去分析问题,这种问题应该很好找的.