我是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);
}
}
答案 0 :(得分:0)
具有访问权限的JDBC的TYPE 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
答案 1 :(得分:0)
确切地检查你是否在con上获得“Null Pointer Exception”,然后必须是你的数据库不可访问。请检查一下。或者发布堆栈跟踪。