-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBC_01.java
More file actions
38 lines (35 loc) · 1.38 KB
/
Copy pathJDBC_01.java
File metadata and controls
38 lines (35 loc) · 1.38 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
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC_01 {
static final String url = "jdbc:mysql://localhost:3306/mydb";
static final String username = "root";
private static final String password = "Admin@123";
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String query = "select * from students";
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
double marks = resultSet.getDouble("marks");
System.out.println("ID: " + id);
System.out.println("NAME: " + name);
System.out.println("AGE: " + age);
System.out.println("MARKS: " + marks);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}