Solved

Java Prepared Statement Problem

Posted on 2011-09-22
8
2,866 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
  • 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
 
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java DateChooser? 3 29
netstat -ano | find "8000" and taskkill /f /pid 2984 3 39
difference of if loops 23 43
Opening PDF on button click and fill new document 2 23
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now