我的应用程序抛出NullPointerException。我创建了MySQL数据库和我的第二个类之间的连接。
我不能通过我的第二个类中的方法调用DefaultTableModel。 我该如何解决这个问题?
public class MySQL extends javax.swing.JFrame {
private DefaultTableModel modelTabeli;
public MySQL(){
initComponents();
BazaDana bd = new BazaDana();
try{
modelTabeli = bd.map();
jTable1.setModel(modelTabeli);
}
catch(Exception e){
System.out.println(e.toString());
}
}
public static void main(String args[]) throws Exception{
BazaDana bd = new BazaDana();
bd.readDataBase();
}
}
第二课
import java.sql.*;
import javax.swing.table.DefaultTableModel;
public class BazaDana{
public DefaultTableModel map() throws SQLException
{
defaultTableModel = new DefaultTableModel();
int numberOfColumns = resultSetMetaData.getColumnCount();
while (resultSet.next())
{
Object [] rowData = new Object[numberOfColumns];
for (int i = 0; i < rowData.length; ++i)
{
rowData[i] = resultSet.getObject(i+1);
}
defaultTableModel.addRow(rowData);
}
return defaultTableModel;
}
}
答案 0 :(得分:2)
好的,如果您的目标是将GUI类与查询数据库的类分开,我可以给您一个替代解决方案。不要返回DefaultTableModel,只返回ResultSet中的值到某个集合,如下所示:
创建MySql GUI类:
import java.awt.EventQueue;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class MySql extends JFrame{
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(new Object[][]{},new String[]{"First column","Second column"});
public MySql(){
table.setModel(model);
add(new JScrollPane(table));
//Populate table
BazaDana bd = new BazaDana();
List<Value> values = bd.selectAll();
for(Value v : values){
model.addRow(new Object[]{v.getFirstValue(),v.getSecondValue()});
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run() {
MySql ms = new MySql();
ms.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
ms.pack();
ms.setVisible(true);
}});
}
}
然后创建Java bean类:
public class Value {
private int id;
private String firstValue;
private String secondValue;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstValue() {
return firstValue;
}
public void setFirstValue(String firstValue) {
this.firstValue = firstValue;
}
public String getSecondValue() {
return secondValue;
}
public void setSecondValue(String secondValue) {
this.secondValue = secondValue;
}
}
最后你的BazaDana类查询数据库:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class BazaDana {
public List<Value> selectAll(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//Create list of values
List<Value> values = new ArrayList<Value>();
try{
Class.forName("com.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nameofdb","user","pass");
st = conn.createStatement();
rs = st.executeQuery("SELECT * FROM mytable");
while(rs.next()){
Value v = new Value();
v.setFirstValue(rs.getString("first_column"));
v.setSecondValue(rs.getString("second_column"));
values.add(v);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return values;
}
}