Hi
Many a times developers say that java util package doesn't support a geveric java tree
the api does have
A generic tree can be written as here and here
Google created its own collections framework that is being used in many of its applications as explained in videos 1 and 2. the project can be downloaded from here.
Monday, January 26, 2009
Friday, January 23, 2009
Oracle doesnt support dirty read
more details at
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.OracleStatement;
public class ConnectionClass {
public ConnectionClass() {
}
public Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@hyc65003fwks.idc.oracle.com:1521:nn11g",
"scott","tiger");
con.setAutoCommit(false);
System.out.println("isolation lvl "+con.getTransactionIsolation());
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
return con;
}
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
ConnectionClass obj = new ConnectionClass();
Connection con1 = obj.getConnection();
Connection con2 = obj.getConnection();
obj.doUpdate(con1);
obj.read(con2);
con1.rollback();
con1.close();
System.out.println("again reading.......");
obj.read(con2);
con2.close();
}
int i=0;
public void doUpdate(Connection con) throws SQLException {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
i++;
String sql = "insert into delme values('aa"+i+"')";
System.out.println("sql >>>"+sql);
System.out.println(stmt.executeUpdate(sql));
stmt.close();
}
public void read(Connection con) throws SQLException {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from delme";
System.out.println("sql >>>"+sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
}
}
/* Exception in thread "main" java.sql.SQLException: READ_COMMITTED and SERIALIZABLE are the only valid transaction levels
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:439)
at oracle.jdbc.driver.PhysicalConnection.setTransactionIsolation(PhysicalConnection.java:3988)
at jdbcproj.ConnectionClass.getConnection(ConnectionClass.java:22)
at jdbcproj.ConnectionClass.main(ConnectionClass.java:29)
Process exited with exit code 1. */
Thursday, January 22, 2009
HashMap and null keys
most of us know a hashMap allows null keys and also null values , but what if we add two values corresponding to same null key
What will be the output of
System.out.println(map)
will for the null key it will print the last key value pair , i.e. as per my understanding the null key is unique and on adding more value elements to the same null key will override the old values
map.put(null,null);
map.put(null,"D");
map.put(null,"E");
What will be the output of
System.out.println(map)
will for the null key it will print the last key value pair , i.e.
Convert HashMap to HashTable
How will you convert a HashMap having null values and/or keys to a hash table ?
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashTableHashMap
{
public static void main(String[] args) {
Map map = new HashMap();
map.put("a",null);
map.put("b","B");
map.put(null,"D");
System.out.println(map);
Hashtable ht = new Hashtable();
Set set = map.entrySet();
Iterator itr = set.iterator();
while(itr.hasNext()){
Map.Entry entry = (Map.Entry)itr.next();
Object key = entry.getKey();
Object val = entry.getValue();
if(key==null){
key = ""+null; // Or whatever you want
}
if(val==null){
val = ""+null; // Or whatever you want
}
ht.put(key,val);
}
System.out.println(ht);
}
}
Monday, January 19, 2009
Monday, January 12, 2009
Tuesday, January 6, 2009
JSF page refresh
To refresh a JSF page the following piece of code is useful
FacesContext context = FacesContext.getCurrentInstance();
String viewId = context.getViewRoot().getViewId();
ViewHandler handler = context.getApplication().getViewHandler();
UIViewRoot root = handler.createView(context, viewId);
root.setViewId(viewId);
context.setViewRoot(root);
If your page is not getting refreshed due to browser caching
FacesContext context = FacesContext.getCurrentInstance();
String viewId = context.getViewRoot().getViewId();
ViewHandler handler = context.getApplication().getViewHandler();
UIViewRoot root = handler.createView(context, viewId);
root.setViewId(viewId);
context.setViewRoot(root);
If your page is not getting refreshed due to browser caching
FacesContext facesContext = event.getFacesContext();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "must-revalidate");
Monday, January 5, 2009
Data Structures
Few good links for Data Structures
- C API : http://code.google.com/p/c-generic-library/
- google collections : http://code.google.com/p/google-collections/
- java listenable collections : http://code.google.com/p/jalico/
- JRDF : http://code.google.com/p/jrdf/
- J5Collections : http://code.google.com/p/j5collections/
- Stanford video tutorial : http://in.youtube.com/view_play_list?p=84A56BC7F4A1F852
- Book : http://www.brpreiss.com/books/opus5/html/page257.html
- all slide handout from the textbook http://ww0.java4.datastructures.net/handouts/
- comparison of link implementation in java (ArrayList, Vector, LikedList) http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.html?page=1
- time your execution program example http://www.rgagnon.com/javadetails/java-0132.html *in Java 5, you can also use nano Timing method.
- Queue in Java 5 http://www.devx.com/Java/Article/21983
- Josephus Problem http://en.wikipedia.org/wiki/Josephus_problem
- Pigeonhole principle http://en.wikipedia.org/wiki/Pigeonhole_principle
- Iterator Example http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter10/iterator.html or http://www.java-examples.com/iterate-through-elements-java-arraylist-using-iterator-example
- Tree in Java 5 http://it-essence.xs4all.nl/roller/technology/entry/three_tree_traversals_in_java
- Prime in Hash http://www.interactivecode.com/programming-coding-1/hashing-why-mod-prime-2101/
- Last 2 slides of ch9.2 page17-18ch09.2.pdf
Subscribe to:
Posts (Atom)