· 9 years ago · Aug 04, 2017, 09:36 PM
1package database;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6import java.sql.Connection;
7import java.sql.DriverManager;
8import java.sql.PreparedStatement;
9import java.sql.ResultSet;
10import java.util.ArrayList;
11
12public class main {
13
14
15 public static void main(String [] args) throws Exception {
16
17 createTable();
18 einsetzen();
19 get();
20 }
21 public static ArrayList<String> get() throws Exception {
22 try {
23 Connection con = getConnection();
24
25
26 PreparedStatement statement = con.prepareStatement("SELECT Funktion, Wert FROM config");
27
28 ResultSet result = statement.executeQuery();
29
30 ArrayList<String> array = new ArrayList<String>();
31
32 FileWriter writer;
33 File datei = new File("test.txt");
34 writer = new FileWriter(datei);
35
36
37 while(result.next()) {
38
39
40 writer.write(result.getString("Funktion"));
41 writer.write(" ");
42 writer.write(result.getString("Wert"));
43 writer.write(System.getProperty("line.separator"));
44
45 array.add(result.getString("Funktion"));
46 array.add(" ");
47 array.add(result.getString("Wert"));
48 array.add(System.getProperty("line.separator"));
49
50
51 }
52
53 writer.flush();
54 return array;
55 }catch(Exception e) {
56 System.out.println(e);
57 }
58 return null;
59 }
60 public static void einsetzen() throws Exception {
61
62 try {
63 Connection con = getConnection();
64
65
66 PreparedStatement einsetzen = con.prepareStatement("INSERT INTO config (Funktion, Wert) VALUES ('aimbot_active','1') ");
67 einsetzen.executeUpdate();
68 }catch(Exception e) {
69 System.out.println(e);
70 }
71 finally {
72 System.out.println("Default Werte eingesetzt");
73 }
74 }
75 public static void createTable() throws Exception {
76 try {
77 Connection con = getConnection();
78
79 PreparedStatement create = con.prepareStatement("CREATE TABLE IF NOT EXISTS config(Funktion VARCHAR(100),Wert INT(100), PRIMARY KEY(Funktion))");
80 create.executeUpdate();
81
82 }catch(Exception e) {
83 System.out.println(e);
84 }
85 finally {
86 System.out.println("Function executed");
87 }
88 }
89 public static Connection getConnection() throws Exception{
90
91 try {
92 String host = "sql8.freesqldatabase.com";
93 String port = "3306";
94 String database = "sql8188659";
95 String username = "sql8188659";
96 String password = "9BsTH3YGPy";
97
98
99 String driver = "com.mysql.jdbc.Driver";
100 String url = "jdbc:mysql://"+ host +":"+ port +"/"+ database ;
101
102 Class.forName(driver);
103
104 Connection conn = DriverManager.getConnection(url, username, password);
105
106 System.out.println("Connected");
107
108 return conn;
109
110 } catch(Exception e) {
111 System.out.println(e);
112 }
113
114 return null;
115
116 }
117
118
119}