I'm trying to connect my project to MYSQL database using MYSQL Connector/j8.0. I tried with many different techniques but still not able to connect. before I was getting errors but I was able to sort them with try and catch, also I think I've properly installed the connector. please help.
This is my code (I followed MYSQL documentation: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager): import java.sql.*;
public class GSM_Mobile_main {
// this is start of the application
static Connection conn = null;
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/GSM_Mobile? useSSL=false","root","Capris55");
// Do something with the Connection
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM receipts");
// Now do something with the ResultSet ....
while(rs.next()){
System.out.println("ref ID="+rs.getInt("referenceNumber")+", Name="+rs.getString("customerName"));
}
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {
// it is a good idea to release
// resources in a finally{} block
// in reverse-order of their creation
// if they are no-longer needed
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
} catch (Exception ex) {
// handle the error
}
}
}