2

Does exists a java library that can create sql statements? I'm not in search of something fancy, just something at "string manipulation" level: I just use jdbc (with Preparestatements and Resultsets) but I don't really like to pass huge strings containing SQL code...

What I need is a "simple" Select class (or something similar); in my mind all I really want is to be able to do

SQLStatement stat = Select("*").from("table").where("condition and condition").orderby("something");
ResultSet rs = Connection.getResultSet(stat.toString());
/* equals to "select * from table where condition and condition order by something" */

Maybe I'm blind, but I cannot find something like that...

Obviously, I want some methods/class able to write inserts and updates and the other stuff...

I excluded ORMs for two reasons:

  1. the db schema it's "old" and I cannot change it, and I'm not sure how can I adapt the ORM to follow our db
  2. AFAIK the ORMs needs to change the model (maybe adding a base class, maybe you need to implements an interface) and the model in my project is big, old and grumpy
  3. Onestly, I don't really like ORMs: Objects and Set theory just aren't made to be mapped (IMHO)
7
  • why not use an ORM? For there are plenty!!! Commented Aug 8, 2011 at 6:49
  • The reason this doesn't exist is probably two-fold: a.) replacing string concatenation with meaningful method names is not that much of a gain and b.) you should not build full selects using string manipulation in the first place (especially not the where condition). Commented Aug 8, 2011 at 6:50
  • Funny, last week I had the same thought! :) Commented Aug 8, 2011 at 6:51
  • Since SELECT,INSERT,UPDATE,DELETE Statements can vary from DB Server you are using. So Its very difficult to use it without a dialect. In Java most common Utility is Hibernate which have all these functions and many more. Commented Aug 8, 2011 at 6:54
  • @Joachim Sauer I always forget the precise syntax of the statements (I don't go at this level very often) and IDE simply cannot parse strings, so I found syntax problems at running times (Yes I do use unittest, but every time I found an error it's always just a typo like "SLECT" or "WERE") Commented Aug 8, 2011 at 7:02

4 Answers 4

2

Try SQLBuilder project. Honestly, I have not used this. Looking at their docs, i think it might suit your requirement.

You can also try to find similar APIs in Sourceforge,Google code etc..

Sign up to request clarification or add additional context in comments.

1 Comment

1

ORM (Object Relational Mapping) library is the clue.

Hibernate is the most mature one.

And the Hibernate-s Criteria API is object - oriented way to create such queries as You wished. Criteria API doc.

4 Comments

+1. Hibernate Criteria allows you to write queries such as: createCriteria(Stuff.class).add(Restrictions.eq("property", propValue)).addOrder(Order.asc("name")).list();
Hmm, I'm skimming the Criteria documentation and it seems to be what I want... just... why the hell they don't use an API similar to SQL?
@Vito: they do support HQL, which is somewhat similar to SQL, but building criterias programmatically has all the advantages you mentioned above.
Appears that Criteria still requires a jpa session etc. which is too heavy for my needs. Just want to build queries to execute with JdbcClient or JdbcTemplate
0

Hibernate is most likely what you're looking for. It contains many advanced features, but SQL statements are more straightforward.

Take a look at their site: http://www.hibernate.org/

I'd also recommend skimming through this guide:

https://www.owasp.org/index.php/Preventing_SQL_Injection_in_Java

1 Comment

I second this answer. Hibernate might exactly be what you are looking for. It maps all the database tables and relations as Java classes which you can use conveniently by method calls in a std java way instead of including huge chunks of sql code into your program.
0

I am not sure if you use Java for a native application or for the web. If you use Java for web you could consider using the Play framework.

Easy and has Hibernate included with a really simple implementation (easier when implementing Hibernate yourself).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.