Posts

Showing posts from February, 2012

Java Date - Insert into database

Image
  your date is finally saved  as "2009-12-31" So you can solve this problem in two ways: 1. Simply use setString(indexPosition, stringDatevalueInAboveFormat)   2.Convert util.Date in sql.Date as below   java . util . Date myDate = new java . util . Date ( "10/10/2009" );     PreparedStatement pstmt = connection . prepareStatement (     "INSERT INTO USERS ( USER_ID, FIRST_NAME, LAST_NAME, SEX, DATE ) " +     " values (?, ?, ?, ?, ? )" );     pstmt . setString ( 1 , userId );     pstmt . setString ( 3 , myUser . getLastName () );     pstmt . setString ( 2 , myUser . getFirtName () );     pstmt . setString ( 4 , myUser . getSex () );     pstmt . setDate ( 5 , new java . sql . Date ( myDate . getTime () ) );   As per the format question, using SimpleDateFormat will do: String s = new SimpleDateFormat ( "dd/MM/yyyy" ). ...

Why use EJB when we can do the same thing with servlets?

Image
Actually, servlets/JSPs and EJB are complementary, not competing technologies: Servlets provide support for writing web based applications whereas EJBs provide support for writing transactional objects. In larger web systems that require scalability, servlet and JSP or XML/XSL technologies provide support for the front end (UI, client) code, where EJB provides support for the back end (database connection pooling, declaritive transactions, declaritive security, standardized parameterization...) The most significant difference between a web application using only servlets and one using servlets with EJBs is that the EJB model mandates a separation between display and business logic. This is generally considered a Good Thing in non-trivial applications because it allows for internal reuse, allows flexibility by providing a separation of concerns, gives a logical separation for work, and allows the business logic to be tested separately from the UI (among others). Som...

Sockets Example

Image
Sockets Example     This example introduces you to Java socket programming. The server listens for a connection. When a connection is established by a client. The client can send data. In the current example the client sends the message "Hi my server". To terminate the connection, the client sends the message "bye". Then the server sends the message "bye" too. Finally the connection is ended and the server waits for an other connection. The two programs should be runned in the same machine. however if you want to run them in two different machines, you may simply change the adress "localhost" by the IP adress of the machine where you will run the server. The server import java.io.*; import java.net.*; public class Provider{ ServerSocket providerSocket; Socket connection = null; ObjectOutputStream out; ObjectInputStream in; String message; Provider(){} void run() { tr...

How to detect end of file when read serialized object from a file

Image
No way to find EOF, Simply store object in a List (e.g. ArrayList, HashMap etc.) then serialize it and write into a file like below.. public class User implements Serializable { String name; String pass;     public User (String name, String pass) {         this.name = name;         this.pass = pass;     }     public String getName() {         return name;     }     public String getPass() {         return pass;     }     public void display(){         System.out.println(name+":"+pass);     } } public class RecordReader {     public static void main(String as[]) throws Exception{         ObjectInputStream input=new Obj...