我在从数据库导入数据时遇到问题,因为当我没有看到编码问题时,我收到NullPointer异常。当我使用二维数组时,我的表中的列名也不会显示,这也会在从数据库显示数据时引起问题。有问题吗?提前谢谢
//Says 'Connection class doesn't exist' yet works in another class I made called Queries
final String URL="jdbc:mysql://localhost:3306/aerogames";
final String DRIVER="com.mysql.jdbc.Driver";
final String USERNAME="root";
final String PASSWORD="";
Connection conn=null;
Statement st=null;
String query;
/
public void GameMenu(){
{
GameCard=new JPanel();
GameCard.setLayout(gridBag);
tableLabel=new JLabel("MANAGE AUTHOR");
insertTable=new JButton("Insert Game");
updateTable=new JButton("Update Game");
deleteTable=new JButton("Delete Game");
backTable=new JButton("Go to Main Menu");
backTable.addActionListener(this);
Object[] col={"Author ID","First Name","Last Name"};
DefaultTableModel model = new DefaultTableModel(data,col);
table=new JTable(model)
{
boolean[] cellEditables={false,true,true, true};
public boolean isCellEditable(int row,int col)
{
return cellEditables[col];
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scroll=new JScrollPane(table);
scroll.setPreferredSize(new Dimension(350,200));
model=new DefaultTableModel(data,col);
table.setModel(model);
// insertBtn.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent evt)
// {
// insertAuthor();
// }
// });
// updateBtn.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent evt)
// {
// updateAuthor();
// }
// });
// deleteBtn.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent evt)
// {
// deleteAuthor();
// }
// });
createTable();
updateTable();
addComponent(tableLabel,GameCard,0,0,0,0,0,0);
addComponent(scroll,GameCard,0,1,0,0,0,0);
c.gridwidth=1;
addComponent(insertTable,GameCard,0,2,0,0,0,0);
addComponent(updateTable,GameCard,0,3,0,0,0,0);
addComponent(deleteTable,GameCard,0,4,0,0,0,0);
addComponent(backTable,GameCard,0,5,0,0,0,0);
}
}
/
public void createTable()
{
model=new DefaultTableModel();
table=new JTable(model)
{
boolean[] cellEditables={false,true,true, true};
public boolean isCellEditable(int row,int col)
{
return cellEditables[col];
}
};
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scroll=new JScrollPane(table);
scroll.setPreferredSize(new Dimension(350,200));
}
public void updateTable()
{
Object[][] data=q.getAllGamesByID_OA();
Object[] colNames={"Game ID","Game Name","Game Price", "Game Stock"};
model=new DefaultTableModel(data,colNames);
table.setModel(model);
}
/空指针异常错误
public Object[][] getAllGamesByID_OA()
{
query="select * from product_details order by product_ID";
Object[][] data=null;
ResultSet rs;
try
{
rs=st.executeQuery(query);
rs.last();
int rowCount=rs.getRow();
rs.beforeFirst();
data=new Object[rowCount][4];
int i=0;
while(rs.next())
{
data[i][0]=rs.getInt("product_ID");
data[i][1]=rs.getString("product_name");
data[i][2]=rs.getInt("unit_price");
data[i][3]=rs.getInt("quantity_in_stock");
i++;
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,e.getMessage(),
"SQL Error",JOptionPane.INFORMATION_MESSAGE);
}
return data;
}