• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to validate the syntax of a SQL query without executing it using JDBC?

 
Greenhorn
Posts: 3
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to validate the syntax of a SQL query without executing it using JDBC?

I know about the 'set noeexec on' in Sybase, but I want to use a similar functionality in JDBC call.

The command 'set noexec on' in Sybase works if used in a env like Rapid SQL. However I want to use it with Prepared statements.

[code=java]String sqlValue = "select * from table_name";
Connection conn = ConnectionPool.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sqlValue);
boolean queryValidityResult = pstmt.execute();[/code]
Is it right to use the 'set noexec on' like this?

[code=java]String sqlValue = "set noexec on " + "select * from table_name";
Connection conn = ConnectionPool.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sqlValue);
boolean queryValidityResult = pstmt.execute();[/code]
Or is there any setter property on PreparedStatement which would help?

Or is there any other way by which I can just validate my SQL instead of executing it?

Any ideas?
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should write and test your SQL in your database's SQL interpreter before putting it into your Java code. This is the quickest and most reliable way to ensure that your SQL is valid, that it runs against the tables/columns in your database schema, and that it returns the correct results. Until you have done this, your SQL is just a string that might or might not be correct. Once you know your SQL is correct, you can put it into your Java code. Don't be afraid of the database: if you are writing SQL you should know how to execute it in your database. If you are afraid of executing SQL in your database, you shouldn't be writing SQL in the first place.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic