· 9 years ago · Jan 26, 2017, 11:24 PM
1package Connections;
2
3import java.io.File;
4import java.io.IOException;
5import java.sql.Connection;
6import java.sql.DriverManager;
7import java.sql.SQLException;
8import java.sql.Statement;
9import java.util.logging.Level;
10
11import Connections.Database; // import the database class.
12import DataLinkMain.MainClass; // import your main class
13
14
15public class SQLLite extends Database{
16
17 String dbname;
18
19 public SQLLite(MainClass instance){
20 super(instance);
21 dbname = plugin.getConfig().getString("SQLite.Filename", "Accounts_Table"); // Set the table name here e.g player_kills
22 }
23
24 public String SQLiteCreateTable = "CREATE TABLE IF NOT EXISTS Accounts_Table (" +
25 "`player` varchar(32) NOT NULL," +
26 "`playerid` int(11) NOT NULL," +
27 "`Logins` int(11) NOT NULL," +
28 "`Kills` int(11) NOT NULL," +
29 "`Deaths` int(11) NOT NULL," +
30 "`Hover` int(11) NOT NULL," +
31 "`Total_Play_Time` int(11) NOT NULL," +
32 "`Referals` int(11) NOT NULL," +
33 "`ReferedBy` varychat(32) NOT NULL," +
34 "PRIMARY KEY (`player`)" +
35 ");";
36
37
38 public Connection getSQLConnection() {
39 File dataFolder = new File(plugin.getDataFolder(), dbname + ".db");
40 if (!dataFolder.exists()) {
41 try {
42 dataFolder.createNewFile();
43 } catch (IOException e) {
44 plugin.getLogger().log(Level.SEVERE, "File write error: " + dbname + ".db");
45 }
46 }
47 try {
48 if (connection != null && !connection.isClosed()) {
49 return connection;
50 }
51 Class.forName("org.sqlite.JDBC");
52 connection = DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
53 return connection;
54 } catch (SQLException ex) {
55 plugin.getLogger().log(Level.SEVERE, "SQLite exception on initialize", ex);
56 } catch (ClassNotFoundException ex) {
57 plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library. Google it. Put it in /lib folder.");
58 }
59 return null;
60 }
61
62
63 public void load() {
64 connection = getSQLConnection();
65 try {
66 Statement s = connection.createStatement();
67 s.executeUpdate(SQLiteCreateTable);
68 s.close();
69 } catch (SQLException e) {
70 e.printStackTrace();
71 }
72 initialize();
73 }
74
75}