sql - java.lang.ArrayIndexOutOfBoundsException: 0 -
this simple program check database , sql queries are
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 students_info
the error getting while executing java program are
exception in thread "main" java.lang.arrayindexoutofboundsexception:0 @ com.jspider.jdbc.common.callablestatementexample1. main(callablestatementexamp le1.java:32)
and here java program
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 driver driver driverref = new driver(); drivermanager.registerdriver(driverref); //2. 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 results if (isdbresults) { system.out.println("result 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 int count"); int count = cstmt.getupdatecount(); system.out.println("row affected count" + count); } } catch (sqlexception e) { e.printstacktrace(); } { // 5. close jdbc object } // end of outer try catch } }
the above java program , please new java ;
edit: error occurs @ line of cstmt.setint(1, integer.parseint(args[0]));
, 32th line.
change this(there no argument in sql statement) call getallstudentsinfo(4)
to call getallstudentsinfo(?)
or remove cstmt.setint(1,integer.parseint(args[0]))
if preferred hard coded value.
string query = "call getallstudentsinfo(?)"; cstmt = con.preparecall(query); cstmt.setint(1,integer.parseint(args[0]));
Comments
Post a Comment