0

Configure OracleDataSource in Struts

Following is the configuration to be defined in <data-sources> and </data-sources>

Things to be remember while defining the OracleDataSource in Struts are
  1. URL should be in uppercase otherwise it will throw following exception

    java.sql.SQLException: Invalid Oracle URL specified: OracleDataSource.makeURL

  2. username and password should be defined in URL string and not separate property. Otherwise it will throw following exception

    java.sql.SQLException: invalid arguments in call

<data-source type="oracle.jdbc.pool.OracleDataSource"
key="ods">
<set-property property="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<set-property property="URL"
value="jdbc:oracle:thin:username/password@localhost:1521:dbname" />
<!--
Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<!--
Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<!--
Maximum time to wait for a dB connection to become available
in ms, in this example 5 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<set-property property="maxWait" value="5000" />
<set-property property="readOnly" value="true" />
<set-property property="autoCommit" value="false" />
</data-source>

|
5

Oracle 10g : How to pass ARRAYS to Oracle using Java?

Here i'm trying to send an array of integers as a parameter to a callablestatement.


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

import javax.sql.DataSource;

import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.StructDescriptor;

import com.cs.datasource.CardSellerDataSource;

public class TestMessageBroadCasting {

public static void main(String args[]) {

try {
DataSource dataSource = ... ...;

Connection connection = dataSource.getConnection();


String sql = "{call package_name.proc_name(?, ?, ?, ?, ?, ?)}";


Integer [] arr = {1 , 2};
ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor(
"RESELLERLIST", connection);
ARRAY array = new ARRAY(arrayDescriptor, connection, arr);


CallableStatement callableStatement = (oracle.jdbc.driver.OracleCallableStatement)connection.prepareCall(sql);
callableStatement.setInt(1, 1);
callableStatement.setString(2, "A for apple"
+ System.currentTimeMillis());
callableStatement.setArray(3, array);
callableStatement.setString(4, "0");
callableStatement.setInt(5, 0);
callableStatement.setString(6, "");
callableStatement.registerOutParameter(4, Types.VARCHAR);
callableStatement.registerOutParameter(5, Types.INTEGER);
callableStatement.registerOutParameter(6, Types.VARCHAR);
callableStatement.execute();

String b = callableStatement.getString(6);
System.out.println("message : " + b);

} catch (SQLException e) {
e.printStackTrace();
}

}
}



So, first define an array of integer

Integer [] arr = {1 , 2};

We need a ArrayDescriptor.

ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor(
"VALUELIST", connection);

VALUELIST is the name of the type defined in your database(Oracle)


Two points we need to note are 
  1. VALUELIST must be defined in SCHEMA LEVEL rather than PACKAGE LEVEL
  2. VALUELIST must always be typed in uppercase letter.
The errors you might get while not defining the name of the type well is
java.sql.SQLException: invalid name pattern: ... ...






|
0

Know the name of currently executing method in Java

This method is used to get the name of method currently executing.

protected String getCurrentlyExecutingMethodName() {
  Throwable t = new Throwable(); 
  StackTraceElement[] elements = t.getStackTrace(); 
  if (elements.length <= 0) return "[No Stack Information Available]";
  // elements[0] is this method
  if (elements.length < 2) return null;
  return elements[1].getMethodName();
}

|
0

Unable to open folder by double clicking...

If double clicking a folder opens search page then try one of the followings

  1. in run type regsvr32 /i shell32.dll
  2. Press Ok 
Or
  1. In registry search for 'MountPoint2' and delete all entries


|
0

Truncate time part from date [ ORACLE TRUNC equivalent in MySQL ]

In Oracle TRUNC(data_time_var) returns only the date portion of a date_time data type.

In MySQL to truncate time part from date we have to use

SELECT DATE_FORMAT(date_time_var, '%Y-%m-%d')
Try different date formate.

Further date time function in mysql can be found in
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

|
0

How to know which method you are in?

  1. public class MethodName {  
  2.    
  3.  public String methodOne() {  
  4.   return new Exception().getStackTrace()[0].toString();  
  5.  }  
  6.    
  7.  public String methodTwo() {  
  8.   return Thread.currentThread().getStackTrace()[2].toString();  
  9.  }  
  10.   
  11.  public static void main(String[] args) {  
  12.   MethodName name = new MethodName();  
  13.     
  14.   long start = System.currentTimeMillis();  
  15.   for (int i = 0; i < 100000; i++) {  
  16.    name.methodOne();  
  17.   }  
  18.   System.out.printf("First method - new Exception() - in %d millis\n",  
  19.      System.currentTimeMillis() - start);  
  20.     
  21.   start = System.currentTimeMillis();  
  22.   for (int i = 0; i < 100000; i++) {  
  23.    name.methodTwo();  
  24.   }  
  25.     
  26.   System.out.printf("Second method - Thread.currentThread() - in %d millis\n",  
  27.      System.currentTimeMillis() - start);  
  28.  }  
  29. }  

|
1

JasperReport Export to Excel Snippet code

Sample code that will export JasperReport(.jasper) to excel, pay attention to the bold code
that's all the tricks. The sample servlet will accept two parameter the date and the name of the report.


/*
* JasperToExcel.java
*/

import bean.report.RowStatistics;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

/**
*
* @author amontejo
* @version
*/
public class JasperToExcel extends HttpServlet {

public static final String REPORT_DIRECTORY = "/reports";

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String reportName = request.getParameter("reportname");
String selectedyear = request.getParameter("SelectedYear");
InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/" + REPORT_DIRECTORY + "/" + reportName + ".jasper");

ServletContext context = this.getServletConfig().getServletContext();
JasperPrint jasperPrint = null;
HashMap parameterMap = new HashMap(); 

parameterMap.put("parayear", new String(selectedyear));

try {
ServletOutputStream servletOutputStream = response.getOutputStream(); 
JRDataSource dataSource = createReportDataSource(request, selectedyear, reportName);
jasperPrint = JasperFillManager.fillReport(reportStream, new HashMap(), dataSource);
generateXLSOutput(reportName, jasperPrint, response);
} catch (Exception e) {
}
}

private String tagreport(String string) {
java.util.Calendar calendar = java.util.Calendar.getInstance();
return string + calendar.get(calendar.MONTH) + calendar.get(calendar.DAY_OF_MONTH) + calendar.get(calendar.YEAR);
}

private void generateXLSOutput(String reportname,
JasperPrint jasperPrint,
HttpServletResponse resp)
throws IOException, JRException {
String reportfilename = tagreport(reportname) + ".xls";
JExcelApiExporter exporterXLS = new JExcelApiExporter();

exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, resp.getOutputStream());
resp.setHeader("Content-Disposition", "inline;filename=" + reportfilename);
resp.setContentType("application/vnd.ms-excel");

exporterXLS.exportReport();
}


// 
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// 

}

|

Copyright © 2009 So That I Can Remember All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.