0

我是 Java 数据库的初学者,谁能帮我解决这个问题。我想在我的 ms 访问文件中编辑一行,但出现“java.lang.NullPointerException”错误。谢谢

这是我的代码...

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);
    }
}
4

2 回答 2

0

请检查您是否在 con 上收到“空指针异常”,那么一定是您的数据库不可访问。请检查。或发布堆栈跟踪。

于 2013-09-27T12:02:22.077 回答
0

具有访问权限的 JDBC 类型 1 驱动程序。

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);
        }
    }
}

在http://www.java2all.com/1/4/20/107/Technology/JDBC/JDBC-example/JDBC-example-with-access上查看更多 信息

于 2013-09-27T12:01:17.303 回答