Java Interview Questions On JDBC Conectivity |
29. How to store and retrive an object?
Answer
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
|
30. How to 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 aTable");
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(); |
31. Why cannot java.util.Date match with java.sql.Date?
Answer
Because java.util.Date represents both date and time. SQL has three types to represent date and time.
- java.sql.Date -- (00/00/00)
- java.sql.Time -- (00:00:00)
- java.sql.Timestamp -- in nanoseconds
Note that they are subclasses of java.util.Date. |
32. How to convert java.util.Date value 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)); |