The Artima Developer Community
Sponsored Link

Java Buzz Forum
Query By Critera Library For JDBC

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Query By Critera Library For JDBC Posted: Dec 9, 2003 1:08 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Query By Critera Library For JDBC
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

All of the recent talk about plans for OJB 1.1 including some thinking about an API and SPI seperation, the Hibernate query-by-criteria stuff, and general itchiness prompted me to finally get a reimplementation of an SQL query-by-criteria library dusted off, pieced together enough to be generally useful. and released. The gist of the library is that it allows for easy to use and flexible (hmm, seeing a theme?) dynamic SQL generation for use with JDBC.

SQLBuilder provides a convenient query-by-criteria style SQL generator in the QueryBuilder. It allows out-of-order addition of elements, will map prepared statement binding values for elements (which is needed when doing outof order generation), etc. There are some fancy things that it doesn't do which I would like it to do, but I get close to stepping on some IP issues with those so they aren't implemented =/ Still, as it exists it is perty useful methinks.

Sample usage:


public List findEmployees(Map constraints) throws SQLException
{
    PreparedStatement stmt = null;
    Connection conn = null;
    ResultSet results = null;
    try
    {
        QueryBuilder qb = QueryBuilder.select().all().from("employees e");
        Map bindings = new HashMap();
        if (constraints.containsKey("firstName"))
        {
            qb.where("e.first_name like {firstName}");
            bindings.put("firstName", "%" + constraints.get("firstName") + "%");
        }
        if (constraints.containsKey("lastName"))
        {
            qb.where("e.last_name like {lastName}");
            bindings.put("lastName", "%" + constraints.get("lastName") + "%");
        }
        if (constraints.containsKey("departmentName")
        {
            qb.leftOuterJoin("employees e", "departments d", 
                             "e.dept_id = d.id and d.name like {deptName}");
            bindings.put("deptName" "%" + constraints.get("departmentName") + "%");
        }
        Connection conn = // Obtain a connection;
        PreparedStatement stmt = conn.prepareStatement(qb.getQueryString());
        qb.bind(stmt, bindings);
        results = stmt.executeQuery();

        List emps = new ArrayList();
        while (results.next())
        {
            emps.add(buildEmployee(ResultSet));
        }
        return Collections.unmodifiableList(emps);
    }
    finally
    {
        if (results != null) results.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
    }
}

Source code , Binary Distribution and 1000 javadocs are all available. It has no 3rd party runtime dependencies, but the test suite depends on HSQLDB and JUnit It is presently BSD-style licensed.

Read: Query By Critera Library For JDBC

Topic: Mac::Glue and sticky environment variables Previous Topic   Next Topic Topic: The Preditor Party

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use