i am trying to develop a jtable based JDBC program that creates a table relating to the table present inside database.i have created 2 methods as databaseHandler() and graphicsHandler() to handle database and graphics related instructions.The coldata and rowdata contain column names and the resultSet rowdata.But when using these values to draw the table,i'm facing a NullPointerException but i cant understand why.Any help from anyone is most appreciated.Thank you.
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Final {
int columnno,rowno;
Connection connection;
PreparedStatement preparedStatement;
ResultSet resultSet;
ResultSetMetaData metaData;
Vector<String> coldata;
Vector<Object> rowdata,temp;
DefaultTableModel tableModel;
JScrollPane scrollPane;
JTable table;
public Final() {
// TODO Auto-generated constructor stub
ValueHolder holder = DatabaseHandler();
GraphicsHandler(holder.getCd(),holder.getRd());
}
private void GraphicsHandler(Vector<String> coldata, Vector<Object> rowdata) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout(0, 20)); //setting up horizontal and vertical gap
scrollPane.setViewportView(table);
scrollPane.setWheelScrollingEnabled(true);
frame.add(scrollPane, BorderLayout.CENTER);
tableModel = new DefaultTableModel(rowdata,coldata);
table = new JTable(tableModel);
frame.setVisible(true);
frame.pack();
}
private ValueHolder DatabaseHandler() {
// TODO Auto-generated method stub
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/company?user=username&password=password";
try {
Class.forName(driver);
connection = DriverManager.getConnection(url);
preparedStatement = connection.prepareStatement("select * from product");
resultSet = preparedStatement.executeQuery();
metaData = resultSet.getMetaData();
columnno = metaData.getColumnCount();
rowno = 1;
coldata = new Vector<String>();
for (int i = 1; i <= columnno; i++) {
coldata.add(metaData.getColumnLabel(i));
}
System.out.println();
rowdata = new Vector<Object>() ; temp = new Vector<Object>();
while (resultSet.next()) {
for (int i = 1; i <= columnno; i++) {
temp.add(resultSet.getString(i));
}
rowdata.add(temp.clone());
temp.clear();
rowno++;
}
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ValueHolder(rowdata, coldata);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Final();
}
}
class ValueHolder {
Vector<Object> rd; Vector<String> cd;
public ValueHolder(Vector<Object> rowdata, Vector<String> coldata) {
this.rd = rowdata;
this.cd = coldata;
}
public Vector<Object> getRd() {
return rd;
}
public Vector<String> getCd() {
return cd;
}
}
I did it just by extending the javax.swing.table.AbstractTableModel and adding the ability to run a query against a DB. Once this works fine, you can simply use a JTable within your Swing project. Hope this helps.