I am trying to show the data of a Table from MySQL server, into a JFrame I am creating. Although everything else works great i cant seem to get correct the TIME data from the corresponding columns.
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.
Both of the timetable_starttime and timetalbe_endtime fields, have the TIME datatype in their table creation as long as a valid insert field.
Here you can see the MySQL Select * statement for that table.
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!
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.
java.sql.Time
is a subclass of java.util.Date
Unfortunately, JTable
does not have a default Renderer
in place for java.sql.Time
. 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
So how to fix this?
Add a custom renderer to your JTable with a specific Renderer for Time:
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.