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 6893551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:40:22+00:00 2026-05-27T06:40:22+00:00

How do I fix this ? I tested my query and it works in

  • 0

How do I fix this ? I tested my query and it works in the MySQL commandline and in workbench but how come when I try to use this SQL query on my java program it gives me this error???

Here’s my Test Class

package test;
import dao.FinanceDao;
import javax.swing.JTextArea;
import java.util.Scanner;
import java.util.List;
import javax.swing.JOptionPane;
import domainmodel.User;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

    public class TestDrive {

        public static void main(String[] args){
            String employeeID = null;
            String password = null;
            JTextArea text = new JTextArea();
            FinanceDao finance = new FinanceDao();
            DataSource  dataSource = SourceObj.getSource();

            finance.setDataSource(dataSource);

            String idNumber = JOptionPane.showInputDialog("Enter Employee ID to Search");

            List<User> test = finance.select(idNumber);
            for(User u: test){
            employeeID = u.getEmpID();
            password = u.getPassword();
            if(employeeID == null){
                text.setText("No Result");
            }else{
                text.setText("\tEmployeeID: "+employeeID+"\n\tPassword: "+password);
                }
            }

            List <User> test1 = finance.selectAll();
            for(User ux: test1){
                employeeID = ux.getEmpID();
                password = ux.getPassword();
                System.out.println("\tEmployeeID: "+employeeID+"\n\tPassword: "+password);
            }

            UI U = new UI(text);
        }
    }

Here’s the other class

package dao;

import javax.sql.DataSource;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import dao.mapper.UserRowMapper;
import domainmodel.User;

public class FinanceDao implements Manage {

    private DataSource ds;

    @Override
    public void setDataSource(DataSource ds) {
        this.ds = ds;

    }

    @Override
    public void Add(String empID, String password) {
        JdbcTemplate Add = new JdbcTemplate(ds);
        Add.update("INSERT INTO user (empID,password) VALUES(?,?)",
        new Object[] { empID, password });
    }

    @Override
    public void Delete(String empID , String password) {
        JdbcTemplate Delete = new JdbcTemplate(ds);
        Delete.update("Delete from User where emp_id = '?'",new Object[]{empID});
    }


    public List<User> select(String empID) {
        JdbcTemplate select = new JdbcTemplate(ds);
        return select.query(
                "select * FROM USER where EMPID = ? ",
                new Object[] { empID }, new UserRowMapper());
    }

    public List<User>selectAll(){
        JdbcTemplate showAll = new JdbcTemplate(ds);
        return showAll.query("Select empID from User", new UserRowMapper());
    }

}

My Result Extractor

package dao.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.ResultSetExtractor;

import domainmodel.User;

public class UserResultSetExtractor implements  ResultSetExtractor{

    @Override
    public Object extractData(ResultSet rs) throws SQLException{
            User user = new User();
            user.setEmpID(rs.getString(1));
            user.setPassword(rs.getString(2));
        return user;
    }

}

And lastly this is the error

Dec 6, 2011 12:05:02 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Dec 6, 2011 12:05:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
Dec 6, 2011 12:05:09 PM org.springframework.jdbc.support.SQLErrorCodesFactory <init>
INFO: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [Select empID from User]; SQL state [S1009]; error code [0]; Column Index out of range, 2 > 1. ; nested exception is java.sql.SQLException: Column Index out of range, 2 > 1. 
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:120)
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:276)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:353)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:404)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:412)
    at dao.FinanceDao.selectAll(FinanceDao.java:42)
    at test.TestDrive.main(TestDrive.java:42)
Caused by: java.sql.SQLException: Column Index out of range, 2 > 1. 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.ResultSetImpl.checkColumnBounds(ResultSetImpl.java:818)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5651)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
    at dao.mapper.UserResultSetExtractor.extractData(UserResultSetExtractor.java:16)
    at dao.mapper.UserRowMapper.mapRow(UserRowMapper.java:13)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
    at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:394)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:342)
    ... 4 more

How would I fix this?

  • 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-27T06:40:22+00:00Added an answer on May 27, 2026 at 6:40 am

    it seems like user table only have 2 columns which you can get by calling
    user.setEmpID(rs.getString(0));
    user.setPassword(rs.getString(1));
    and when you use
    user.setPassword(rs.getString(2));
    You are trying to get column number 3 which does not exist and that’s why you are getting this error.


    Change your query in this to return all columns:

    public List<User>selectAll(){
        JdbcTemplate showAll = new JdbcTemplate(ds);
        return showAll.query("Select * from User", new UserRowMapper());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure out how to fix this annoying problem. Searched and tested a
How do I fix this so that when php calls $fontcolor and the user
Any ideas how to fix this? UserService.UserServiceClient userServiceClient = new UserServiceClient(); userServiceClient.GetUsersCompleted += new
How do i fix this link in javascript. <a href=javascript:clientGalleryLink(Business)>Link</a> Its missing single quotes
How can I fix this? REGEX: //REGEX $match_expression = '/Rt..tt<\/td> <td>(.*)<\/td>/'; preg_match($match_expression,$text,$matches1); $final =
How do I fix this error? error C2668: 'std::_Tree<_Traits>::end' : ambiguous call to overloaded
How would you fix this code? template <typename T> void closed_range(T begin, T end)
How can I fix this error, and install thin or other gems? $ sudo
I've been trying to fix this for two hours straight and I can't figure
Even after all the hotfixes and updates that are supposed to fix this, my

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.