我正在使用命令从 SQLite 数据库读取到 ResultSetallCustomers = stmt.executeQuery("SELECT * FROM customers");
该表customers
有 3 列:customerID
、firstname
,lastname
其中 customerID 是 0 到 199 之间的整数。
在我的代码中做了一些其他的事情之后,我来到了这一行:
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 中没有第二列,为什么我得到“200”作为 allCustomers.getInt(1) 的结果
这是我的整个代码:(
注意:由于某些原因,完全相同的事情在第一个任务中工作正常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();
}
}
}