简体   繁体   English

获取:使用MySQL列中的JDBC输出1970年1月1日

[英]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.Timejava.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.

相关问题 Java Applet - 版本7更新51 - “信任: <url> fpApplet / FDxSDKPro.jar已结束:Thu Jan 01 01:00:00 CET 1970“ - Java Applet - version 7 update 51 - “Trust for: <url>fpApplet/FDxSDKPro.jar has ended: Thu Jan 01 01:00:00 CET 1970” 没有通过 servlet 从 jdbc 获取输出 - not getting the output from jdbc throught servlet 如何从Java中的纪元(1970-01-01)获得毫秒? - How do I get milliseconds from epoch (1970-01-01) in Java? DatePickerDialog错误地强制执行1970年1月1日的最短日期 - DatePickerDialog incorrectly enforcing a minimum date of Jan 1, 1970 得到??? output 来自 dbms_output.get_lines 使用 JDBC - Getting ??? output from dbms_output.get_lines using JDBC 使用 JDBC 从 dbms_output.get_lines 获取输出 - Getting output from dbms_output.get_lines using JDBC 如何在Java中的LocalDate数据类型中从oracle(格式“01-JAN-19”)转换Date数据类型 - How to convert Date data type from oracle ( format “ 01-JAN-19”) in LocalDate data type in Java 数据类型为日期时间的列的值在 Java 中始终为 1970/01/01 - The value of the column with data type of datetime always is 1970/01/01 in Java Java.util.Calendar - 自1970年1月1日以来的毫秒数 - Java.util.Calendar - milliseconds since Jan 1, 1970 Unix时间戳在1970年1月17日继续返回 - Unix timestamp keeps returning Jan 17 1970 in DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM