Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:25:25+00:00 2026-05-26T02:25:25+00:00

I have following database schema for tabel tblpers Field Type Null Key Default Extra

  • 0

I have following database schema for tabel tblpers

Field      Type         Null    Key     Default              Extra           Privileges                 

userid     int(11)      NO      PRI     (NULL)               auto_increment  
firstname  char(10)     NO                                
lastname   char(15)     NO              192.168.1.15
sdate  datetime     NO              0000-00-00 00:00:00
address    varchar(100) YES             (NULL)                

Here is my java code;

public class PSMTTest {

private static String jdbcDriver = "org.gjt.mm.mysql.Driver";
private static String host="localhost";
private static String userName = "root"; 
private static String password = "rootpass";

public static void main(String args[]){

    Connection con=null;
    try{
        if(jdbcDriver!=null){
            Class.forName(jdbcDriver);
            String url = "jdbc:mysql://"+host+"/mysql";
            con = DriverManager.getConnection(url,userName,password);

            String insertQuery="insert into `querytest`.`tblpers` " +
            "(`userid`,`firstname`,`lastname`,`sdate`,`address`)values ( NULL,?,?,Now(),NULL);";

            System.out.println("insertQuery : "+insertQuery);

            PreparedStatement psmt=con.prepareStatement(insertQuery);

            psmt.setString(2,"param");
            psmt.setString(3,"ganak");  //as per the sql exception error comes here
            psmt.executeUpdate();
            psmt.close();

        }
    }catch(ClassNotFoundException cnfe){
        cnfe.printStackTrace();
    }catch(SQLException sqle){
        sqle.printStackTrace();

    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }
}
}

But when I am tries to run the code It throws following exception

java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2474)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3191)
at PSMTTest.main(PSMTTest.java:45)

My userid is a primary key and auto increment field so value for it is automatically generated by database.
for the fields firstname and lastname I have the values.
but in case of date I want to add current data and time of the db.

In case of address the value is not provided yet so it should be kept as it is

Please tell me friends whats wrong with the query in my code?
why its giving an exception?
please guide me friends!
Thank You!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:25:26+00:00Added an answer on May 26, 2026 at 2:25 am

    The error message is self-explanatory – you’ve only specified two parameter placeholders (question marks) but you’re trying to use indexes 2 and 3. So this:

    psmt.setString(2, "param");
    psmt.setString(3, "ganak");
    

    should be:

    psmt.setString(1, "param");
    psmt.setString(2, "ganak");
    

    The fact that they are the second and third fields within the value list is irrelevant – they’re the first and second parameters.

    You could make the correspondence clearer by reordering your SQL though:

    String insertQuery = "insert into `querytest`.`tblpers` " +
        "(`firstname`, `lastname`, `sdate`, `userid`, `address`) values " + 
        "(?, ?, Now(), NULL, NULL);";
    

    Now the first and second prepared statement parameters correspond with the first and second fields specified in the SQL.

    It’s not clear to me that you need to specify the userid field anyway, mind you – given that it’s going to be provided by default. I would remove it from the SQL entirely, to be honest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following schema: Database: test. Table: per_login_user, Field: username (PK), password Database:
I have a database where I store objects. I have the following (simplified) schema
So I have the following user defined type in my oracle database: CREATE OR
I have a database in the following format: ID TYPE SUBTYPE COUNT MONTH 1
I have no control over database schema and have the following (simplified) table structure:
I have the following mysql table schema: SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; -- -- Database: `network` --
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have the following tables in my database that have a many-to-many relationship, which
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following function that is pulling data from a database. The ajax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.