Posts

Showing posts from April, 2012

Getting the last inserted record id of a database table in java

Image
Getting the last inserted record id of a database table in java The new JDBC-3.0 method getGeneratedKeys() which is now the preferred method to use if you need to retrieve AUTO_INCREMENT keys. PreparedStatement ps = con.prepareStatement("insert into emp values(?,?)", Statement.RETURN_GENERATED_KEYS);             ps.setInt(1, e.getId());             ps.setString(2, e.getName());             ps.executeUpdate();             ResultSet rset = ps.getGeneratedKeys();            while (rset.next()) {            int i = rset.getInt(1);            System.out.println("key=" + i);     ...

MultiActionController with Command Class in Spring

Image
 MultiActionController with Command Class How one can use command class Object while working with MultiActionController? MultiActionController is used to group logically related behaviours (operations) in single Controller. It is something like struts DispatchAction but provides more flexibility. In below example take two numbers from user and display result as addition or subtraction or multiplication based on which button is clicked. All of three operations are handled by a single Controller class. Outputwhen clicked on add button Steps 1.        Create a new project and add Spring Web MVC library 2.        Create input page like below Index.jsp <body>        <h3> Enter Numbers:</h3>         <form action="send.htm"  method="post">          ...

SimpleFormController in Spring with Validator

Image
SimpleFormController How will be provide valid form data to the Controller in Spring , answer is SimpleFormController. This class provided very customizable or simple way to process form data in spring. Which is describe diagrammatically below. The SimpleFormController creates and populates a command bean, which contains all the parameters from the form as JavaBean properties accessible via getters and setters. The command bean will be validated if validation is enabled, and if no errors are found, the command bean will be processed. Otherwise, the original form page will be shown again, this time with the errors from validation. Septs: 1.        Create new project and include Spring MVC library 2.        Create input page like below Spring Form tags is used for input designing Index.jsp <%@taglib  uri="http://www.springframework.org/tags/form" prefix="form" %> <h...

Table per class hierarchy mapping in hibernate

Image
Representing an inheritance relationship using Table per class hierarchy An entire class hierarchy could be mapped to a single table. This table would include columns for all properties of all classes in the hierarchy. The concrete subclass represented by a particular row is identified by the value of a type discriminator column. This mapping strategy is a winner in terms of both performance and simplicity How to persist object of Details in database Steps: 1.        Create hibernate application and include hibernate library 2.        Create java bean class which represent inheritance relationship public class User {     String uname;     String upass;      public String getUname() {         return uname;     }     public void setUname(String uname) {   ...

ListView, ListActivity and Custom Adapter in Android

Image
ListView  A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view. ListAdapter defines layout for individual row of the list and provide data to the ListView via setAdapter() method of ListView. Android provides several standard adapters; the most important are ArrayAdapter and CursorAdapter . ArrayAdapter can handle data based on Arrays or Lists while SimpleCursorAdapter handle database related data. In this example ArrayAdapter is used, ArrayAdapter  provide constructor with four parameters to construct an ArrayAdapter For Example: ArrayAdapter(this, android.R.layout.simple_list_item_1,   android.R.id.text1,  name); Explanation of arguments: First parameter    : The current Context Second parameter   : ID for a row layout to use when instantiating views Third parameter -  : The id of the TextView within the layout resource to be populated ...