Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

An easy JDBC wrapper

A quick data access solution for simple programs


Table.java listing
// $Id$

import java.sql.*;

public class Table
{
  private Database database;
  private String name;

  public Table( Database database, String name ) {
    this.database = database;
    this.name = name;
  }

  private RowSet execute( String criteria ) throws SQLException {
    Connection con = database.getConnection();
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery( "select * from "+name+
      (criteria==null?"":(" where "+criteria)) );
    ResultSetMetaData rsmd = rs.getMetaData();
    RowSet rows = new RowSet();
    int cols = rsmd.getColumnCount();
    while (rs.next()) {
      Row row = new Row();
      for (int i=0; i<cols; ++i) {
        String name = rsmd.getColumnName( i+1 );
        String value = rs.getString( i+1 );
        row.put( name, value );
      }
      rows.add( row );
    }
    con.close();
    return rows;
  }

  public Row getRow( String criteria ) throws SQLException {
    RowSet rs = execute( criteria );
    return rs.get( 0 );
  }

  public RowSet getRows( String criteria ) throws SQLException {
    RowSet rs = execute( criteria );
    return rs;
  }

  public RowSet getRows() throws SQLException {
    return getRows( null );
  }

  public void putRow( Row row ) throws SQLException {
    putRow( row, null );
  }

  public void putRow( Row row, String conditions ) throws SQLException {
    String ss = "";
    if (conditions==null) {
      ss = "INSERT INTO "+name+" VALUES (";
      for (int i=0; i<row.length(); ++i) {
        String v = row.get( i );
        ss += "'"+v+"'";
        if (i != row.length()-1)
          ss += ", ";
      }
      ss += ")";
    } else {
      ss = "UPDATE "+name+" SET ";
      for (int i=0; i<row.length(); ++i) {
        String k = row.getKey( i );
        String v = row.get( i );
        ss += k+"='"+v+"'";
        if (i != row.length()-1)
          ss += ", ";
      }
      ss += " WHERE ";
      ss += conditions;
    }

    System.out.println( "SS "+ss );
    Connection con = database.getConnection();
    Statement st = con.createStatement();
    st.executeUpdate( ss );
  }
}

Return to the article.