1

I'm working with JDBC for past 2 days now. But still not succeeded to make a successful connection with the database. here is the code for making a database connection:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnector {

Connection conn = null;
public static String SERVER_NAME = "localhost";
public static String PORT_NUMBER = "3306";
public static String DATABASE_NAME = "TemporaryDatabase";
public String uri = "jdbc:mysql://" + SERVER_NAME + ":" + PORT_NUMBER + "/" + DATABASE_NAME;

public String username = "root";
public String password = "";

public DBConnector() {
}

public boolean Connect() {

    try {

        if (init()) {
            conn = DriverManager.getConnection(uri, username, password);
            return true;

        } else {
            System.out.println("Can not load driver.");
            return false;
        }
    } catch (SQLException se) {
        se.printStackTrace();
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

public static boolean init() {
    String driverName = "com.mysql.jdbc.Driver"; // for MySql

    try {
        Class.forName(driverName);
        return true;

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return false;
    }

}

}

And here are the errors I'm getting when I run this piece of code:

  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:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at DBConnector.Connect(DBConnector.java:24)
    at WebCrawler.main(WebCrawler.java:37)
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:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
    ... 16 more
CONNECTION: false

I'm using MS SQL Server 2012. Can anyone please let me know where I'm doing it wrong. I guess its with the username and password but I don't know the correct ones either. I'm working this with the default SQL server instance.

1
  • 2
    If you are using Microsoft SQL Server then why are you using the MySQL driver (and URL syntax)? Commented Jan 2, 2016 at 9:54

1 Answer 1

1

You're using SQL Server but your JDBC url is for MySQL. The correct JDBC url form for SQL Server is like following:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

For example:

jdbc:sqlserver://localhost:1433;databaseName=TemporaryDatabase

Also you shoud use SQL Server's JDBC driver instead of MySQL's. You could find the driver here and information about SQL Server connection strings here.

Sign up to request clarification or add additional context in comments.

8 Comments

than what should be the url belonging to SQL server?
I added the uri for SQL server as uri = "jdbc:sqlserver://" + SERVER_NAME + ":" + PORT_NUMBER + "/" + DATABASE_NAME; But now I'm getting the error, stating No suitable driver found for jdbc:sqlserver://localhost:3306/TemporaryDatabase
Also add SQL Server JDBC driver into classpath instead of MySQL
Now your connection string is OK but you need the correct driver. Download the driver from the link, add it to your classpath and remove mysql driver from there
Done everything I was told. The error now says com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host local, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.". CONNECTION: false
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.