Am trying to read a date that i have stored in a database table and then parse it to a date object of the Date class using a servlet, but it returns null for the converted date.Where am i going wrong?Some advice on how to sort that out would be helpful. The servlet code is below:

Connection con;
        Statement st;
        ResultSet rs;
        Date d3 = null;
        
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:lab","admin","");
            st = con.createStatement();
            String Qry = ("SELECT * FROM Rodents;");
            rs = st.executeQuery(Qry);
            while(rs.next())
            {
                String ResID = rs.getString(1);
                String ProdID = rs.getString(2);
                String PName = rs.getString(3);
                String Price = rs.getString(4);
                
                //***********************************************
                //Chaning the date from String to an object of Date
                //***********************************************
                String ds = rs.getString(5);
                DateFormat df = DateFormat.getDateInstance();
                try 
                {
                    d3 = df.parse(ds);
                }
                catch(ParseException e) 
                {
                    System.out.println("Unable to parse");
                }
                //**********************************************
                //Printing out the data in the table
                //**********************************************
                out.print(ResID+" ");
                out.print(ProdID+" ");
                out.print(PName+" ");
                out.print(Price+" ");
                out.print(ds);
            }
            rs.close();
            st.close();
            con.close();
        }
        catch(Exception e)
        {
            System.out.print("Unable to find DB");
        }
        finally {
            out.close();
        }
    }

Just use getDate.

Yeh i did try using the getDate nethod but it just ended up producing another compile error stating that I could not the date object (on the red area).

String ds = rs.getDate(5);
                DateFormat df = DateFormat.getDateInstance();
                try 
                {
                    d3 = df.parse(ds);
                }

getDate() method returns Date object type so forcing it into String will obviously kick errors...
so you should just simply do d3 = rs.getDate(5);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.