0

I try to connect to SQL DB through java. When i run the code, in the console is printed: Connecting to database... then nothing happens and i have to stop the program.

package javasqltest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JavaSQLTest {

    static final String JDB_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:1527/";

    static final String USER = "test";
    static final String PASS = "test";

    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;

        try {          
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            System.out.println("Creating database...");
            stmt = conn.createStatement();

            String sql = "CREATE DATABASE STUDENTS";
            stmt.executeUpdate(sql);
            System.out.println("Database created successfully...");
        } catch (SQLException se) {          
            se.printStackTrace();
        } catch (Exception e) {           
            e.printStackTrace();
        } finally {          
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}

Any help for fixing this issue.

Thanks in Advance.

LE:

If i change the port to 3306 this is the console output:

run:
Connecting to database...
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
Goodbye!
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at javasqltest.JavaSQLTest.main(JavaSQLTest.java:27)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:244)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
    ... 15 more
BUILD SUCCESSFUL (total time: 2 seconds)
1
  • For what it's worth, it's not customary to create entire databases like this; you usually do that with management tools externally. The program will often manipulate tables within a database. Dec 11, 2016 at 7:10

1 Answer 1

1

The MySQL JDBC connection URL usually looks something like this:

jdbc:mysql://localhost:3306/dbname

where dbname is the name of your MySQL database, and 3306 is the default port on which MySQL is listening. If your MySQL be really listening on port 1527 then you can use that instead. You never specified the name of a database, and this alone would cause the connection to fail.

10
  • Hello Tim and thank you, i created the DB through Netbeans IDE and that was the default port that appeared. Even if i modify the URL to /localhost:1527/sqltest the result is the same. I followed the tutorial on tutorialspoint, but i don't understand why it stops there with no error.
    – Florin Ics
    Dec 11, 2016 at 6:43
  • Find out which port MySQL is actually using. Dec 11, 2016 at 6:46
  • If i switch the port to 3306 i get an error: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. so i think the port 1527 is ok. How can i find out the port used by MySQL?
    – Florin Ics
    Dec 11, 2016 at 6:49
  • In services tab the db name appears like this: jdbc:derby://localhost:1527/SQLTest [test on TEST]
    – Florin Ics
    Dec 11, 2016 at 6:52
  • Is there really no exception at all? This seems unlikely. Dec 11, 2016 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.