IVAN PAUL
IVAN PAUL

Reputation: 179

How to fix SQLNonTransientConnectionException in JDBC

I am trying to connect my java program to a MySql database through Wamp, and all I get is plethora of SQL Exceptions.

I have tried nearly every question or queries or answers available on Stackoverflow, but none of the answers solved my issue....

Maybe my question will be marked duplicate but i have no idea why solutions mentioned in other answers aren't working for me despite getting hundred's of upvotes.

Here is my code

import java.sql.*;

public class SQLTest{
    private static Connection connect = null;
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("Trying to Establish Database Connection.....");
            //connect = DriverManager.getConnection("jdbc:mysql://localhost:8080/sys?user=root&password=root&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
            connect = DriverManager.getConnection("jdbc:mysql://localhost:8080/sys", "root", "root");
            System.out.println("Connection Established.");
        } catch (SQLException se) {
            System.out.println("connection problem");
            se.printStackTrace();
        } catch (Exception e){
            System.out.println("Some Other Problem.");
            e.printStackTrace();
        }
    }
}

Here is what i get in return

The output of program

Upvotes: 1

Views: 4846

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123849

The default port in httpd.conf file was set to 8080. I dont know somehow 3306 works.

The WAMP stack consists of four components: Windows, Apache, MySQL, and PHP.

Apache is the server software that receives HTTP requests and returns web pages. httpd.conf is its configuration file. The default port for Apache is 80, but WAMP stacks usually use a different port like 8080 for development purposes.

MySQL is the component that receives SQL queries and returns the results from those queries. It listens on a different port than Apache. The configuration file for MySQL is my.ini. The default port for MySQL is 3306.

Upvotes: 1

Related Questions