· 6 years ago · Jun 02, 2019, 11:38 AM
1package me.harry0198.clanunion.database;
2
3import java.sql.*;
4
5public class DbHelp {
6
7
8 public static void setup(Database db) throws SQLException {
9 createTable(db);
10 prepareStatement(
11 "INSERT INTO CLANS(ClanName, Master, Motd, Prefix, Tier) " + "VALUES ('Test1', 'me', 'motd', 'hia', 1);", db.getConnection())
12 .executeUpdate();
13 }
14
15 /**
16 * Creates the database table 'CLANS'. ClanName, Master, Motd(Null), Prefix(Null), Tier,
17 *
18 * @throws SQLException If the connection is invalid.
19 */
20 public static void createTable(Database db) throws SQLException {
21 Statement st = db.getConnection().createStatement();
22 String createTable;
23 createTable = "CREATE TABLE IF NOT EXISTS CLANS (ClanName VARCHAR(15) NOT NULL, Master VARCHAR(36) NOT NULL, Motd varchar(40) NULL, Prefix varchar(4) NULL, Tier int(2) );";
24 st.execute(createTable);
25 }
26
27 public static PreparedStatement prepareStatement(String query, Connection connection) {
28 PreparedStatement ps = null;
29 try {
30 ps = connection.prepareStatement(query);
31 } catch (SQLException e) {
32 e.printStackTrace();
33 }
34 return ps;
35 }
36
37 public static void getClans(Connection connection) throws SQLException {
38
39 ResultSet rs = prepareStatement("SELECT * ClanName FROM CLANS;", connection).executeQuery();
40 rs.next();
41 System.out.println("OKAY");
42 System.out.println(rs.getString("ClanName"));
43 System.out.println("okay");
44 }
45}