Experimenting with SQLite
parent
f4adbe6bca
commit
a300758261
@ -0,0 +1,14 @@
|
||||
package sqlite;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws SQLException {
|
||||
PlayersDB playersDatabase = new PlayersDB();
|
||||
|
||||
playersDatabase.getAllPlayers();
|
||||
playersDatabase.insertPlayer("Jizan#21290", "2450", "3350", "3400", "280");
|
||||
playersDatabase.getAllPlayers();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package sqlite;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
import static sqlite.SQLTransactions.*;
|
||||
|
||||
public class PlayersDB {
|
||||
|
||||
private Connection connection;
|
||||
private final String currentDatabase = "players.db";
|
||||
|
||||
public PlayersDB() throws SQLException {
|
||||
this.connect().createStatement().execute(CREATE_PLAYERS_TABLE);
|
||||
this.connect().createStatement().executeUpdate(POPULATE_PLAYERS_TABLE);
|
||||
}
|
||||
|
||||
public void getAllPlayers() throws SQLException {
|
||||
ResultSet resultSet = connect().createStatement().executeQuery(GET_ALL_PLAYERS);
|
||||
|
||||
while (resultSet.next()) {
|
||||
System.out.println(
|
||||
"\nPlayer Name: " + resultSet.getString("battlenet")
|
||||
+ "\nLevel: " + resultSet.getString("level")
|
||||
+ "\nDamage SR: " + resultSet.getString("damageSR")
|
||||
+ "\nTank SR: " + resultSet.getString("tankSR")
|
||||
+ "\nSupport SR: " + resultSet.getString("supportSR")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public void insertPlayer(String battlenet, String damageSR, String tankSR, String supportSR, String level) throws SQLException {
|
||||
PreparedStatement preparedStatement = connect().prepareStatement(INSERT_PLAYER);
|
||||
|
||||
preparedStatement.setString(1, battlenet);
|
||||
preparedStatement.setString(2, damageSR);
|
||||
preparedStatement.setString(3, tankSR);
|
||||
preparedStatement.setString(4, supportSR);
|
||||
preparedStatement.setString(5, level);
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
private Connection connect() throws SQLException {
|
||||
if (connection == null) {
|
||||
connection = DriverManager.getConnection(BASE_JDBC_URL + currentDatabase);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package sqlite;
|
||||
|
||||
public class SQLTransactions {
|
||||
|
||||
static final String BASE_JDBC_URL = "jdbc:sqlite:./Personal/src/sqlite/database/";
|
||||
|
||||
static final String CREATE_PLAYERS_TABLE = """
|
||||
CREATE TABLE IF NOT EXISTS players
|
||||
(
|
||||
battlenet text PRIMARY KEY,
|
||||
damageSR text NOT NULL,
|
||||
tankSR text NOT NULL,
|
||||
supportSR text NOT NULL,
|
||||
level text NOT NULL
|
||||
);
|
||||
""";
|
||||
|
||||
static final String POPULATE_PLAYERS_TABLE = """
|
||||
BEGIN TRANSACTION;
|
||||
INSERT INTO players VALUES ('Hammy#21436', '3000', '2650', '3520', '400') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO players VALUES ('Mootbox#2537', '3050', '3500', '4000', '500') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO players VALUES ('Carlos#24356', '2250', '1800', '1940', '98') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO players VALUES ('SmolMio#2844', '3499', '2679', '3000', '428') ON CONFLICT DO NOTHING;
|
||||
INSERT INTO players VALUES ('Jizan#21290', '2450', '3350', '3400', '280') ON CONFLICT DO NOTHING;
|
||||
COMMIT;
|
||||
""";
|
||||
|
||||
static final String GET_ALL_PLAYERS = "SELECT * FROM players;";
|
||||
|
||||
static final String INSERT_PLAYER = "INSERT INTO players VALUES(?, ?, ?, ?, ?) ON CONFLICT DO NOTHING;";
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue