Vito De Tullio
Vito De Tullio

Reputation: 1683

Java library to write sql statements?

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)

Upvotes: 2

Views: 5037

Answers (4)

chedine
chedine

Reputation: 2374

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..

Upvotes: 2

Justin
Justin

Reputation: 2999

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).

Upvotes: 0

never
never

Reputation: 681

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.

Upvotes: 1

Dot NET
Dot NET

Reputation: 4907

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

Upvotes: 0

Related Questions