The error message you are getting indicates that your Java application is not able to establish a connection with your MYSQL server.
Here is a script to achieve the same.
Sample code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlTest {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Load your MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish a connection
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/projects?useSSL=false", "user1", "123");
// Create a statement object to execute queries
stmt = conn.createStatement();
// Execute a query
rs = stmt.executeQuery("SELECT * FROM your_table_name");
// Process the result set
while (rs.next()) {
// Retrieve and print data from result set
int id = rs.getInt("id"); // assuming the column name is 'id'
String name = rs.getString("name"); // assuming the column name is 'name'
System.out.println("ID: " + id + ", Name: " + name);
}
System.out.println("Connected successfully!");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
// Clean up resources
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}