Database to table problem
Hi,
The basic thing I'm trying to do here is output a set of results from a SQL query to a table and I keep getting the following error:
Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException
at clsDatabase.runQuery(clsDatabase.java:46)
at frmAppointmentSearch.btnSearchActionPerformed(frmAppointmentSearch.java:404)
at frmAppointmentSearch.access$600(frmAppointmentSearch.java:12)
at frmAppointmentSearch$8.actionPerformed(frmAppointmentSearch.java:177)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
And the following is the code I'm trying to use (its a Microsoft Access database, if that's of any help):
clsDatabase sales =new clsDatabase();
try{
sales.loadDriver();
results = sales.runQuery(criteria,"search","null");
int columnID = 1;
int rowID = 1;
while(results.next() ==true)
{
tblData.setValueAt(results.getString(columnID), rowID, columnID);
columnID++;
if (Math.IEEEremainder((double)columnID, 6.0) != 0.0)
{
//If all six columns of a row have been filled, move to the next row
columnID = 1;
++rowID;
//tblData.editCellAt(rowID,columnID);
}
else
{
}
}
}catch (ClassNotFoundException ex){
ex.printStackTrace();
}catch (SQLException ex){
ex.printStackTrace();
}
Here is the code I'm using to connect my database to my program:
import java.sql.*;
import java.util.*;
publicclass clsDatabase{
Connection connection;
Statement statement;
/** Creates a new instance of clsDatabase */
public clsDatabase(){
}
publicvoid loadDriver()throws ClassNotFoundException{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:sales_database");
}
catch (ClassNotFoundException ex){
ex.printStackTrace();
}catch (SQLException ex){
ex.printStackTrace();
}
}
public ResultSet runQuery(String criteria, String type, String deleteID)
{
try{
boolean foundResults =false;
ResultSet myData;
if (type =="search")
{
if(statement.execute(criteria) !=false)
{
foundResults =true;
}
if (foundResults ==true)
{
myData = statement.getResultSet();
ResultSetMetaData info = myData.getMetaData();
return myData;
}
}
elseif (type =="deleteCustomer")
{
statement.execute("SELECT * FROM Customer WHERE CustomerID = '" + deleteID +"'");
myData = statement.getResultSet();
myData.deleteRow();
statement.execute("SELECT * FROM Customer");
myData = statement.getResultSet();
return myData;
}
elseif (type =="deleteAppointment")
{
statement.execute("SELECT * FROM Appointment WHERE AppointmentID = '" + deleteID +"'");
myData = statement.getResultSet();
myData.deleteRow();
statement.execute("SELECT * FROM Appointment");
myData = statement.getResultSet();
return myData;
}
elseif (type =="editCustomer")
{
statement.execute("SELECT * FROM Customer");
myData = statement.getResultSet();
return myData;
}
elseif (type =="editAppointment")
{
statement.execute("SELECT * FROM Appointment");
myData = statement.getResultSet();
return myData;
}
else
{
returnnull;
}
}catch (SQLException ex){
ex.printStackTrace();
}
returnnull;
}
}
Sorry for the long post, but I figured it would be best to get it all out there at once. Also, I have already linked the database in the ODBC applet within Administrative Tools under the Control Panel. Any ideas?
Thanks in advance.

