• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details

JDBC connectivity with MS Access DB

iamharish15

Broken In
I am working on a project in which I need to store the information about the users into a Database and based on the querry I need to take the results from the databse and display the results retreived from database in a frame. But I could not put the retrieved information from the database into the frame and display it.
I am using J2SE JDK 1.6 and Netbeans 6.9.1 and MS Access(MS Office 2010 EE).
So please suggest the code or any other solution to it.
 
OP
iamharish15

iamharish15

Broken In
this is not the problem i had. I had successfully connected with the access db using jdbc-odbc bridge but i am having trouble retrieving results from that database and display it on the frame or any output device.
for example the project i am working on requires me to take some specific tuples from the DB based on input criteria by the user(viz. firstname) and put it on a frame and displaying it.
Below is a code snippet i am having trouble with:
private void PhoneDisplayActionPerformed(java.awt.event.ActionEvent evt) {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:eek:dbc:pcm"); //pcm is the dsn
PreparedStatement st = con.prepareStatement("select firstname,lastname,email1,city,state,country,mobile1,home from ContactsInfo"
+ "where firstname=?%");
st.setString(1,jComboBox7.getSelectedItem().toString());
JOptionPane.showMessageDialog(PhoneDisplay,"Retrieving Results....");
ResultSet rs = st.executeQuery();
int c = rs.getMetaData().getColumnCount();
rs.last();
int r = rs.getRow();
Object[][] data = new Object[r][c];


rs.first();
for(int i=0;i<r;i++)
{
data[0] = rs.getInt(1);
data[1] = rs.getString(2);
data[2] = rs.getString(3);
data[3] = rs.getString(4);
data[4] = rs.getString(5);
data[5] = rs.getString(6);
data[6] = rs.getString(7);
data[7] = rs.getString(8);
rs.next();
}

Object[] col = {"First Name","Last Name","Email1","City","State","Country","Mobile","Home No."};
JTable t = new JTable(data,col);
QuerryFrame qf = new QuerryFrame();
qf.add(t);
qf.setSize(500, 600);
qf.setVisible(true);
con.setAutoCommit(true);
con.close();

}
catch (ClassNotFoundException ex) {
//Logger.getLogger(SearchPhone.class.getName()).log(Level.SEVERE, null, ex);
}
catch(SQLException se)
{
//JOptionPane.showMessageDialog(PhoneDisplay,"SQL Exception");
}
 
OP
iamharish15

iamharish15

Broken In
When I compiled the above code, it showed no errors but shows a run time exception.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.clearParameter(JdbcOdbcPreparedStatement.java:1023)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setChar(JdbcOdbcPreparedStatement.java:3057)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(JdbcOdbcPreparedStatement.java:766)
at personalcontactmanagement.SearchPhone.PhoneDisplayActionPerformed(SearchPhone.java:187)
at personalcontactmanagement.SearchPhone.access$100(SearchPhone.java:22)
at personalcontactmanagement.SearchPhone$2.actionPerformed(SearchPhone.java:71)
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.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
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:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
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)
When I run the static SQL select command through Statement object, it runs perfectly but when I try to take the parameter at runtime through PreparedStatement object, it shows the above exception.
Please suggest some solution for the above exception.
 
OP
iamharish15

iamharish15

Broken In
yes I have tried that it is working correctly.
static SQL commands are running good but the dynamic SQL commands with PreparedStatement object is having the trouble...
 
Top Bottom