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
SQL injection can wipe your data or leak entire tables—but
PreparedStatement stops it cold. In this post, I’ll show you the only safe way to embed user input in a SQL query.
Here’s a straightforward example that takes
name and email directly as 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
name and email contain—even a malicious string like '; DROP TABLE person; --—they are treated purely as data. The INSERT statement structure remains intact; the parameters never interfere with the SQL syntax.
Match each column’s data type with the appropriate
set* method. For an INTEGER column, call setInt; for a DATE, use setDate, and so on. The official PreparedStatement documentation lists every getter and setter you’ll need. One thing to keep in mind: modern Java lets you replace the repetitive finally block with a try‑with‑resources statement, which closes connections and statements automatically.redirect message to IO stream
How can Java send regular output to the console and error messages to a file?
Java’s
PrintStream errorStream = new PrintStream(new FileOutputStream("error.log"));
System.setErr(errorStream);
// System.out remains on the console
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.
->Make variables as 'transient' which are not to be serialized.
Subscribe to:
Posts
(
Atom
)