Java JDBC with MySQL in VSCode

Опубликовано: 27 Апрель 2022
на канале: MKR Programming
27,081
201

Find the 1st Part of this:    • Java Database Connectivity with MySQL  
JAVA Source (db: mktest, table: stud)
import java.sql.*;

public class App {
static final String DB_URL = "jdbc:mysql://localhost:3306/mktest";
static final String USER = "root";
static final String PASS = "password";
static final String QUERY = "SELECT * FROM stud";

public static void main(String[] args) {
// 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.print("Roll: " + rs.getString("roll"));
System.out.print(", Name: " + rs.getString("name"));
System.out.println(", CGPA: " + rs.getFloat("cgpa"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}