-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectorJdbc.java
37 lines (35 loc) · 1.37 KB
/
ConnectorJdbc.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
import java.sql.*;
public class ConnectorJdbc {
public static void main(String args[]) throws ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "System@321sql";
String query = "select * from employees;";
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded successfully");
} catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
Connection con = DriverManager.getConnection(url, username, password);
System.out.println("connection established successfully");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String job = rs.getString("job");
double salary = rs.getDouble("salary");
System.out.println();
System.out.println("=========================");
System.out.println("Id : "+ id);
System.out.println("Name: "+ name);
System.out.println("Job : "+ job);
System.out.println("Salary : "+ salary);
}
}catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}