0

I have two excel databases connected using two different connection variables con1 and con2.I want to retrieve data from my second database by matching the columns in both the databases.How can I do this in java?I have tried this much. I am getting NullPointerException when I run this code.

try
{
    sql10 = "select [Data$].Species,[Plot$].District,[Plot$].State from [Plot$],[Data$] where [Data$].Plot_ID = ? ";
    pst10=con.prepareStatement(sql10);
    pst10.setString(1, tmp1);
    rs14=pst10.executeQuery();

    while(rs14.next())
    {
        String st=rs14.getString("State");
        String dis=rs14.getString("District");
        String sp=rs14.getString("Species");
        System.out.println(sp);
        sql11="select * from [database$] where State = st  and District = dis and Species = sp";
        pst11=con1.prepareStatement(sql11); 
        pst11.setString(1, tmp1);
        rs15=pst11.executeQuery();

        while(rs15.next())
        {
            System.out.println(rs15.getString("Local_equation"));
        }
    }

Stack trace is:

java.lang.NullPointerException
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.clearParameter(JdbcOdbcPreparedStatement.java:1022)
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setChar(JdbcOdbcPreparedStatement.java:3056)
    at sun.jdbc.odbc.JdbcOdbcPreparedStatement.setString(JdbcOdbcPreparedStatement.java:765)
    at biomasscal.GLA14.buttonCALCULATEActionPerformed(GLA14.java:599)
    at biomasscal.GLA14.access$400(GLA14.java:60)
    at biomasscal.GLA14$5.actionPerformed(GLA14.java:279)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
1
  • @LiamWilson94-Thanks for your help.Thanku so much. It worked. Commented Apr 2, 2015 at 16:52

1 Answer 1

1

When you query your second excel database you need to pass the variables from the first query in as parameters (as you did previously):

 sql11="select * from [database$] where State = ? and District = ? and Species = ?";
     pst11=con1.prepareStatement(sql11); 
     pst11.setString(1, st);
     pst11.setString(2, dis);
     pst11.setString(3, sp);
     rs15=pst11.executeQuery();

However, with out seeing the full stack trace, I cannot be sure.

Sign up to request clarification or add additional context in comments.

1 Comment

@LiamWilson94-This query on two result sets is displaying values that match with each other. Now if I want to display the values that do not match with each other what query should I use?Like if I want to display the value of species then what should I do?can you please help me out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.