我試圖從MySQL服務器中顯示一個表的數據到我創建的JFrame中。雖然一切工作很好,我似乎無法得到正確的相應列的時間數據。獲取:1970年1月1日使用MySQL輸出JDBC列
我的代碼,以及輸出的例子如下!
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!
我工作在Ubuntu 14.04使用MySQL Workbench和NetBeans的。
timetable_starttime和timetalbe_endtime字段都在創建表時創建TIME數據類型,只要有效的插入字段。
Here you can see the MySQL Select * statement for that table.
我想通了,那裏有事情做與時代的時間,但我沒能找到一個實際的解決問題的辦法!
任何幫助將不勝感激!
謝謝你的時間!
你可以分享你的表格定義嗎? – shmosel
顯然'timetable_starttime'和'timetalbe_endtime'數據類型應該是'TIME'而不是'DATE' – Baby
這裏是表格定義! http://i.imgur.com/XwnhjMa.jpg ...我的意思是我的主要帖子上面的時間不是日期... – Diaman