To know more about java tutorials.... Click here
More Javascript tutorials you can found here

Selection Sort Example Program in Java

April 19, 2014 Leave a comment

package in.malliktalksjava;

/**
* @author malliktalksjava
* @version 1.0
*
* Below class sorts the given array using SelectionSort
* mechanism and prints the output into console.
*/
public class SelectionSortExample {

/**
* @param args
*/
public static void main(String args[]){

int[] arr1 = {12,434,2,23,7,44,66,42};
int[] arr2 = doSelectionSort(arr1);
for(int var:arr2){
System.out.print(var);
System.out.print(“, “);
}
}

/**
* @param arr
* @return sorted array
*/
public static int[] doSelectionSort(int[] arr){

for (int count = 0; count < arr.length – 1; count++)
{
int index = count;
for (int count2 = count + 1; count2 < arr.length; count2++){
if (arr[count2] < arr[index]){
index = count2;
}
}

int smallerNumber = arr[index];
arr[index] = arr[count];
arr[count] = smallerNumber;
}
return arr;
}
}

About these ads

Bubble Sort Example in JAVA

April 18, 2014 Leave a comment

package in.malliktalksjava;

/**
* @author malliktalksjava
*
*/
public class BubbleSortExample {

private static int[] input = { 4, 2, 9, 6, 23, 11, 44, 0 };

/**
* @param args
* main method to run the logic.
*/
public static void main(String[] args) {
bubbleSort(input);

}
/**
* @param array – input array which required to sort.
*
* logic to sort the elements
*/
public static void bubbleSort(int array[]) {
int count = array.length;
int var;
for (int temp = count; temp >= 0; temp–) {
for (int temp2 = 0; temp2 < count – 1; temp2++) {
var = temp2 + 1;
if (array[temp2] > array[var]) {
swapNumbers(temp2, var, array);
}
}
printNumbers(array);
}
}

/**
* @param var1
* @param var2
* @param array
* Swap two numbers
*/
private static void swapNumbers(int var1, int var2, int[] array) {

int temp;
temp = array[var1];
array[var1] = array[var2];
array[var2] = temp;
}

/**
* @param input – Sorted array
*
* Prints the sorted array into console.
*/
private static void printNumbers(int[] input) {

for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + “, “);
}
System.out.println(“\n”);
}
}

Number series example program using JAVA

April 18, 2014 Leave a comment

package in.malliktalksjava;

/**
* @author malliktalksjava.in
*
* This program prints the numbers in below pattern
* 1
* 123
* 12345
* 1234567
* 12345
* 123
* 1
*/
class PrintingNumberPatterns{

public static void main(String[] args){
printNumberSeries();
}

/**
* Print the numbers in assending and decending order by iterating it.
*/
private static void printNumberSeries() {
for (int i = 1; i <= 7; i += 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}

for (int i = 5; i >= 1; i -= 2) {
for (int j = 1; j < i + 1; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

javax.servlet.ServletException: BeanUtils.populate

February 2, 2014 Leave a comment

javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:497)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:798)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:205)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Root Cause:

java.lang.IllegalArgumentException: argument type mismatch
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1789)
org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.java:1684)
org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:1713)
org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:1019)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:798)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:205)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Solution:

Fallowings are may be the reason for the above exception
1) The action attribute of an tag must match exactly the path attribute of the action definition in the struts-config.xml file. This is how Struts associates the ActionForm bean with the action.

2) This error usually occurs when you have specified a form name that does not exist in your tag. For example, you specific and ‘myForm’ is not the name of a form associated with myAction in the struts-config file

3) You get this message when Struts is unable to map the data in the HTML form to the properties in your ActionForm bean. Make sure each of the properties on your bean is either a String or a Boolean. Do you have any properties of type java.util.Date or other objects? That might cause this error. Also check to see that you have public getters and setter for each of your properties.

org.hibernate.exception.ConstraintViolationException

February 2, 2014 Leave a comment

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:91)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:86)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:171)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2048)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:353)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at struts.dao.project.budgetAnalysis.BudgetPlanningDAO.saveNewBudget(BudgetAnalysisDAO.java:428)
at struts.project.budgetAnalysis.NewBudgetPlanSaveAction.execute(NewBudgetSaveAction.java:99)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into (“ROOT”.”PROJECT_BUDGET_TEST”.”APPROVED_STATUS”)

at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:8739)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
… 36 more

 

Solution:

This type of problems generally occurs when we try to violate the constraint relationship of the tables.
The fallowing cases may be the reason for the above exception:
1)If the user try to enter null value into not null fields of the database.
2)If the user trying to store other format value into the database field.

org.hibernate.TransientObjectException

February 2, 2014 Leave a comment

org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before
flushing: hibernate.hr.employee.EmployeeStoreValue
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1826)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2172)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:84)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:353)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at struts.dao.project.budgetAnalysis.BudgetAnalysisDAO.updateProjectBudget(BudgetDAO.java:574)
at struts.project.budgetAnalysis.BudgetPlanningAction.approveRejectBudgetExecute(BudgetStoringgAction.java:480)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

Solution:

This type of Exception do you get generally when you try to store a null object in to the database column which has foreign key relationship with the other table column. Even if you are trying to store other column value which is not a primary key column.

Basic MQ Commands

January 19, 2014 1 comment

1. Create/start/stop a Queue Manager

crtmqm [-z] [-q] [-c Text] [-d DefXmitQ] [-h MaxHandles]
[-g ApplicationGroup] [-t TrigInt]
[-u DeadQ] [-x MaxUMsgs] [-lp LogPri] [-ls LogSec]
[-lc | -ll] [-lf LogFileSize] [-ld LogPath] QMgrName

Output :

WebSphere MQ queue manager created.
Creating or replacing default objects for QMA.
Default objects statistics : 43 created. 0 replaced. 0 failed.
Completing setup.
Setup completed.

2. Starting a Queue Manager

$ strmqm QMA

Output:

WebSphere MQ queue manager ‘QMA’ starting.
2108 log records accessed on queue manager ‘QMA’ during the log
replay phase.
Log replay for queue manager ‘QMA’ complete.
Transaction manager state recovered for queue manager ‘QMA’.
WebSphere MQ queue manager ‘QMA’ started.

3. Checking that the Queue Manager is running

$ dspmq

Output :

QMNAME(QMA) STATUS(Running)

4. Stopping a Queue Manager

$ endmqm –i QMA

5. Deleting a Queue Manager

The following command will stop all the Listeners associated with Queue Manager pointed to by the –m flag (QMA in this example). The –w flag means the command will wait until all the Listeners are stopped before returning control:

$ endmqlsr -w -m QMA

The command to stop (end) the Queue Manager is:

$ endmqm QMA

And fnally the command to delete the Queue Manager is:

$ dltmqm QMA

 

Other Useful Links:

Enable JMX Remote port in WebSphere

 

Follow

Get every new post delivered to your Inbox.

Join 208 other followers