• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic