0% found this document useful (0 votes)
9 views2 pages

Dbe Exp No.6

The document contains a Java program that connects to a MySQL database and retrieves data from a 'student' table. It establishes a connection using JDBC, executes a query to select all records, and prints the ID, Name, and password of each student. The program also handles potential SQL and driver-related exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Dbe Exp No.6

The document contains a Java program that connects to a MySQL database and retrieves data from a 'student' table. It establishes a connection using JDBC, executes a query to select all records, and prints the ID, Name, and password of each student. The program also handles potential SQL and driver-related exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment N.

6
Code:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.ResultSet;

public class connection1{

public static void main(String[] args) {

// Database credentials

String url = "jdbc:mysql://localhost:3306/st"; // your database name

String user = "root"; // default user in XAMPP

String password = ""; // default is empty in XAMPP

try {

// Load MySQL JDBC driver (optional in newer Java versions)

Class.forName("com.mysql.cj.jdbc.Driver");

// Connect to database

Connection conn = DriverManager.getConnection(url, user, password);

System.out.println("Connected successfully!");

// Create a statement

Statement stmt = conn.createStatement();

// Execute query

ResultSet rs = stmt.executeQuery("SELECT * FROM student");

// Loop through results

while (rs.next()) {
// System.out.println("ID: " + rs.getInt("id"));

// System.out.println("Name: " + rs.getString("Name"));

System.out.println("ID: " +rs.getInt(1)+" Name: "+rs.getString(2)+" pass:


"+rs.getString(3));

// Close connection

conn.close();

} catch (SQLException e) {

System.out.println("Database error: " + e.getMessage());

} catch (ClassNotFoundException e) {

System.out.println("JDBC Driver not found!");

OUTPUT:

You might also like