我試圖在JTable中顯示來自我的字符串數組(創建的方法)的結果 - 但它似乎不工作。JAVA-用SQL中的2d字符串數組填充JTable
代碼方法(只粘貼前兩個):
package t3.isprojekt.uppg2.dal;
import java.sql.*;
import java.util.*;
public class DAL {
private static String connStr = "jdbc:sqlserver://Localhost:1433;databases=CronusDB;user=root;password=root;";
public static Connection getConn() throws SQLException {
return DriverManager.getConnection(connStr);
}
// ------ Methods for Employee & related tables ------\\
public String[][] getEmpData() throws SQLException {
String getEmp = "SELECT [No_], [First Name], [Last Name], [Address], [City] FROM [CRONUS Sverige AB$Employee];";
Statement stmt = null;
stmt = getConn().createStatement();
ResultSet rset = stmt.executeQuery(getEmp);
ResultSetMetaData rsetMeta = rset.getMetaData();
int colCount = rsetMeta.getColumnCount();
int rowCount = 0;
while (rset.next()) {
rowCount = rset.getRow();
}
String[][] temp = convertRsToArray(rset, rowCount, colCount);
return temp;
}
public String[][] getEmployeeMetaData() throws SQLException {
String empMetaData = "SELECT TABLE_CATALOG, TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM [CronusDB].[INFORMATION_SCHEMA].[COLUMNS]";
Statement stmt = null;
stmt = getConn().createStatement();
ResultSet rset = stmt.executeQuery(empMetaData);
ResultSetMetaData rsetMeta = rset.getMetaData();
int colCount = rsetMeta.getColumnCount();
int rowCount = 0;
while (rset.next()) {
rowCount = rset.getRow();
}
String[][] temp = convertRsToArray(rset, rowCount, colCount);
return temp;
}
GUI類 - 在這裏,我不知道如何將模型連接到字符串數組。試圖谷歌,但沒有恢復到什麼,我想實現的任何信息:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import t3.isprojekt.uppg2.controller.Controller;
import t3.isprojekt.uppg2.dal.DAL;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class View extends JFrame {
private JPanel contentPane;
private Controller ctrl;
private DefaultTableModel tableModel = new DefaultTableModel(new Object[][] { ctrl.getEmployeeData() },
new Object[] { ctrl.getEmpMetaData() });
private JTable table_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
View frame = new View();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*
* @throws SQLException
*/
public View() throws SQLException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTable table = new JTable(tableModel);
table.setBounds(0, 65, 432, 188);
contentPane.add(table_2);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 247, 432, -175);
contentPane.add(scrollPane);
}
}
是的,這是一個錯字,我的壞。但是當我試圖在這個語句上運行它時,我得到了NullPointerException: ''private DefaultTableModel tableModel = new DefaultTableModel(new Object [] [] {ctrl.getEmployeeData()}, new Object [] {ctrl.getEmpMetaData()} );」 ' – user2868900
那麼一個nullpointers意味着你應該調試你的代碼。在線上做一個斷點,你instanciate你的tableModel和看起來至極參數是null。 –