Wednesday, 13 April 2016

How to Retrieve Automatically Generated Keys in JDBC?

You can retrieve automatically generated keys (also called auto-generated keys or auto increment) from a table using JDBC 3.0 methods getGeneratedKeys(). The getGeneratedKeys()provide a standard way to make auto-generated or identity column values available to an application that is updating a database table without a requiring a query and a second round-trip to the server. SQL Server allows only a single auto increment column per table.
The ResultSet that is returned by getGeneratedKeys method will have only one column, with the returned column name of GENERATED_KEYS.
If generated keys are requested on a table that has no auto increment column, the JDBC driver will return a null result set.
When you insert rows by executeUpdate or execute an INSERT statement or an INSERT within SELECT statement, you need to indicate that you will want to retrieve automatically generated key values. You do that by setting a flag in a Connection.prepareStatement, Statement.executeUpdate, or Statement.execute method call. The statement that is executed must be an INSERT statement or an INSERT within SELECT statement. Otherwise, the JDBC driver ignores the parameter that sets the flag.

Monday, 11 April 2016

Java Basic Quiz -1

Java Basic Quiz -1

Please select atleast one option
  1. Which one is thread safe ?

  2. ArrayList
    HashTable
    HashMap

  3. How many public class created in one java file ?

  4. 1
    2
    3

  5. Which of these method of class String is used to compare two String objects for their equality? ?

  6. Equals()
    equals()
    iseEqual()

  7. Standard output variable ‘out’ is defined in which class?

  8. Process
    Runtime
    System

  9. Which of the following is correct way of implementing an interface salary by class manager?

  10. class manager implements salary {}
    class manager imports salary {}
    class manager extends salary {}

If you have any doubt mail me : jpt112233@gmail.com

Wednesday, 14 October 2015

Split string on the first white space occurence

  
If you only care about the space character (and not tabs or other whitespace characters) and only care about everything before the first space and everything after the first space, you can do it without a regular expression like this:

var str = "Hello India Jai Hind";
str.substr(0,str.indexOf(' ')); // "Hello"
str.substr(str.indexOf(' ')+1); // "India Jai Hind"

Note that if there is no space at all, then the first line will return an empty string and the second line will return the entire string. Be sure that is the behavior that you want in that situation (or that that situation will not arise).

Friday, 25 September 2015

How to clear File Input

You can just clone it and replace it with itself, with all events still attached:'

<input type="file" id="control">

and

var input = $("#control");

function something_happens() {
    input.replaceWith(input.val('').clone(true));
};

Tuesday, 11 August 2015

How System.out.println() really works

  • Commonly asked by Java beginners is the question, “How does System.out.println() work?”; 
  • Most of the relevant code can be found on OpenJDK, which is the primary implementation of Java itself. System.out.println() may be implemented completely differently on other Java platforms.

I will warn you now that this article is very long and not for the easily bored.

First steps

  • Our first step in figuring out how System.out.println works is by first understanding what System.out is and how it came to be.
  • Let’s take a look through the OpenJDK’s Mercurial online repository. Digging around a bit, we find System.java. In this file System.out is declared:

public final static PrintStream out = nullPrintStream();

But when we find the code for nullPrintStream():

private static PrintStream nullPrintStream() throwsNullPointerException{      
if (currentTimeMillis() > 0) {        
  return null;    
    }    
  throw new NullPointerException();

}


  • So nullPrintStream() simply returns null or throws an exception. This can’t be it. What’s going on here?

The answer can be found in the function initializeSystemClass(), also in System.java:

FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);

setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));

  • There’s a lot of stuff going on in this code. I’m going to refer back to this two lines of code later, but setOut0() is what actually initializes System.out.
  • The function setOut0() is a native function. We can find its implementation in System.c:
JNIEXPORT void JNICALL
Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
{
    jfieldID fid =
        (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
    if (fid == 0)
        return;
    (*env)->SetStaticObjectField(env,cla,fid,stream);
}

  • This is pretty standard JNI code that sets System.out to the argument passed to it.
  • At first all this deal with setting System.out to nullPrintStream() and later setting it with JNI seems entirely unnecessary. But this is actually justified.
  • In Java, static fields are initialized first, and everything else comes after. So even before the JVM and the System class is fully initialized, the JVM tries to initialize System.out.
  • Unfortunately at this point the rest of the JVM isn’t properly initialized so it’s impossible to reasonably set System.out at this point. The best that could be done would be to set it to null.
  • The System class, along with System.out is properly initialized in initializeSystemClass() which is called by the JVM after static and thread initialization.


There is a problem, however. System.out is final, meaning we cannot simply set it to something else in initializeSystemClass(). There’s a way around that, however. Using native code, it is possible to modify a final variable.