forked from LuigiCortese/java-certification-ocp8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
45 lines (35 loc) · 1.22 KB
/
App.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
38
39
40
41
42
43
44
45
package net.devsedge.jdbc.basics;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author Luigi Cortese
*
*/
public class App {
public static void main(String[] args) {
try ( // Getting a Connection from DriverManager
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "password");
// Getting a Statement from Connection
Statement stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Getting a ResultSet from Statement
ResultSet res = stat.executeQuery("SELECT * FROM employee");) {
// Iterating ResultSet
while (res.next()) {
System.out.print(res.getString(1) + " ");
System.out.print(res.getString(2) + " ");
System.out.print(res.getString(3) + " ");
System.out.println(res.getString(4));
}
// Getting a ResultSetMetaData from ResultSet
ResultSetMetaData meta = res.getMetaData();
// Reading ResultSetMetaData
System.out.println(meta.getColumnCount());
} catch (SQLException e) {
e.printStackTrace();
}
}
}