generated from RamiSJ12/RamiSJ12
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseConnection.java
69 lines (61 loc) · 2.12 KB
/
DatabaseConnection.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package medication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Shaurav
*/
public class DatabaseConnection {
Connection connection = null;
private String userName = "root";
private String password = "";
String DATABASE_URL = "jdbc:mysql://localhost:3306/pharmacy";
public DatabaseConnection(String DATABASE_URL){
this.DATABASE_URL = DATABASE_URL;
}
public Connection getConnection()
{
return connection;
}
public void setUsername (String userName){
this.userName = userName;
}
public void setPassword(String password){
this.password = password;
}
public Connection startConnection(){
try {
String DATABASE_URL = "jdbc:mysql://localhost:3306/pharmacy";
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Page2.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Page2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Page2.class.getName()).log(Level.SEVERE, null, ex);
}
try {
connection = DriverManager.getConnection(DATABASE_URL,userName,password);
} catch (SQLException ex) {
Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex);
}
return connection;
}
public void closeConnection(){
try {
connection.close();
} catch (SQLException ex) {
Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}