-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtry1.java
More file actions
30 lines (26 loc) · 1.1 KB
/
try1.java
File metadata and controls
30 lines (26 loc) · 1.1 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
import java.sql.*;
public class try1 {
static final String DB_URL = "jdbc:mysql://localhost/db1?characterEncoding=utf8";
static final String USER = "root";
static final String PASS = "changeme";
static final String QUERY = "SELECT Sid, Name, Salary, Age FROM sailor";
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);) {
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
//System.out.println(rs);
System.out.print("ID: " + rs.getString("Sid"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", Name: " + rs.getString("Name"));
System.out.println(", Salary: " + rs.getInt("Salary"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}