Solved

Java Prepared Statement Problem

Posted on 2011-09-22
8
2,967 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
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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell script 13 101
tomcat not starting 6 45
Exchange 2013 free and busy between mailbox databases 77 92
ESX 6.0 Best Practices for datastore size 5 77
This article describes some very basic things about SQL Server filegroups.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 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