[英]Getting :Jan 01, 1970 output with JDBC from MySQL columns
I am trying to show the data of a Table from MySQL server, into a JFrame I am creating. 我试图将MySQL服务器中的表数据显示到我正在创建的JFrame中。 Although everything else works great i cant seem to get correct the TIME data from the corresponding columns.
虽然其他一切都很好,但我似乎无法从相应的列中获得正确的TIME数据。
My code, as well as an example of the output, follows! 我的代码以及输出示例如下!
package pkginterface;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.time.*;
public class Staff_Info extends JFrame
{
public Staff_Info()
{
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
String url = "jdbc:mysql://localhost:3306/cinema";
String userid = "root";
String password = "password";
String sql = "SELECT * FROM TimeTable";
try (Connection connection = DriverManager.getConnection( url, userid, password );
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql ))
{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++)
{
columnNames.add( md.getColumnName(i) );
}
while (rs.next())
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
row.add( rs.getObject(i) );
}
data.add( row );
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
Vector columnNamesVector = new Vector();
Vector dataVector = new Vector();
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
// Create table with database data
JTable table = new JTable(dataVector, columnNamesVector)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
pack();
}
public static void main(String[] args)
{
Staff_Info frame = new Staff_Info();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
And here you can see the above output! 在这里你可以看到上面的输出!
I am working on Ubuntu 14.04 with MySQL Workbench and NetBeans. 我正在使用MySQL Workbench和NetBeans开发Ubuntu 14.04。
Both of the timetable_starttime and timetalbe_endtime fields, have the TIME datatype in their table creation as long as a valid insert field. timetable_starttime和timetalbe_endtime字段在其表创建中都具有TIME数据类型,只要有效的插入字段。
Here you can see the MySQL Select * statement for that table. 在这里,您可以看到该表的MySQL Select *语句。
I figured out that there has something to do with Epoch time but I wasn't able to find an actual solution to the problem! 我发现与Epoch时间有关,但我无法找到问题的实际解决方案!
Any help would be greatly appreciated! 任何帮助将不胜感激!
Thanks for your time! 谢谢你的时间!
As to this documentation , the call to rs.getObject(i)
will return an instance of java.sql.Time
for your TIME
columns. 对于此文档 ,对
rs.getObject(i)
的调用将为TIME
列返回java.sql.Time
的实例。
java.sql.Time
is a subclass of java.util.Date
java.sql.Time
是java.util.Date
的子类
Unfortunately, JTable
does not have a default Renderer
in place for java.sql.Time
. 不幸的是,
JTable
没有java.sql.Time
的默认Renderer
。 But is does have one for java.util.Date
- which uses a short date format for display - defaulting to showing only the date part, which is all zero for a Time
- so it's always Januar 1st 1970 但是确实有一个用于
java.util.Date
- 它使用短日期格式进行显示 - 默认只显示日期部分,一个Time
全部为零 - 所以它总是Januar 1970年1月
So how to fix this? 那么如何解决这个问题呢?
Add a custom renderer to your JTable with a specific Renderer for Time: 使用特定的时间渲染器将自定义渲染器添加到JTable:
TableCellRenderer tableCellRenderer = new DefaultTableCellRenderer() {
SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if( value instanceof Time) {
value = f.format(value);
}
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
};
table.setDefaultRenderer(Time.class, tableCellRenderer);
Good Luck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.