Это код для моего подключения к базе данных и запросов:
Код: Выделить всё
public class UserDao {
private static Connection conn = null;
public static Connection connect() throws Exception {
if(conn == null) {
conn = (Connection) DriverManager.getConnection("jdbc:sqlite:c:/Users/ryo/database.db");
} else {
conn.close();
conn = (Connection) DriverManager.getConnection("jdbc:sqlite:c:/Users/ryo/database.db");
}
return conn;
}
private String table;
public UserDao(String table) {
this.table = table;
}
public User findUser(String username) throws SQLException {
PreparedStatement pStmt = conn.prepareStatement("SELECT * FROM " + table + " WHERE Username=?");
pStmt.setString(1, username);
try (
ResultSet rs = pStmt.executeQuery()) {
if(rs.next()) {
return new User(
rs.getString("Name"),
rs.getString("Username"),
rs.getString("Password"),
rs.getBoolean("Admin")
);
}
} catch (Exception e) {
System.out.println("ERROR FINDING USER");
e.printStackTrace();
return null;
}
return null;
}
public long addUser(User user, String password) throws SQLException {
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO " + table + " (Username, Name, Password, Admin) VALUES(?,?,?,?)");
pStmt.setString(1, user.getUsername());
pStmt.setString(2, user.getName());
pStmt.setString(3, password);
pStmt.setBoolean(4, user.isAdmin());
int rowsAdded = pStmt.executeUpdate();
long allocatedId = 0L;
if (rowsAdded == 1) {
ResultSet rs = pStmt.getGeneratedKeys();
if(rs.next()) {
allocatedId = rs.getLong(1);
}
}
return allocatedId;
}
}
Код: Выделить всё
[url=./login.html]
[/url]
[url=./signup.html]
[/url]
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/793 ... e-is-locke