Java JDBC Derby Connection getRowsFromDatabase(Connection con, int numberOfRows, boolean reuseConnection, String driver, String dsn, String user, String password, String tableName, String whereString, String orderByString, String groupByString)

Here you can find the source of getRowsFromDatabase(Connection con, int numberOfRows, boolean reuseConnection, String driver, String dsn, String user, String password, String tableName, String whereString, String orderByString, String groupByString)

Description

get Rows From Database

License

Open Source License

Declaration

public static Vector getRowsFromDatabase(Connection con, int numberOfRows, boolean reuseConnection,
            String driver, String dsn, String user, String password, String tableName, String whereString,
            String orderByString, String groupByString) throws Exception 

Method Source Code

//package com.java2s;
/*//from  w w w .  j  a  va2s .co m
 * Id: 
 *
 * Copyright (C) 2004, Cladonia Ltd. All rights reserved.
 *
 * This software is the proprietary information of Cladonia Ltd.  
 * Use is subject to license terms.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

public class Main {
    public static Vector getRowsFromDatabase(Connection con, int numberOfRows, boolean reuseConnection,
            String driver, String dsn, String user, String password, String tableName, String whereString,
            String orderByString, String groupByString) throws Exception {

        if (reuseConnection == false) {
            //close and reopen connection
            con.close();
            con = getConnection(driver, dsn, user, password);
        }

        Vector temp = new Vector();
        String query = null;
        String[] separated;

        Statement stmt = con.createStatement();

        //allow the setMaxRows to be set
        if (numberOfRows > 0)
            stmt.setMaxRows(numberOfRows);

        query = "select * from " + tableName;

        if (numberOfRows == 0) {
            String whereString1 = whereString;
            String orderByString1 = orderByString;
            String groupByString1 = groupByString;

            //check for whereString
            //System.out.println(">"+whereString+"<"+"\n"+">"+whereString.trim()+"<");
            if (whereString1.trim().length() > 0) {
                final String WHERE = "where ";

                //add it to the query
                if ((whereString1.indexOf(WHERE) > -1) || (whereString1.indexOf(WHERE.toUpperCase()) > -1)) {
                    query += " " + whereString;
                } else {
                    query += " where " + whereString;
                }

            }
            //check for orderbyString
            if (orderByString1.trim().length() > 0) {
                //add it to the query
                final String ORDER_BY = "order by ";
                if ((orderByString1.indexOf(ORDER_BY) > -1)
                        || (orderByString1.indexOf(ORDER_BY.toUpperCase()) > -1)) {
                    query += " " + orderByString;
                } else {
                    query += " order by " + orderByString;
                }

            }
            //check for groupbyString
            if (groupByString1.trim().length() > 0) {
                //add it to the query
                final String GROUP_BY = "group by ";
                if ((groupByString1.indexOf(GROUP_BY) > -1)
                        || (groupByString1.indexOf(GROUP_BY.toUpperCase()) > -1)) {
                    query += " " + groupByString;
                } else {
                    query += " group by " + groupByString;
                }
            }

        }

        ResultSet data = stmt.executeQuery(query);
        ResultSetMetaData rmeta = data.getMetaData();
        separated = new String[rmeta.getColumnCount()];
        //get column names
        for (int ccnt = 1; ccnt < rmeta.getColumnCount() + 1; ++ccnt) {
            separated[ccnt - 1] = rmeta.getColumnName(ccnt);
            //System.out.println(rmeta.getColumnName(ccnt));
        }
        temp.add(separated);
        while (data.next()) {
            String[] sep = new String[rmeta.getColumnCount()];
            for (int cnt = 1; cnt < rmeta.getColumnCount() + 1; ++cnt) {
                sep[cnt - 1] = data.getString(cnt);
            }
            temp.add(sep);
        }

        return (temp);
    }

    public static Connection getConnection(String driver, String dsn, String user, String password)
            throws Exception {
        Connection con1 = null;
        Class.forName(driver);
        con1 = DriverManager.getConnection(dsn, user, password);

        return (con1);
    }
}

Related

  1. getDerbyConnection(String filename)
  2. getDerbyDatabaseProperty(Statement derbyStatementForQuerying, String propertyKey)
  3. getDerbyUnitConnection()
  4. getDriver(String className)
  5. getLocalConnection()
  6. getSessionIds()
  7. getSize2()
  8. getTables(File database)
  9. printStats(String statstring)