0

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();
        }
    }
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by peter_budo
0

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);
                }
1

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);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.