All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ro.nextreports.engine.queryexec.demo.QueryExecDemo Maven / Gradle / Ivy

Go to download

NextReports Engine is a lightweight Java platform development library which can be used to run NextReports inside your applications.

There is a newer version: 9.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ro.nextreports.engine.queryexec.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import ro.nextreports.engine.queryexec.Query;
import ro.nextreports.engine.queryexec.QueryExecutor;
import ro.nextreports.engine.queryexec.QueryParameter;
import ro.nextreports.engine.queryexec.QueryResult;
import ro.nextreports.engine.queryexec.util.QueryResultPrinter;
import ro.nextreports.engine.queryexec.util.SqlFile;
import ro.nextreports.engine.queryexec.util.StringUtil;


/**
 * @author Decebal Suiu
 */
public class QueryExecDemo {

    private static Connection conn;
    
    public static void main(String[] args) {   
    	QueryResult result = null;
        try {
            SqlFile sqlFile = new SqlFile("demo.sql");
            String sql = sqlFile.getSqlList().get(0);
            System.out.println("=== sql ===");
            System.out.println(sql);
            
            sql = format(sql);
            System.out.println();
            System.out.println("=== sql after format ===");
            System.out.println(sql);

            Query query = new Query(sql);
            String[] parameterNames = query.getParameterNames();
            printParameterNames(parameterNames);
            
            Connection conn = getConnection();
            Map parameters = new HashMap();
//            QueryParameter param = new QueryParameter("clientId", "", QueryParameter.INTEGER_VALUE);
            QueryParameter param = new QueryParameter("name", "", QueryParameter.STRING_VALUE);
            parameters.put(param.getName(), param);
            Map values = new HashMap();
//            values.put(param.getName(), new Integer(1000));
            values.put(param.getName(), "M%");
            QueryExecutor executor = new QueryExecutor(query, parameters, values, conn);
            result = executor.execute();
            System.out.println("columns = " + result.getColumnCount());
            //System.out.println("rows = " + result.getRowCount());
            QueryResultPrinter.printResult(result);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        } finally {
        	if (result !=  null) {
        		result.close();
        	}
        }
    }

    private static void printParameterNames(String[] parametersNames) {
        System.out.println("Parmaeter names:");
        for (int i = 0; i < parametersNames.length; i++) {
            System.out.println(parametersNames[i]);
        }
    }
    
    private static String format(String sql) {
        sql = StringUtil.stripSingleLineComments(sql);
        sql = StringUtil.stripMultiLineComments(sql);
        sql = StringUtil.stripBlankLines(sql);
        sql = StringUtil.trimLines(sql);
        sql = StringUtil.replaceTabsWithSpaces(sql, 1);
        sql = StringUtil.stripNewlines(sql);
        return sql;
    }

    private static Connection getConnection() throws Exception {
        if (conn == null) {
            // load the JDBC driver
            String driverName = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driverName);

            // Create a conn to the database
//            String serverName = "192.168.16.205";
            String serverName = "192.168.12.14";
            String portNumber = "1521";
//            String sid = "ksi";
            String sid = "dev1";
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
//            String username = "Kollecto";
            String username = "misdev1";
//            String password = "ksi";
            String password = "misdev1";
            conn = DriverManager.getConnection(url, username, password);
        }

        return conn;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy