在简单的 CRUD java 程序上成功使用 mysql 数据库后,我尝试使用 SQLite 使用此驱动程序:http://cdn.bitbucket.org/xerial/sqlite-jdbc/downloads/sqlite-jdbc-3.7.2.jar我收到此错误:
java.sql.SQLException: SQLite only supports TYPE_FORWARD_ONLY cursorsException in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tes.MainForm.Tampil(MainForm.java:31)
mainform.java:
private void Tampil(){
try{
int row = tabel.getRowCount();
for(int i=0;i<row;i++){
tabeldata.delete(0, row);
}
String sql;
sql = "SELECT * from asu";
ResultSet rs = c.script.executeQuery(sql); // line 31
while(rs.next()){
DataPegawai d = new DataPegawai();
d.setNo(rs.getInt("no"));
d.setNip(rs.getInt("nip"));
d.setNama(rs.getString("nama"));
d.setDivisi(rs.getString("divisi"));
d.setLevel(rs.getInt("level"));
tabeldata.add(d);
}
}catch(SQLException e){
System.err.print(e);
}
}
public MainForm() {
initComponents();
c = new DBConnection();
tabeldata = new TabelData();
tabel.setModel(tabeldata);
Tampil();
}
DBconnection.java:
public class DBConnection {
Connection c = null;
Statement script;
public DBConnection(){
try{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:C:/titit2.db");
script = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
System.out.println("Koneksi Sukses");
}catch( SQLException | ClassNotFoundException ex){
System.err.print(ex);
}
}
}
我想我只使用 rs(next),我错过了什么吗?
最佳答案
在DBConnection
中,您不应指定ResultSet.TYPE_SCROLL_INSENSITIVE
,而应指定ResultSet.TYPE_FORWARD_ONLY
,因为SQLite不支持滚动不敏感(或滚动敏感)。
问题不是出现在第 31 行,而是出现在 DBConnection
中,您忽略SQLException
,然后继续使用script
字段为 null
。然后,这会在第 31 行触发 NullPointerException
。
这并不是立即清楚的,因为 1) 您忽略 SQLException
和 2) 使用不添加换行符的 System.out.print
。相反,您应该使用 printStackTrace()
。
关于java - 使用 rs(next) 但在 SQLite 中得到 TYPE_FOWARD_ONLY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066635/