-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaking_Connection.java
More file actions
35 lines (28 loc) · 1.21 KB
/
Copy pathMaking_Connection.java
File metadata and controls
35 lines (28 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
// Method to get database connection
import java.sql.Connection;
//Helps Java find and connect to the correct database driver
import java.sql.DriverManager;
import java.sql.SQLException;
public class Making_Connection {
// One connection to one database only
// Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", user, pass);
//Connection conn2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", user, pass);
// stmt.execute("USE db2");
// New database just change the String url to the databse name
public static Connection getConnection() throws SQLException {
// Database Connection Details
// The address of your MySQL database.
String url = "the url";
String user = "root";
String password = "password";
return DriverManager.getConnection(url, user, password);
// DriverManager.getConnection() tries to establish a connection using the URL, username, and password.
}
public static void main(String[] args) {
try (Connection con = getConnection()) {
System.out.println("Connected to database successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}