Scenarios in which Serialization cannot happen

What are the special cases in which serialization cannot happen?
-> There are following scenarios in which serialization cannot happen:
a. Variables are transient.
b. Variables are static.
c. Base class variables are serialized if class itself is serializable.

java prevent sql injection - using PreparedStatement


PreparedStatement is the best way to prevent sql injection in java, rather than escaping strings. 
Here's a simple example taking the user's input as the parameters:
public insertUser(String name, String email) {
   Connection conn = null;
   PreparedStatement stmt = null;
   try {
      conn = setupTheDatabaseConnectionSomehow();
      stmt = conn.prepareStatement("INSERT INTO person (name, email) values (?, ?)");
      stmt.setString(1, name);
      stmt.setString(2, email);
      stmt.executeUpdate();
   }
   finally {
      try {
         if (stmt != null) { stmt.close(); }
      }
      catch (Exception e) {
         // log this error
      }
      try {
         if (conn != null) { conn.close(); }
      }
      catch (Exception e) {
         // log this error
      }
   }
}
No matter what characters are in name and email, those characters will be placed directly in the database. They won't affect the INSERT statement in any way.
There are different set methods for different data types -- which one you use depends on what your database fields are. For example, if you have an INTEGER column in the database, you should use asetInt method. The PreparedStatement documentation lists all the different methods available for setting and getting data.

redirect message to IO stream

How can Java send regular output to the console and error messages to a file?
Java’s System class provides the static PrintStream fields out and err. By default both write to the console, but you can redirect them separately with setOut() and setErr(). Here’s how to send only error messages to a file while keeping standard output on the console:

PrintStream errorStream = new PrintStream(new FileOutputStream("error.log")); 
System.setErr(errorStream);
// System.out remains on the console

java socket connect read string

Java - create socket connection to HOST:PORT and read message from there
import java.io.*;
import java.net.*;
import java.util.*;

public class Time_Server_Socket_Test_Java {
    public static void main(String[] args) {
        try {
            Socket s = new Socket(HOST, PORT);//use your own HOST:PORT
            try {
                InputStream inStream = s.getInputStream();
                Scanner in = new Scanner(inStream);

                while (in.hasNextLine()) {
                    String line = in.nextLine();
                    System.out.println(line);
                }
            } finally {
                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



How to serialize variables selectively

In a Java class, one has 10 variables. One wants to serialize only 3 variables,how can this be achieved?
->Make variables as 'transient' which are not to be serialized.

maximize a JFrame window in java

How to Maximize a JFrame :
 
    JFrame myFrame = new JFrame();
    myFrame.setVisible(true);
    myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
    

The usage of Java packages.

How Java packages organize code, prevent name collisions, and enforce access control.

Java packages act as a logical folder structure for your source code—java.lang, java.util, or a custom com.mycompany.util. They group related classes and sub-packages, making a large project modular and easy to navigate. Crucially, they provide a unique namespace that prevents naming conflicts: you can have your own Date class without clashing with java.util.Date because each sits in a different package. 
Beyond organization, the default package-private access level is a simple yet effective encapsulation feature. Any field or method declared without a modifier (public, protected, private) is visible only within the exact same package. This lets you hide internal helpers and data from outside code, protecting implementation details and ensuring that only authorized classes (those in the same package) can interact with sensitive members.

Java: difference between private, protected, and public?

These keywords are for allowing privileges to components such as java methods and variables.
Public: accessible to all classes
Private: accessible only to the class to which they belong
Protected: accessible to the class to which they belong and any subclasses.

Access specifiers are keywords that determines the type of access to the member of a class. These are:
* Public
* Protected
* Private
* Defaults

java show window JFrame JDialog always on top

We have to use this feature of java.awt.Window : Window.alwaysOnTop(boolean); Example code :

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Always_on_Top_JFrame_JAVA{
    public static void main(String[] args) {
        JFrame frame = new JFrame("Title - always on top :D ");
        // Set's the window to be "always on top"
        frame.setAlwaysOnTop( true );

        frame.setLocationByPlatform( true );
        frame.add( new JLabel(" Always on TOP ") );
        frame.pack();
        frame.setVisible( true );
    }
}

Order of catching exception in java

Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?

A. Yes, it does. The FileNotFoundException is inherited from the IOException. 
Exception's subclasses have to be caught first.

Exception
^
|
IOException
^
|
FileNotFoundException 
So while catching exceptions, we must catch the low level exception first - here : FileNotFoundException .


#The hierarchy in Java Exception framework :