-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSQLLinker.java
More file actions
58 lines (48 loc) · 1.56 KB
/
Copy pathSQLLinker.java
File metadata and controls
58 lines (48 loc) · 1.56 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
package com.pwdmgr.passwordmanagerv1;
import java.sql.*;
public class SQLLinker {
Connection connection = null;
public Connection connectsql(){
String url = "jdbc:mysql://localhost:3306/PasswordManager";
String user = "root";
String password = "Sam@SQL23";
// Initialize the connection
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// Create the database connection
connection = DriverManager.getConnection(url, user, password);
PreparedStatement ps = connection.prepareStatement("SELECT*FROM Password;");
ps.executeQuery();
return connection;
}
catch (SQLException e)
{
System.err.println("Connection to the database failed. Error: " + e.getMessage());
return null;
}
catch (ClassNotFoundException e)
{
System.err.println("MySQL JDBC driver not found. Make sure you have it in your classpath.");
return null;
}
}
public void closesql(){
if (connection != null)
{
try{
connection.close();
System.out.println("Connection closed.");
}
catch (SQLException e) {}
}
else {
System.out.println("SQL Database not connected");
}
}
// public static void main(String[] args) {
// myjdbc A = new myjdbc();
// A.connectsql();
// A.closesql();
//
// }
}