Wednesday, March 22, 2023

Step-by-Step Guide: Connecting MySQL with Eclipse for Seamless Database Management

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Mysql {

private static final String DB_URL = "jdbc:mysql://localhost:3306/local";

private static final String DB_USERNAME = "root";

private static final String DB_PASSWORD = "your_password";

@SuppressWarnings("unused")

public static void main(String[] args) {

try {

Connection con = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);

System.out.println("Connected to the database.");

Statement statement = con.createStatement();

ResultSet resultSet = statement.executeQuery("SELECT * FROM trial_user");

while (resultSet.next()) {

int id = resultSet.getInt("demoid");

String email = resultSet.getString("email");

String exam = resultSet.getString("exam");

String password = resultSet.getString("password");

String phoneno = resultSet.getString("primarycontactphoneno");

System.out.println(id + " " + email + " " + phoneno);

}

} catch (SQLException ex) {

System.err.println("Error occurred while connecting to the database: " + ex.getMessage());

}

}

}

No comments:

Post a Comment