Skip to content

Commit 8b9dde1

Browse files
authored
Select statement JDBC
1 parent a5eca11 commit 8b9dde1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

JDBCExample6.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Select statement JDBC
2+
import java.sql.Connection;
3+
import java.sql.DriverManager;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
import java.sql.Statement;
7+
8+
public class JDBCExample6 {
9+
static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
10+
static final String USER = "guest";
11+
static final String PASS = "guest123";
12+
static final String QUERY = "SELECT id, first, last, age FROM Registration";
13+
14+
public static void main(String[] args) {
15+
// Open a connection
16+
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
17+
Statement stmt = conn.createStatement();
18+
ResultSet rs = stmt.executeQuery(QUERY);
19+
) {
20+
while(rs.next()){
21+
//Display values
22+
System.out.print("ID: " + rs.getInt("id"));
23+
System.out.print(", Age: " + rs.getInt("age"));
24+
System.out.print(", First: " + rs.getString("first"));
25+
System.out.println(", Last: " + rs.getString("last"));
26+
}
27+
} catch (SQLException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)