Java Developer Interview

What is difference between Hashtable and Hashmap?

Hashtable is synchronized means it's therad safe, where as hashmap is not and hash table can't store null value hashmap can store null values.  A hashmap is not synchronized and faster than hashtable

In one of the field of my html form, I have to send large text(huge text) to mysql database using jdbc. So how do I store this large text using Clob/Blob in mysql.

This code portion might help you, do changes as your requirment:

Connection con=null;
       try
    {
   System.out.println("Getting inside try block");
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   String url ="jdbc:mysql://localhost:3306/databasename";
   con = DriverManager.getConnection(url,"root","rpass");
   PreparedStatement pstmt = null;
   con.setAutoCommit(false);

   File file = new File(browse10a);//browse10a is file path string variable
   if (! file.exists())
   {
    out.println(browse10a+" file not exist"+"<br>");
    throw new NullPointerException("File Specified is not Exist");
   }
   else
   {
    out.println(browse10a+" file exist"+"<br>");
    out.println("file name is: "+file.getName());
    FileInputStream fis = new FileInputStream(file);
    InputStream value = (InputStream)fis;
    PreparedStatement ps = con.prepareStatement("INSERT INTO mytable VALUES (?, ?)");
    ps.setString(1, file.getName());
    ps.setBinaryStream(2, value, (int)file.length());
    ps.executeUpdate();
    ps.close();
    fis.close();
   }
   con.commit();
       System.out.println("Completed try block successfully");
  }
  catch (Exception exp1)
  {
    out.println("Error in Transaction");
    exp1.printStackTrace();
    exp1.getMessage();
    try{con.rollback();}
    catch (Exception exp2){out.println("Can't Rollback DataBase Transaction");}
        }
        finally
        {
   try{con.setAutoCommit(true);con.close();}
   catch (Exception exp3){out.println("Can't Close DataBase Connection");}
  }//finally end

        out.flush();

//hope  this code will help you... all the best    *-- Yogesh Tiwari

Can the JDBC-ODBC Bridge be used with applets?

Use of the JDBC-ODBC bridge from an untrusted applet running in a browser, such as Netscape Navigator, isn't allowed. The JDBC-ODBC bridge doesn't allow untrusted code to call it for security reasons. This is good because it means that an untrusted applet that is downloaded by the browser can't circumvent Java security by calling ODBC. Remember that ODBC is native code, so once ODBC is called the Java programming language can't guarantee that a security violation won't occur. On the other hand, Pure Java JDBC drivers work well with applets. They are fully downloadable and do not require any client-side configuration. Finally, we would like to note that it is possible to use the JDBC-ODBC bridge with applets that will be run in appletviewer since appletviewer assumes that applets are trusted. In general, it is dangerous to turn applet security off, but it may be appropriate in certain controlled situations, such as for applets that will only be used in a secure intranet environment. Remember to exercise caution if you choose this option, and use an all-Java JDBC driver whenever possible to avoid security problems.

What is the difference between String and StringBuffer?

In String class we can't change but stringBuffer class does.  String is final object which means cannot be changed, while StringBuffer is not final object, can be changed after initalization. 

Well String is a final class so is StringBuffer class declaration is as below:

public final class StringBufferextends Object implements Serializable, CharSequence

A class being final does not imply that it is immutable or reference can not be changed. String has been implemented in such a way that every time you assign or append a String variable a new object would be created and replacing the reference to the old object. Which means that the old String object(to which reference has been lost) exists till the JVM stops or it gets garbage collected. The reason why you declare a class final is not that you want to make the objects immutable, rather when a class is made final you are sure that the class cannot be extended(sub-classed) to change the default implementation or purpose of the String type.

What is the difference between ArrayList and Vector?

Arraylist is not synchronized while vector is.  Arraylist has no default size while vector has a default size of 10. *. Arraylist don't define any increment size while vector does.  Arraylist can be seen directly without any iterator while vector requires an iterator to display all it's content. (not very sure). 

Vector is synchronized which means tread safe, while ArrayList is not..   Here I would also like to add that though ArrayList is not thread safe that does not imply that Vectors are better than ArrayList. One should prefer ArrayList over Vector, thats the general rule. Since Vector is synchronized it leads to performance degradation. If there are any thread safety issues you could synchronize that particular block of code.  One should remember that Arrays are faster than ArrayList, if performance is a concern, then it is prefereable over ArrayList and Vector.                   *-- CK

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.