-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectMySQLJava.java
36 lines (32 loc) · 1.01 KB
/
ConnectMySQLJava.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
import java.sql.*;
//to establish a JDBC connection
public class ConnectMySQLJava {
public static final String url = "jdbc:mysql://grocerapp.c92wi7qbbarw.us-west-2.rds.amazonaws.com:3306/groceryDb";
public static final String username = "surendra";
public static final String password = "surendra";
public static final String driver = "com.mysql.jdbc.Driver";
public Connection createConnection() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
System.out.println("Connection to the database failed");
} catch (ClassNotFoundException e) {
System.out.println("Connection to the database failed");
}
return con;
}
public void close(Connection con) {
try {
con.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
// Test connection
// public static void main(String[] args)
// {
// System.out.println(createConnection());
// }
}