私はDatabase
とControl
クラスを持っています。 Database class
は、SQLiteデータベースを作成し、Dataなどを挿入するためのメソッドを提供することにより、SQLiteデータベースへのアクセスを容易にします。これらのメソッドは、すべてのメソッドで処理されるSqlJetExceptionをスローしている可能性があります。キャッチされたSqlJetExceptionを持つメソッドは、実行後にSqlJetExceptionをスローします
Control class
はDatabase class
からこれらのメソッドを呼び出していますが、SqlJetExceptionがキャッチされていないと訴えています。私は個人的には、オブジェクト指向の悪い設計としてControl class
のSqlJetExceptionの処理を見積もっているので、私はそれを避けたいと思います。
私の質問なぜ私のデータベースメソッドは例外をスローしますか?そして私はそれをどのように修正できますか?
エラーログ
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ voc ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /home/toogley/src/voc/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[14,23] error: unreported exception SqlJetException; must be caught or declared to be thrown
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[15,25] error: unreported exception SqlJetException; must be caught or declared to be thrown
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[16,21] error: unreported exception SqlJetException; must be caught or declared to be thrown
[ERROR] /home/toogley/src/voc/src/main/java/voc/Control.java:[20,21] error: unreported exception SqlJetException; must be caught or declared to be thrown
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
データベースクラス:
package voc;
import org.tmatesoft.sqljet.core.*;
import org.tmatesoft.sqljet.core.table.ISqlJetTable;
import org.tmatesoft.sqljet.core.table.SqlJetDb;
import java.io.File;
public class Database {
private SqlJetDb db;
public void createNew() throws SqlJetException {
File dbFile = new File("db.sqlite");
dbFile.delete();
try {
db = SqlJetDb.open(dbFile, true);
db.getOptions().setAutovacuum(true);
db.beginTransaction(SqlJetTransactionMode.WRITE);
db.getOptions().setUserVersion(1);
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
}
public void createTable() throws SqlJetException {
try {
db.beginTransaction(SqlJetTransactionMode.WRITE);
db.createTable("voc");
db.createIndex("english-term");
db.createIndex("german-translation");
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
}
public void setData(String englishVoc, String germanTranslation) throws SqlJetException {
try {
db.beginTransaction(SqlJetTransactionMode.WRITE);
ISqlJetTable table = db.getTable("voc");
table.insert(englishVoc, germanTranslation);
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
}
public String getData(String s) throws SqlJetException {
String returnVal = "";
try {
db.beginTransaction(SqlJetTransactionMode.READ_ONLY);
ISqlJetTable table = db.getTable("voc");
returnVal = table.lookup(s).toString();
} catch (SqlJetException e) {
e.printStackTrace();
} finally {
db.commit();
}
return returnVal;
}
コントロールクラス
package voc;
public class Control {
private Database theDB;
public Control() {
theDB = new Database();
}
public void fillDB() {
theDB.createNew(); // <<< SqlJetException
theDB.createTable(); // <<< SqlJetException
theDB.setData("mobility", "Ortsunabhängigkeit"); // <<< SqlJetException
}
public void receiveData() {
theDB.getData("test"); // <<< SqlJetException
}
}