Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: Missing IN or OUT parameter at index:: 1

I made some Java 1.6-Oracle11g-JDBC (using OJDBC 6) code (below). I am getting an exception - java.sql.SQLException: Missing IN or OUT parameter at index:: 1 Why is this happening and how do I fix it ?

My output is-

create CREATE TABLE employee(emp_name varchar(25),emp_address varchar(25)) insert INSERT INTO employee(jim,germany) values(?,?) Exception: java.sql.SQLException: Missing IN or OUT parameter at index:: 1 

The code is-

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement;   public class Oracle {  public static void main(String[]args) {      try     {          Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe", "newman", "123456");         Statement stmt = con.createStatement();           String create = "CREATE TABLE employee(emp_name varchar(25),emp_address varchar(25))";         System.out.println("create " + create);//         stmt.execute(create);          //insert 1st row                     String inserting = "INSERT INTO employee(hans,germany) values(?,?)";         System.out.println("insert " + inserting);//         PreparedStatement ps = con.prepareStatement(inserting);          ps.executeUpdate();          //insert 2nd row                     inserting = "INSERT INTO employee(david,austria) values(?,?)";         System.out.println("insert " + inserting);//         ps = con.prepareStatement(inserting);          ps.executeUpdate();      }catch(SQLException ex){System.out.println("Exception: " + ex);}       }  } 

EDIT - To correct the code, we use-

//insert 1st row

        String inserting = "INSERT INTO                      employee(emp_name,emp_address) values(?,?)";         PreparedStatement ps = con.prepareStatement(inserting);         System.out.println("insert " + inserting);//         ps.setString(1, "hans");         ps.setString(2, "germany");         ps.executeUpdate(); 

//insert 2nd row

        inserting = "INSERT INTO                      employee(emp_name,emp_address) values(?,?)";         ps = con.prepareStatement(inserting);         System.out.println("insert " + inserting);//         ps.setString(1, "david");         ps.setString(2, "austria");          ps.executeUpdate(); 
like image 262
sweet dreams Avatar asked Aug 08 '12 19:08

sweet dreams


People also ask

Can we throw SQLException in Java?

The following subclasses of SQLException can also be thrown: BatchUpdateException is thrown when an error occurs during a batch update operation. In addition to the information provided by SQLException , BatchUpdateException provides the update counts for all statements that were executed before the error occurred.

What is invalid column index in SQLException?

The most common cause of "java. sql. SQLException: Invalid column index" is a misconception that column index started with "0" like array or String index but that's not true instead column index starts with "1" so whenever you try to get or Set column data with column index "0" you will get "java. sql.

What does SQLException mean?

An exception that provides information on a database access error or other errors. Each SQLException provides several kinds of information: a string describing the error.


2 Answers

This is not how SQL works:

INSERT INTO employee(hans,germany) values(?,?) 

The values (hans,germany) should use column names (emp_name, emp_address). The values are provided by your program by using the Statement.setString(pos,value) methods. It is complaining because you said there were two parameters (the question marks) but didn't provide values.

You should be creating a PreparedStatement and then setting parameter values as in:

String insert= "INSERT INTO employee(emp_name,emp_address) values(?,?)"; PreparedStatement stmt = con.prepareStatement(insert); stmt.setString(1,"hans"); stmt.setString(2,"germany"); stmt.execute(); 
like image 77
Jim Garrison Avatar answered Sep 21 '22 01:09

Jim Garrison


You must use the column names and then set the values to insert (both ? marks):

//insert 1st row             String inserting = "INSERT INTO employee(emp_name ,emp_address) values(?,?)"; System.out.println("insert " + inserting);// PreparedStatement ps = con.prepareStatement(inserting);  ps.setString(1, "hans"); ps.setString(2, "germany"); ps.executeUpdate(); 
like image 29
Frank Orellana Avatar answered Sep 22 '22 01:09

Frank Orellana