This will only be used for displaying.
I have come up with the following code. My program consists of the following classes: CreateConnection, GuiScreen, QueryTableModel and TestDriver.
When I try to run my application I get a null pointerException.Could some one please help me fix the problem with my application?
Any help is greatly apprenticed! I am trying to learn java and this is one of my first projects.
Any suggestions on how to better display the results of a query in a JTable?
Many Thanks!
package lab5.ids401.uic.edu;
import edu.uic.ids401.util.*;
import java.sql.*;
import java.util.*;
import java.sql.*;
public class CreateConnection {
static Connection getConnection(){
String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://131.193.211.171:3306/ids517";
String userName = "scott";
String password ="tiger";
Connection con = null;
try
{
Class.forName(driver).newInstance();
}
catch (Exception ex)
{
System.out.println("Check classpath. Cannot load database driver"+driver);
}
try
{
con = DriverManager.getConnection(url,userName,password);
}
catch (SQLException e)
{
System.out.println("Driver loaded, but cannot connect to database"+url);
}
return con;
}
}
package lab5.ids401.uic.edu;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.table.*;
public class GuiScreen extends JFrame {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 1024;
private JTextField sqlQuery;
QueryTableModel qtm;
public void display()
{
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(0,2));
inputPanel.add(new JLabel(""));
inputPanel.add(new JLabel(""));
inputPanel.add(new JLabel("SQL Query:"));
sqlQuery = new JTextField("");
sqlQuery.setBackground(Color.WHITE);
inputPanel.add(sqlQuery);
inputPanel.add(new JLabel(""));
inputPanel.add(new JLabel(""));
JButton Execute = new JButton("Execute");
inputPanel.add(Execute);
Execute.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
qtm.setQuery(sqlQuery.getText().trim());
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
JButton Exit = new JButton("Exit");
inputPanel.add(Exit);
qtm = new QueryTableModel();
JTable table = new JTable(qtm);
JScrollPane scrollpane = new JScrollPane(table);
JPanel tablePanel = new JPanel();
tablePanel.add(scrollpane);
add(inputPanel,BorderLayout.NORTH);
add(tablePanel,BorderLayout.SOUTH);
}
}
package lab5.ids401.uic.edu;
import java.awt.*;
import edu.uic.ids401.util.*;
import java.sql.*;
import java.util.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
public class QueryTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
Vector<String[]> cache;
int colCount;
String[] headers;
Connection conn;
Statement st;
public String getColumnName(int i)
{
return headers[i];
}
public int getColumnCount()
{
return colCount;
}
public int getRowCount()
{
return cache.size();
}
public Object getValueAt(int row, int col)
{
return ((String[]) cache.elementAt(row))[col];
}
public void setQuery(String sqlQuery) throws SQLException
{
cache = new Vector<String[]>();
Connection conn =CreateConnection.getConnection();
System.out.println("Got Connection");
Statement st = conn.createStatement();
st = conn.createStatement();
try
{
ResultSet rs = st.executeQuery(sqlQuery);
ResultSetMetaData meta = rs.getMetaData();
colCount = meta.getColumnCount();
///Execute the query and store the result set and its metadata
headers = new String[colCount];
for(int i=1;i<colCount;i++)
{
headers[i-1]=meta.getColumnName(i);
}
///Rebuild the headers array with the new column names
while(rs.next())
{
String[] record = new String[colCount];
for(int i=0;i<colCount;i++)
{
record[i]=rs.getString(i+1);
}
cache.addElement(record);
}
fireTableChanged(null);
}
catch(Exception e)
{
cache = new Vector<String[]>();
e.printStackTrace();
}
}
}
package lab5.ids401.uic.edu;
import java.sql.SQLException;
public class TestDriver {
public static void main(String[] args) throws SQLException {
GuiScreen screen = new GuiScreen();
screen.display();
screen.setVisible(true);
}
}
This post has been edited by mnosek3: 28 November 2011 - 06:12 PM

New Topic/Question
Reply




MultiQuote





|