-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.java
More file actions
43 lines (35 loc) · 1.21 KB
/
Copy pathDatabaseConnection.java
File metadata and controls
43 lines (35 loc) · 1.21 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
package POSmain;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static DatabaseConnection instance;
private Connection connection;
public static DatabaseConnection getInstance() {
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
}
private DatabaseConnection() {
}
public void connectToDatabase() throws SQLException {
if (connection == null || connection.isClosed()) {
String server = "localhost";
String port = "3306";
String database = "user_database";
String userName = "root";
String password = "123";
try {
connection = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, userName, password);
System.out.println("Database Connected Successfully!");
} catch (SQLException e) {
System.err.println("Failed to connect to the database.");
e.printStackTrace();
}
}
}
public Connection getConnection() {
return connection;
}
}