· 6 years ago · May 17, 2019, 08:54 AM
1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.PreparedStatement;
4import java.sql.ResultSet;
5import java.util.ArrayList;
6
7public class Main {
8
9 public static void main(String[] args) throws Exception {
10 // TODO Auto-generated method stub
11 createTable("1","2","3","4");
12 post("1","2","3","4");
13 get();
14
15
16 }
17 public static ArrayList<String> get() throws Exception{
18 try{
19 Connection con = getConnection();
20 PreparedStatement statement = con.prepareStatement("SELECT * FROM iamalive ORDER BY first DESC LIMIT 1");
21
22 ResultSet result = statement.executeQuery();
23
24 ArrayList<String> array = new ArrayList<String>();
25 while(result.next()){
26 System.out.print(result.getString("first"));
27 System.out.print(" ");
28 System.out.println(result.getString("last"));
29
30 array.add(result.getString("last"));
31 }
32 System.out.println("All records have been selected!");
33 return array;
34
35 }catch(Exception e){System.out.println(e);}
36 return null;
37
38 }
39 public static void post(String clanname, String owner, String invites, String member) throws Exception{
40
41 try{
42 Connection con = getConnection();
43 PreparedStatement posted = con.prepareStatement("INSERT INTO Clans (`Clanname`,`Owner`,`Member`,`Einladungen`) VALUES ('"+clanname+"', '"+owner+"','"+invites+"', '"+member+"')");
44
45 posted.executeUpdate();
46 } catch(Exception e){System.out.println(e);}
47 finally {
48 System.out.println("Insert Completed.");
49 }
50 }
51 public static void createTable(String clanname, String owner, String invites, String member) throws Exception{
52 try{
53 Connection con = getConnection();
54 PreparedStatement create = con.prepareStatement("CREATE TABLE IF NOT EXISTS" + clanname + "(id int NOT NULL AUTO_INCREMENT, first varchar(255), last varchar(255), PRIMARY KEY(id))");
55 create.executeUpdate();
56 }catch(Exception e){System.out.println(e);}
57 finally{
58 System.out.println("Function Complete.");
59 };
60
61 }
62 public static Connection getConnection() throws Exception{
63 try{
64 String driver = "com.mysql.cj.jdbc.Driver";
65 String url = "jdbc:mysql://remotemysql.com";
66 String username = "c0Bbouv4z1";
67 String password = "037iIzaF5l";
68 Class.forName(driver);
69
70 Connection conn = DriverManager.getConnection(url,username,password);
71 System.out.println("Connected");
72 return conn;
73 } catch(Exception e){System.out.println(e);}
74
75
76 return null;
77 }
78
79}