Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
41 lines (33 loc) · 1.75 KB

File metadata and controls

41 lines (33 loc) · 1.75 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.sql.*; // Use 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
// JDK 1.7 and above
public class JdbcSelectTest { // Save as "JdbcSelectTest.java"
public static void main(String[] args) {
try (
// Step 1: Allocate a database 'Connection' object
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/IUT_DB?autoReconnect=true&useSSL=false", "root", "1234");
// MySQL: "jdbc:mysql://hostname:port/databaseName", "username", "password"
// Step 2: Allocate a 'Statement' object in the Connection
Statement stmt = conn.createStatement();) {
// Step 3: Execute a SQL SELECT query, the query result
// is returned in a 'ResultSet' object.
String strSelect = "select Student_name, studentID from student";
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
System.out.println();
ResultSet rset = stmt.executeQuery(strSelect);
// Step 4: Process the ResultSet by scrolling the cursor forward via next().
// For each row, retrieve the contents of the cells with getXxx(columnName).
System.out.println("The records selected are:");
int rowCount = 0;
while(rset.next()) { // Move the cursor to the next row, return false if no more row
String sname = rset.getString("student_name");
int sid= rset.getInt("studentID");
System.out.println(sid+ " " +sname );
++rowCount;
}
System.out.println("Total number of records = " + rowCount);
} catch(SQLException ex) {
ex.printStackTrace();
}
// Step 5: Close the resources - Done automatically by try-with-resources
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.