コマンドを使用してSQLiteデータベースからResultSetに読み込んでいますallCustomers = stmt.executeQuery("SELECT * FROM customers");
テーブルには 、 の3つのcustomers
列があり、
customerID は 0 ~ 199 の整数です。customerID
firstname
lastname
私のコードで他のことをした後、私は次の行に行きます:
if(alleKunden.next()){
System.out.println( allCustomers.getInt(1) + " " + allCustomers.getString(2));
}
この行でcolumn 2 out of bounds [1,1]
エラーが発生します。allCustomers.getString(2)
出力をドロップ
する200
と、次の反復で (これは for ループで発生するため)、例外ResultSet closed
によってプログラムが終了します。
Firefox プラグイン SQLite でデータベースを確認したところ、データベースにすべての顧客情報が正しく含まれていることがわかりました。
なぜ ResultSet に 2 番目の列がなく、allCustomers.getInt(1) の結果として「200」を取得するのはなぜですか?
ここに私のコード全体があります:(
注:いくつかの理由で、まったく同じことが最初のタスクでうまく機能しますcreateCustomers()
)
package telefonbook;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class SqlDB {
private static final SqlDB dbcontroller = new SqlDB(Application.VERBOSE_MODE, Application.CREATE_NEW_DATA, Application.FAKTOR, Application.DB_PATH);
private static Connection connection;
private static final String DB_PATH = System.getProperty("user.home") + "/" + "tmp/telefonbook.sqlite";
static boolean verboseMode;
static boolean createNewData;
static int faktor;
static String dbPath;
static {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
System.err.println("Error with the JDBC driver");
e.printStackTrace();
}
}
public SqlDB(boolean verboseMode, boolean createNewData, int faktor, String dbPath){
this.verboseMode = verboseMode;
this.createNewData = createNewData;
this.faktor = faktor;
this.dbPath = dbPath;
}
public static SqlDB getInstance(){
return dbcontroller;
}
private void initDBConnection() {
try {
if (connection != null)
return;
System.out.println("Creating Connection to Database...");
connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH);
if (!connection.isClosed())
System.out.println("...Connection established");
} catch (SQLException e) {
throw new RuntimeException(e);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
if (!connection.isClosed() && connection != null) {
connection.close();
if (connection.isClosed())
System.out.println("Connection to Database closed");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
private void createCustomers(int customerAmount) {
try {
System.out.print("Creating customers...\n ");
Statement stmt = connection.createStatement();
stmt.executeUpdate("DROP TABLE IF EXISTS customer;");
stmt.executeUpdate("CREATE TABLE customer (customerID, lastname, firstname);");
String firstname;
String lastname;
for (int i = 0; i < customerAmount; i++){
//here a random name is generated. Gonna skip this code
firstname = randomname();
lastname = randomname();
stmt.execute("INSERT INTO customer (customerID, lastname, firstname) VALUES (" + i + ",'" + lastname + "','" + firstname + "')");
}
connection.commit();
if(verboseMode){
ResultSet allCustomers = stmt.executeQuery("SELECT * FROM customer");
while(allCustomers.next()){
System.out.println(allCustomers.getString(1) + "\t" + allCustomers.getString(2) + "\t" +allCustomers.getString(3));
}
}
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
}
}
private void createTelefone(int telefonAmount) {
try {
String telefonnumber;
System.out.print("Creating telefones...\n ");
Statement stmt = connection.createStatement();
stmt.executeUpdate("DROP TABLE IF EXISTS telefone;");
stmt.execute("CREATE TABLE telefone (telefonID, telefonnumber, ownerID);");
ResultSet allCustomers = stmt.executeQuery("SELECT * FROM customer");
int customerAmount = stmt.executeQuery("SELECT COUNT(*) FROM customer").getInt(1);
for (int i = 0; i < telefonAmount; i++){
telefonnumber = randomNumber();
if(allCustomers.next()){
// This line will fail!
System.out.println(allCustomers.getInt(1) + " " + allCustomers.getString(2));
stmt.execute("INSERT INTO telefone (telefonID, telefonnumber, ownerID) VALUES (" + i + ",'" + telefonnumber + "', " + allCustomers.getInt(1) + ")");
} else {
//do something else
}
}
ResultSet allTelefones = stmt.executeQuery("SELECT * FROM telefone");
if(verboseMode){
while(allTelefones.next()){
System.out.println(allTelefones.getString(1) + "\t" + allTelefones.getString(2) + "\t" +allTelefones.getString(3));
}
}
} catch (SQLException e) {
System.err.println("Couldn't handle DB-Query");
e.printStackTrace();
}
}
public static void Application() {
int customerAmount = 100*faktor;
int telefonAmount = 100*faktor;
SqlDB dbc = SqlDB.getInstance();
dbc.initDBConnection();
try{
connection.setAutoCommit(false);
if (createNewData){
dbc.createCustomers(customerAmount);
dbc.createTelefone(telefonAmount);
}
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}