Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Monday, October 22, 2012

Basic Constructor Injection


Through the Spring beans XML file you can configure your bean to initialize with an argument for the constructor, and then assign the arguments. Spring essentially “injects” the argument into your bean. This is referred to as constructor injection. The following example passes in the String message using a constructor. The class is the same as the one in Basic Bean Creation except the default message on the message variable has been cleared and is now set to null. A single parameter constructor has been added to set a message.

ConstructorMessage .java

public class ConstructorMessage {
  private String message = null;
  public ConstructorMessage(String message) {
    this.message = message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
  public String getMessage() {
   return message;
  }
}

The constructor-arg element injects a message into the bean using the constructor-arg element's value attribute.

ConstructorMessageTest-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="message" class="org.springbyexample.di.xml.ConstructorMessage">
    <constructor-arg  value="Spring is fun." />
  </bean>
</beans>

Monday, September 27, 2010

Read data from Excel worksheet In Java

Let's assume we have a worksheet like this
LASTNAME       FIRSTNAME   ID
waran          Vignesh     102
sekaran        Rethna      111
mathi          kalai       200
Vishnu         Ananada     300
To access this data, we can use the JDBC-ODBC bridge. Microsoft provides an ODBC driver to Excel worksheet.
Define an ODBC datasource (system DSN) named "employee_xls" that points to that worksheet.
example 1
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;

public class EmployeeReader{
   public static final String DRIVER_NAME =
          "sun.jdbc.odbc.JdbcOdbcDriver";
   public static final String DATABASE_URL = "jdbc:odbc:employee_xls";

   public static void main(String[] args)
      throws ClassNotFoundException, SQLException{
      Class.forName(DRIVER_NAME);
      Connection con = null;
      try {
         con = DriverManager.getConnection(DATABASE_URL);
         Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery
            ("select lastname, firstname, id from [Sheet1$]");
         while (rs.next()) {
            String lname = rs.getString(1);
            String fname = rs.getString(2);
            int id = rs.getInt(3);
            
            System.out.println(fname + " " + lname + "  id : " + id);
         }
         rs.close();
         stmt.close();
      }
      finally {
         if (con != null)
            con.close();
      }
   }
}
example 2
import java.io.*;
import java.sql.*;

public class EmployeeReader{
  public static void main(String[] args){
    Connection connection = null;
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection con = DriverManager.getConnection( "jdbc:odbc:employee_xls" );
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );

      ResultSetMetaData rsmd = rs.getMetaData();
      int numberOfColumns = rsmd.getColumnCount();
                
      System.out.println ( "No of cols "+numberOfColumns  );              

      while (rs.next()) {
          for (int i = 1; i <= numberOfColumns; i++) {
            if (i > 1) System.out.print(", ");
            String columnValue = rs.getString(i);
            System.out.print(columnValue);
            }
            System.out.println(""); 
          }
      rs.close();  
      st.close();
      } 
    catch(Exception ex) {
      System.err.print("Exception: ");
      System.err.println(ex.getMessage());
      }
    finally {
      con.close();
      }
    }
 }