[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Java Prepared Statement Problem

Posted on 2011-09-22
8
Medium Priority
?
3,673 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 4

Accepted Solution

by:
reijnemans earned 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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