Link to home
Start Free TrialLog in
Avatar of GAUTAM
GAUTAMFlag for United States of America

asked on

Java Prepared Statement Problem

Hi Experts...
I have a prepared statement which queries StudentId,Names as shown below in code.
Here getNames function calls the runSutudentFetchQuery function by passing id one at a time.

I am getting the exception as below:
java.sql.SQLException: JZ0S3: The inherited method executeQuery(String) cannot be used in this subclass.
        at com.sybase.jdbc2.jdbc.ErrorMessage.raiseError(ErrorMessage.java:426)
        at com.sybase.jdbc2.jdbc.SybPreparedStatement.executeQuery(SybPreparedStatement.java:76)
        at radioDb.runStudenttFetchQuery(radioDb.java:88)
        at radioDb.getNames(radioDb.java:25)
        at Automate.main(radioAutomat.java:342)

whose description is
Description: PreparedStatement does not support executeQuery(String), executeUpdate(String), or execute(String).
Please help...

public static ResultSet getNames(String ID)
{
	String query="select StdentId,SName";
		   query+=" from dbo.Student";
		   query+=" where studentID=?";
		   
		   ResultSet rs=runStudentFetchQuery(query,ID);
		   return rs;
		   
}

public static ResultSet runStudentFetchQuery(String query,String ID) 

{
	Connection conn=null;
	ResultSet result=null;
	try
	{

   conn = getConnection();
   PreparedStatement stat = conn.prepareStatement(query);
   stat.setString(1,ID);
   result = stat.executeQuery(query);
   return result;
}
catch(IOException e)
{
	e.printStackTrace();
}
catch(SQLException e)
{
	e.printStackTrace();
}
finally
{
  try 
  {
	conn.close();
   } 
   catch (SQLException e) 
   {
	e.printStackTrace();
   }
}
	return result;
}

Open in new window

Avatar of reijnemans
reijnemans
Flag of Netherlands image

The follwoing is written on the Sybase webpage:

The inherited method _____ cannot be used in this subclass.

Description: PreparedStatement does not support executeQuery(String), executeUpdate(String), or execute(String).

Action: If you want to pass a query string, use Statement, not PreparedStatement.
Avatar of GAUTAM

ASKER

@reijnemans:Yes i saw that.
How do i solve this.
Please help...
use the method createStatement not prepareStaement.
ASKER CERTIFIED SOLUTION
Avatar of reijnemans
reijnemans
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
uncomment the line
// conn = getConnection();

Open in new window

Avatar of GAUTAM

ASKER

@reijnemans:i tried the below query.
I passed an id an it returns the error:com.sybase.jdbc2.jdbc.SybSQLException: Invalid column name 'SS15'
But if i run the same query directly i get the correct values i.e:-StudentID,Studentname
Please help....

public static ResultSet getNames(String ID) {
		String query = "select StdentId,SName";
		query += " from dbo.Student";
		query += " where studentID = " + ID;

		ResultSet rs = runStudentFetchQuery(query, ID);
		return rs;

	}

Open in new window

Avatar of GAUTAM

ASKER

@reijnemans:How do i single quote the value SS15.
If single quoted it will not cause the error.
Please help...
Avatar of GAUTAM

ASKER

@reijnemans:Thanks a lot solved it.
 query+=" where StudentID = '"+ID+ "'";