简体   繁体   中英

Java odbc with MS Access Null Pointer Exception

I'm a beginner in Java Database can anyone help me with this problem.. I want to edit a row in my ms access file but I'm getting a "java.lang.NullPointerException" Error.. Thanks

Here's my code...

public void editBook(String inputTitle, String[] newBookInfo)
{
    boolean result = false;
    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:database");
        st = con.createStatement();
        rs = st.executeQuery("select * from library");

        while(rs.next())
        {
            if(boyerMoore(rs.getString("Title"), inputTitle))
            {
                rs.updateString("ISBN", newBookInfo[0]);
                rs.updateString("Title", newBookInfo[1]);
                rs.updateString("Author", newBookInfo[2]);
                rs.updateString("Publisher", newBookInfo[3]);
                rs.updateString("Published Year", newBookInfo[4]);
                rs.updateString("Available Copies", newBookInfo[5]);
                rs.updateString("Total Copies", newBookInfo[6]);
                rs.updateRow();
                rs.close();
                st.close();
                con.close();
                JOptionPane.showMessageDialog(null, "Edit Succes", "Succes", JOptionPane.PLAIN_MESSAGE);
                result = true;
            }
        }

        if(!result)
            JOptionPane.showMessageDialog(null, "\"" + inputTitle + "\"  not Found in the Library", "Error", JOptionPane.ERROR_MESSAGE);
    }
    catch(Exception ex)
    {
        JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
    }
}

TYPE 1 driver program for JDBC with access.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Type_One 
{
    public static void main(String[] args) 
    {
        try
               {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver
            Connection con = DriverManager.getConnection("jdbc:odbc:HOD_DATA"); //Create Connection with Data Source Name : HOD_DATA
            Statement s = con.createStatement(); // Create Statement
            String query = "select * from Data"; // Create Query
            s.execute(query); // Execute Query 
            ResultSet rs = s.getResultSet(); //return the data from Statement into ResultSet
            while(rs.next()) // Retrieve data from ResultSet
            {
                System.out.print("Serial number : "+rs.getString(1)); //1st column of Table from database
                System.out.print(" , Name : "+rs.getString(2)); //2nd column of Table 
                System.out.print(" , City : "+rs.getString(3)); //3rd column of Table 
                System.out.println(" and Age : "+rs.getString(4)); //4th column of Table 
            }
            s.close();
            con.close();
        }
        catch (Exception e) 
                {
            System.out.println("Exception : "+e);
        }
    }
}

see more on http://www.java2all.com/1/4/20/107/Technology/JDBC/JDBC-example/JDBC-example-with-access

Exactly pls check if you are getting "Null Pointer Exception" on con , then it must be that your database is not accessible.Pls check that. Or post the stack trace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM