· 10 years ago · Jun 06, 2016, 11:15 PM
1package SQL;
2import java.sql.*;
3
4
5public class QueryExample {
6
7 private static Connection conn = null;
8
9 private static Connection getInstance() {
10 if (conn==null) new QueryExample();
11 return conn;
12 }
13
14 private QueryExample() {
15 String url = "jdbc:mysql://";
16 String user = "root";
17 String password = "";
18 try {
19 Class.forName("com.mysql.jdbc.Driver").newInstance();
20 Connection conn = DriverManager.getConnection(url, user, password);
21 } catch (Exception e) {
22 e.printStackTrace();
23 }/*
24 } catch (ClassNotFoundException e) {
25 System.out.println("Treiber nicht gefunden");
26 } catch (SQLException e) {
27 System.out.println("Connect nicht moeglich");
28 }*/
29 }
30
31 public static void createTable(){
32 try{
33 conn = getInstance();
34 PreparedStatement create = conn.prepareStatement(
35 "CREATE TABLE IF NOT EXISTS appointments(number INT, difficulty INT");
36 create.executeUpdate();
37 } catch(SQLException e) {
38 e.printStackTrace();
39 }
40 }
41
42 public static void createDatabase(){
43 try {
44 Statement statem = conn.createStatement();
45
46
47 statem.execute("CREATE DATABASE IF NOT EXISTS pmt");
48 statem.execute("USE pmt");
49 } catch (Exception e) {
50 e.printStackTrace();
51 }
52 }
53
54 public static void connect() {
55 String url = "jdbc:mysql://";
56 String user = "root";
57 String password = "";
58
59 try {
60 Class.forName("com.mysql.jdbc.Driver").newInstance();
61 Connection conn = DriverManager.getConnection(url, user, password);
62
63
64 // statem.execute("INSERT INTO pmt (number, difficulty) VALUES"
65 // + "('1', '10'), ('2', '20'), ('3', '30'), ('4', '45'), ('5', '50')"
66 // + "('6', '55'), ('7', '70'), ('8', '75'), ('9', '85'), ('10', '95')");
67
68 } catch (Exception e) {
69 e.printStackTrace();
70 }
71 }
72
73
74 public static void main(String[] args) {
75 QueryExample.getInstance();
76 QueryExample.connect();
77 QueryExample.createDatabase();
78 QueryExample.createTable();
79 }
80
81}