-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCreateDatabase.java
More file actions
94 lines (78 loc) · 3.12 KB
/
CreateDatabase.java
File metadata and controls
94 lines (78 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Create DB in MySQL
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateDatabase {
static final String driver = "com.mysql.cj.jdbc.Driver";
static final String url = "jdbc:mysql://192.168.72.21:3306/";
//User log in for DB
static final String user = "TheHub";
static final String pass = "$TheHub2023$";
public static void main() throws SQLException {
main(null);
}
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement st = null;
try {
//Sending connection request to the server
Class.forName(driver);
System.out.println("Connecting to Database...");
//logging in to the server
conn = DriverManager.getConnection(url, user, pass);
System.out.println("Creating Database..");
st = conn.createStatement();
//creating the database
String sql = "CREATE DATABASE IF NOT EXISTS thehub;";
st.executeUpdate(sql);
System.out.println("Database created successfully");
//Accessing DB
String sql2 = "USE thehub;";
st.executeUpdate(sql2);
//Creating profiles table
String sql3 = "CREATE TABLE profiles " +
"(id INT AUTO_INCREMENT PRIMARY KEY, " +
" firstname VARCHAR(255), " +
" lastname VARCHAR(255), " +
" username VARCHAR(255), " +
" password VARCHAR(255), " +
" gender VARCHAR(255), " +
" email VARCHAR(255), " +
" bio VARCHAR(255), " +
" location VARCHAR(255), " +
" phone_number VARCHAR(50), " +
" auth_level VARCHAR(50), " +
" volunteer_status VARCHAR(50)) ;";
st.executeUpdate(sql3);
System.out.println("Table created successfully...");
//Creating events table
String sql4 = "CREATE TABLE IF NOT EXISTS events " +
"(id INT AUTO_INCREMENT PRIMARY KEY, " +
" date INT(255), " +
" host VARCHAR(255), " +
" event_name VARCHAR(255), " +
" location VARCHAR(255), " +
" event_type VARCHAR(255), " +
" description VARCHAR(255), " +
" all_ages VARCHAR(255), " +
" entry_fee INT(255), " +
" volunteers VARCHAR(255));";
st.executeUpdate(sql4);
System.out.println("Table created successfully...");
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if ( st!= null)
st.close();
try {
if (conn != null)
conn.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
System.out.println("Goodbye..");
}
}