Solved

Java Prepared Statement Problem

Posted on 2011-09-22
8
3,221 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:gaugeta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:reijnemans
ID: 36580390
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.
0
 

Author Comment

by:gaugeta
ID: 36580401
@reijnemans:Yes i saw that.
How do i solve this.
Please help...
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36580412
use the method createStatement not prepareStaement.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
reijnemans earned 500 total points
ID: 36580469
example:
	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;

	}

	public static ResultSet runStudentFetchQuery(String query, String ID)

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

			// conn = getConnection();
			Statement stat = conn.createStatement();
			result = stat.executeQuery(query);
			return result;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

Open in new window

0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36580474
uncomment the line
// conn = getConnection();

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36580611
@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

0
 

Author Comment

by:gaugeta
ID: 36580632
@reijnemans:How do i single quote the value SS15.
If single quoted it will not cause the error.
Please help...
0
 

Author Comment

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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question