如何从sql数据库获取日期数据类型到java?

时间:2020-11-26 16:05:51

How to get the date datattype from sql database to java???so far I have use this code but failed to fetch the date.

如何将日期数据类型从sql数据库获取到java?到目前为止,我已经使用了这段代码,但未能获取日期。

import java.sql.*;//this is my import file     String search="select * from tblflight";//select all data from my table     ResultSet rs=stmt.executeQuery(search);//puting result in temporary var     while(rs.next()){   Date d_time=rs.getDate("date_time");//getting result   combo_box.addItem(d_time);//n showing error here while putting the date var here}

2 个解决方案

#1


1  

You have two options.
1 - Take sql date object and construct utils date using the getTime method of sql date
or
2 - call getTimeStamp instead of getDate , It will return object of sql timeStamp class that is actually child class of java.util.Date

你有两个选择。 1 - 使用sql日期对象并使用sql date或2的getTime方法构造utils日期 - 调用getTimeStamp而不是getDate,它将返回sql timeStamp类的对象,它实际上是java.util.Date的子类

1 -  Date d1 = new Date(rs.getDate("date_time").getTime());2 -  Date d2 = rs.getTimestamp("date_time");

#2


0  

If combo box is not allowing object to inserted, while adding it to the combo_box use d_time.toString() method.

如果组合框不允许插入对象,则在将其添加到combo_box时使用d_time.toString()方法。

#1


1  

You have two options.
1 - Take sql date object and construct utils date using the getTime method of sql date
or
2 - call getTimeStamp instead of getDate , It will return object of sql timeStamp class that is actually child class of java.util.Date

你有两个选择。 1 - 使用sql日期对象并使用sql date或2的getTime方法构造utils日期 - 调用getTimeStamp而不是getDate,它将返回sql timeStamp类的对象,它实际上是java.util.Date的子类

1 -  Date d1 = new Date(rs.getDate("date_time").getTime());2 -  Date d2 = rs.getTimestamp("date_time");

#2


0  

If combo box is not allowing object to inserted, while adding it to the combo_box use d_time.toString() method.

如果组合框不允许插入对象,则在将其添加到combo_box时使用d_time.toString()方法。