SQLite nie działa

0

Cześć, to mój kod:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLiteTest {

	private static Connection connection;
	private static boolean hasData = false;

	public ResultSet displayUsers() throws SQLException, ClassNotFoundException {
		if (connection == null) {
			getConnection();
		}

		Statement statement = connection.createStatement();
		ResultSet result = statement.executeQuery("SELECT fname, lname FROM user");
		return result;
	}

	private void getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("org.sqlite.JDBC");
		connection = DriverManager.getConnection("jdbc:sqlite:SQLite1.db");
		initialise();
	}

	private void initialise() throws SQLException {
		if (!hasData) {
			hasData = true;
		}

		Statement statement = connection.createStatement();
		ResultSet result = statement.executeQuery("SELECT name FROM sqlite master WHERE type='table' AND name='user'");
		if (!result.next()) {
			System.out.println("Building the User table with prepopulated values");
			Statement statement2 = connection.createStatement();
			statement2.execute("CREATE TABLE user(id integer, fName varchar(60), lName varchar(60), primary key(id));");
			PreparedStatement prep = connection.prepareStatement("INSERT INTO user VALUES(?,?,?);");

			prep.setString(2, "John");
			prep.setString(3, "McNeil");
			prep.execute();

			prep.setString(2, "Paul");
			prep.setString(3, "Smith");
			prep.execute();
		}
	}

	public void addUser(String firstname, String lastname) throws SQLException, ClassNotFoundException {
		if (connection == null) {
			getConnection();
		}

		PreparedStatement prep = connection.prepareStatement("Insert INTO user values(?,?,?)");
		prep.setString(2, firstname);
		prep.setString(3, lastname);
		prep.execute();
	}

}
import java.sql.ResultSet;

public class Main {

	public static void main(String[] args) {
		SQLiteTest test = new SQLiteTest();
		ResultSet rs;

		try {
			rs = test.displayUsers();
			while (rs.next()) {
				System.out.println("fname" + " " + rs.getString("lname"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Program rzuca taki błąd:
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: SQLite1)
at org.sqlite.core.DB.newSQLException(DB.java:909)
at org.sqlite.core.DB.newSQLException(DB.java:921)
at org.sqlite.core.DB.throwex(DB.java:886)
at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
at org.sqlite.core.NativeDB.prepare(NativeDB.java:127)
at org.sqlite.core.DB.prepare(DB.java:227)
at org.sqlite.jdbc3.JDBC3Statement.executeQuery(JDBC3Statement.java:81)
at SQLiteTest.initialise(SQLiteTest.java:35)
at SQLiteTest.getConnection(SQLiteTest.java:26)
at SQLiteTest.displayUsers(SQLiteTest.java:15)
at Main.main(Main.java:10)

Robiłem to z jakimś samouczkiem bo nie znam zbyt dobrze SQL, czy wiecie co zrobić, żeby program zaczął działać?

0

NIe stworzyłeś pliku SQLite1.db lub nie jest tam gdzie myślisz. Njpier zrób sobie tą bazę i pozakładaj tabele.

1 użytkowników online, w tym zalogowanych: 0, gości: 1