forked from Safnaj/Java-Web-Application
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBconnection.java
More file actions
63 lines (49 loc) · 1.52 KB
/
DBconnection.java
File metadata and controls
63 lines (49 loc) · 1.52 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
import java.sql.*;
import com.mysql.jdbc.Driver;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBconnection {
private static String DB_URL = "jdbc:mysql://localhost:3306/gamingsite";
private static String USER = "root";
private static String PASS = "";
private static Connection connection;
private DBconnection(){
}
public static Connection getDBConnection() throws ClassNotFoundException, SQLException {
if(connection == null || connection.isClosed()){
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL,USER,PASS);
}
return connection;
}
/* method 2 (safnaj)
Connection con;
public Connection getCon()
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gamingsite","root","");
} catch (ClassNotFoundException e) {
System.out.println(e);
} catch (SQLException e) {
System.out.println(e);
}
return con;
} */
}
/* method (Stack Over Flow)
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Database Driver Found");
}catch(ClassNotFoundException e) {
System.out.println("Database Driver Not Found..!");
}
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/gamingsite?useSSL=false", "root", "");
System.out.println("Successfully Connected to your Database..!");
} catch (SQLException e) {
System.out.println("Database NOT Connected");
}
} */