Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to search teachers from database using first name and last name in java,
I am trying to passed parameters (String firstname, String lastname) into setString method. I have to get the result using first and last name.
But, it does not seem to work,I have to pass the unit test, and when i run the unit test, The unit test tells me that "no value specified for parameter 2".
What is the other way of doing it? as i can not seem to find it.
I did the same thing when i was searching by id and it worked.
here is the method that i used when i was searching by id
public Teacher findTeacher(int id) throws SQLException
	{
		
		Teacher teacher = new Teacher(0, null, null);
		
		String selectSQL = "SELECT * FROM database_activity.Teacher WHERE id = ?";
		PreparedStatement statement = conn.prepareStatement(selectSQL);
		statement.setString(1, String.valueOf(id));
		ResultSet resultSet = statement.executeQuery();
		try {
			
			while(resultSet.next())

				teacher = new Teacher(Integer.parseInt(resultSet.getString("id")), resultSet.getString("firstname"), 
						resultSet.getString("lastname"));
				
			} 
		catch (SQLException e) 
		{
			
		}
		return teacher;
		
			
	}


I have Teacher class with constructor and getters and setters.
public class Teacher {

	private int id;
	private String firstName;
	private String lastName;
        public Teacher(int id, String firstName, String lastName) 
	{
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}
          public int getID() {
		return id;
	}
          public void setId(int id) {
		this.id = id;
	}
          public String getFirstName() {
		return firstName;
	}
          public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
          public String getLastName() {
		return lastName;
	}
          public void setLastName(String lastName) {
		this.lastName = lastName;
	}
        @Override
	  public String toString() 
	{
		return "id: " + id +"Name: " + firstName + "Surname: " + lastName;
	}


What I have tried:

public List<Teacher> findTeacher(String firstName, String lastName) throws SQLException {
		
		List<Teacher> list = new ArrayList<>();
		Teacher teacher = new Teacher(0, null, null);		
		String query = "SELECT * FROM database_activity.Teacher WHERE        firstname like %?% and lastname like %?%";
		PreparedStatement statement = conn.prepareStatement(query);
		statement.setString(1, String.valueOf(firstName));
		statement.setString(1, String.valueOf(lastName));
		ResultSet resultSet = statement.executeQuery();
		try {
			while(resultSet.next())
			teacher = new  Teacher(Integer.parseInt(resultSet.getString("id")),resultSet.getString("firstname") , resultSet.getString("lastname"));
			    
                    list.add(teacher);
		    }
                  catch (SQLException e) 
                    {}
		return list;

	}
Posted
Updated 5-Feb-19 6:42am
v2
Comments
Richard MacCutchan 5-Feb-19 11:33am    
"But, it does not seem to work,"
We cannot guess what that means. Please explain exactly what does not work, what results you see and where they occur.

Also writing catch blocks that ignore the exception is a great way to make certain that your code "does not work".
Nabeel Munir 5-Feb-19 11:35am    
Hello @Richard. Actually i have to pass the unit test and when i execute and and run the unit test, it tells me that, "no value specified for parameter 2".
Richard MacCutchan 5-Feb-19 11:37am    
What and where does this happen? Please do not assume that we can read your mind.
Nabeel Munir 5-Feb-19 11:39am    
I m sorry for inconvenience.

statement.setString(1, String.valueOf(firstName));
statement.setString(1, String.valueOf(lastName));

The above two statements does not seem to work.
Richard MacCutchan 5-Feb-19 11:55am    
No, of course they don't. You are setting both values as parameter number 1. Please spend more time checking your code if it does not work.

You need quotes round the SQL LIKE string or it won't be recognised:
Java
String query = "SELECT * FROM database_activity.Teacher WHERE firstname like '%' + ? + '%' and lastname like '%' + ? + '%'";
 
Share this answer
 
Besides the string needing to be wrapped in quotes as OG pointed out; it looks like you are adding then redefining the first parameter
Java
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, String.valueOf(firstName));//       setString(1   OK
statement.setString(1, String.valueOf(lastName)); //       setString(1...again
ResultSet resultSet = statement.executeQuery();

I think it should be more like this:
Java
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, String.valueOf(firstName));..       setString(1    
statement.setString(2, String.valueOf(lastName)); //       setString(2
ResultSet resultSet = statement.executeQuery();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900