简体   繁体   English

java.lang.ArrayIndexOutOfBoundsException:0

[英]java.lang.ArrayIndexOutOfBoundsException: 0

this is a simple program to check the database , sql queries are 这是一个检查数据库的简单程序,sql查询是

    create database studentapp_db ;
    create table  students_info
    ( regno int(10) not null,
    firstname varchar (50) ,
    middlename varchar (50),
    lastname varchar (50),
    primary key(regno) ) ;
    insert into students_info

the error which i am getting while executing my java program are 我在执行Java程序时遇到的错误是

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0
 at  com.jspider.jdbc.common.callableStatementexample1.
 main(callableStatementexamp le1.java:32)

and here is my java program 这是我的java程序

package com.jspider.jdbc.common;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Driver;

public class callableStatementexample1 {

    public static void main(String[] args) {
        Connection con = null;
        CallableStatement cstmt = null;
        ResultSet rs = null;

        try {
            //1. load the driver

            Driver driverRef = new Driver();
            DriverManager.registerDriver(driverRef);

            //2. Get the Db conection 
            String dbUrl = "jdbc:mysql://localhost:3306  / Studentapp_db ? user = j2ee & password = j2ee ";
            con = DriverManager.getConnection(dbUrl);
            //3. Issue sql queries via connection

            //String query = "call getAllStudentsInfo()";
            String query = "call getAllStudentsInfo(4)";
            cstmt = con.prepareCall(query);
            cstmt.setInt(1, Integer.parseInt(args[0]));

            boolean isDBResults = cstmt.execute();

            //4 Process the results
            if (isDBResults) {
                System.out.println("Result is DB results");
                rs = cstmt.getResultSet();


                while (rs.next()) {
                    int regno = rs.getInt("regno");
                    String fNM = rs.getString("firstname");
                    String mNM = rs.getString("middlename");
                    String lNM = rs.getString("lastname");
                    System.out.println("Reg.No :" + regno);
                    System.out.println("First Name :" + fNM);
                    System.out.println("middle Name :" + mNM);
                    System.out.println("last Name :" + lNM);
                    System.out.println("-------------------");

                } //end of while 
            } else {
                System.out.println("Result is INT count");
                int Count = cstmt.getUpdateCount();
                System.out.println("Row affected Count" + Count);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 5. close all jdbc object
        } // end of outer try catch 
    }
}

the above is my java program , please help i am new to java ; 以上是我的java程序,请帮助我是java的新手;

EDIT: The error occurs at the line of cstmt.setInt(1, Integer.parseInt(args[0])); 编辑:错误发生在cstmt.setInt(1, Integer.parseInt(args[0])); , which is the 32th line. ,这是第32行。

change this(There is no argument in sql statement) call getAllStudentsInfo(4) to call getAllStudentsInfo(?) or Remove cstmt.setInt(1,Integer.parseInt(args[0])) if you preferred hard coded value. 更改此(sql语句中没有参数)调用getAllStudentsInfo(4) call getAllStudentsInfo(?)或删除cstmt.setInt(1,Integer.parseInt(args[0]))如果您喜欢硬编码值)。

String query = "call getAllStudentsInfo(?)";
cstmt = con.prepareCall(query);
cstmt.setInt(1,Integer.parseInt(args[0]));

Looks like you have started your program without parameter. 看起来您已经启动了没有参数的程序。 So args has no objects. 因此, args没有对象。

You should check that before calling Integer.parseInt(args[0]) ; 您应该在调用Integer.parseInt(args[0])之前检查一下;

Also your statement has no placeholder for your argument. 同样,您的陈述没有占位符。

You are getting an error, because of this line: 由于此行,您得到一个错误:

cstmt.setInt(1, Integer.parseInt(args[0]));

args is the array representing the command-line arguments passed when you ran the program. args是表示运行程序时传递的命令行参数的数组。 Since you did not pass any command-line arguments, there is no 0th element of args . 由于您没有传递任何命令行参数,因此args没有第0个元素。

Solution: pass an integer value as command-line argument when you run the program. 解决方案:运行程序时,将整数值作为命令行参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM