-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectDB.java
27 lines (26 loc) · 1.02 KB
/
ConnectDB.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
import java.sql.*;
public class ConnectDB {
public static void main(String[] args){
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/try", "root", "");
if(conn != null){
System.out.println("Connection Successfully!");
Statement statement = conn.createStatement();
// String addUsers = "INSERT INTO users (names, email, password) VALUES ('Emmy', '[email protected]', '1234')";
// int num = statement.executeUpdate(addUsers);
String query = "SELECT * FROM users";
ResultSet res = statement.executeQuery(query);
while(res.next()){
String names = res.getString("names");
String email = res.getString("email");
System.out.println(">" + names + " - " + email);
}
}
else {
System.out.println("Oops, Connection Failed!");
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
}