From a300758261083a2a796d9b2e6560308247c20900 Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 15 Feb 2021 01:10:27 +0000 Subject: [PATCH] Experimenting with SQLite --- Personal/src/sqlite/Main.java | 14 +++++++ Personal/src/sqlite/PlayersDB.java | 51 +++++++++++++++++++++++ Personal/src/sqlite/SQLTransactions.java | 31 ++++++++++++++ Personal/src/sqlite/database/players.db | Bin 0 -> 12288 bytes 4 files changed, 96 insertions(+) create mode 100644 Personal/src/sqlite/Main.java create mode 100644 Personal/src/sqlite/PlayersDB.java create mode 100644 Personal/src/sqlite/SQLTransactions.java create mode 100644 Personal/src/sqlite/database/players.db diff --git a/Personal/src/sqlite/Main.java b/Personal/src/sqlite/Main.java new file mode 100644 index 0000000..67dca40 --- /dev/null +++ b/Personal/src/sqlite/Main.java @@ -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(); + + } +} diff --git a/Personal/src/sqlite/PlayersDB.java b/Personal/src/sqlite/PlayersDB.java new file mode 100644 index 0000000..3a01853 --- /dev/null +++ b/Personal/src/sqlite/PlayersDB.java @@ -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; + } + +} diff --git a/Personal/src/sqlite/SQLTransactions.java b/Personal/src/sqlite/SQLTransactions.java new file mode 100644 index 0000000..acb502e --- /dev/null +++ b/Personal/src/sqlite/SQLTransactions.java @@ -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;"; +} diff --git a/Personal/src/sqlite/database/players.db b/Personal/src/sqlite/database/players.db new file mode 100644 index 0000000000000000000000000000000000000000..75a86bef7fd2e6492db37ae5c6a4c2c28fc7502a GIT binary patch literal 12288 zcmeI$&uSAv90%~(Y_i5cor;7Wa+$IeET~LochlWVF_wVZq}C)JdI}RdmEdkRCexDk zrcdD2m*@)!-aYvS;wyOb=xmb03WfI4+jn4?KfmApcJ_1I>Ah`_v|=aeG?AKlWSv+R zdCnLi#4+7A{i<1JQoLH2F4)Q+<6fR%a^y zzg}hgX~EfO)+{m_AFKK7V{HEF2l7;>i}hf2=0SZm$^Prhndh46CE6$S7yU-R(lawf zg8&2|009U<00Izz00bZa0SG|g76Qz1+-kM@==JDAj$Kg~5f^^IJ!$cl@UhkJ4wr!!ahUeFLC;PsI6dgSvc+_aZPI%%qh z>D(28x8rg1j=&T!Ma;mg