Thursday 23 February 2012

How to convert a string array to arraylist??

ans : If the concrete implementation doesn't matter, you can use Arrays.asList() to convert your array to a List.

exp:
        import java.util.Arrays;
        import java.util.List;
        import java.util.ArrayList;
        public class StringArrayTest {
           public static void main(String[] args) {
              String[] words = {"ace", "boom", "crew", "dog", "eon"};    
              List<String> wordList = Arrays.asList(words);    
               for (String e : wordList)  {          
                      System.out.println(e);
                }    
            }
        }

expla:-
       
 There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.

Wednesday 22 February 2012

How to execute stored procedures in java?

Answer 
      Here is an example on how to execute a stored procedure with JDBC (to use this in a servlet is the same the only thing is that you create the connection and callable statement in the init() of the servlet):

package DBTest;
import java.sql.*;

public class JdbcTest {
private String mysql_url = "jdbc:mysql://localhost:3306/Sample";
private String mysql_driver = "com.mysql.jdbc.Driver";
private Connection connection = null;;
private CallableStatement msProcedure = null;

public JdbcTest() {
try {
Class.forName(mysql_driver).newInstance();
connection = DriverManager.getConnection( mysql_url, "root", "root" );
msProcedure = connection.prepareCall(
"{? = call sp_sav_Bom_Header( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) }"
);
msProcedure.registerOutParameter( 1, java.sql.Types.VARCHAR );
msProcedure.setInt( 2, -1 );
msProcedure.setInt( 3, 39 );
msProcedure.setString( 4, "format" );
long ltTest = new java.util.Date().getTime();
System.out.println( "Today: " + ltTest );
msProcedure.setTimestamp( 5, new Timestamp( ltTest ) );
msProcedure.setString( 6, "type" );
msProcedure.setString( 7, "submitter" );
msProcedure.setString( 8, "email" );
msProcedure.setString( 9, "phone" );
msProcedure.setString( 10, "comments" );
msProcedure.setString( 11, "label" );
msProcedure.setInt( 12, 52 );
msProcedure.setBoolean( 13, true );
msProcedure.setBoolean( 14, false );
msProcedure.setInt( 15, 53 );
msProcedure.setString( 16, "runtime" );
msProcedure.setString( 17, "configuration" );
msProcedure.setBoolean( 18, true );
msProcedure.setBoolean( 19, false );
msProcedure.setString( 20, "special instructions" );
msProcedure.setInt( 21, 54 );

ResultSet lrsReturn = null;
System.out.println( "Execute: " + (lrsReturn = msProcedure.executeQuery() ) );
while( lrsReturn.next() ) {
System.out.println( "Got from result set: " + lrsReturn.getInt( 1 ) );
}
System.out.println( "Got from stored procedure: " + msProcedure.getString( 1 ) );
} catch( Throwable e ) {
e.printStackTrace();
} finally {
if (null != connection)
connection.close();
}
}

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