-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdbc connection.java
More file actions
40 lines (39 loc) · 1.55 KB
/
Copy pathjdbc connection.java
File metadata and controls
40 lines (39 loc) · 1.55 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
import java.sql.*;
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "#Reyazlove1";
String query = "Select * from employee;";
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 stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String job_title = rs.getString("job_title");
double salary = rs.getDouble("salary");
System.out.println();
System.out.println("ID: "+id);
System.out.println("Name: "+name);
System.out.println("Job Title: "+job_title);
System.out.println("Salary: "+salary);
}
rs.close();
stmt.close();
con.close();
System.out.println();
System.out.println("connection closed successfully");
}catch (SQLException e){
System.out.println(e.getMessage());
}
}
}