JDBC - Part 4  «Prev  Next»


  1. How does one store and retrieve an object?
    A class can be serialized to a binary database field in much the same way as the image.
    You may use the code above to store and retrive an object.

  2. How do you use meta data to check a column type?

    Answer:
    Use getMetaData().getColumnType() method to check data type.
    For example to retrieve an Integer, you may check it first:
    
    int count=0;
    Connection con=getConnection();
    Statement stmt= con.createStatement();
    stmt.executeQuery("select counter from Table");
    ResultSet rs = stmt.getResultSet();
    if(rs.next()) {
    if(rs.getMetaData().getColumnType(1) == Types.INTEGER) {
    Integer i=(Integer)rs.getObject(1);
    count=i.intValue();
    }
    }
    rs.close();
    

  3. Why can't java.util.Date be used with java.sql.Date?

    Answer:
    Because java.util.Date represents both date and time.
    SQL uses three types to represent date and time.
    1. java.sql.Date -- (00/00/00)
    2. java.sql.Time -- (00:00:00)
    3. java.sql.Timestamp -- in nanoseconds
    Note that they are subclasses of java.util.Date.


  4. How do you convert the value of java.util.Date to java.sql.Date?

    Answer:
    Use the code below:
    
    Calendar currenttime=Calendar.getInstance();
    java.sql.Date startdate= new java.sql.Date((currenttime.getTime()).getTime());
    or 
    SimpleDateFormat template = new SimpleDateFormat("yyyy-MM-dd"); 
    java.util.Date enddate = new java.util.Date("10/31/99"); 
    java.sql.Date sqlDate = java.sql.Date.valueOf(template.format(enddate));