Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC, Connect to sql server

I'm pretty new to Java development and I'm starting to learn about connecting to sql servers. I've read tonnes of tutorials and I am currently running into a problem with my application, the error I am currently facing is:

No suitable driver found for jdbc:sqlserver://192.168.*.***:1433;Database=STC

What I would like to know is what exactly do I have to do to the server to allow for the connection to be fully made? Please note as well that the database and server is not on my desktop but in a different location. All help is appreciated.

Here is my code too.

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


public class SecondTest 
{

    public static void main(String[] argv) 
    {

        System.out.println("-------- MySQL JDBC Connection Testing ------------");

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {

            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return;
        }

        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;

        try 
        {

            String url = "jdbc:sqlserver://192.168.***.***:1433;Database=STC";

            String username = "*****";
           String password = "******";
           connection = DriverManager.getConnection(url, username, password);

        } 

        catch (SQLException e) 
        {
            System.out.println("Connection Failed!");
            e.printStackTrace();
            return;
        }

        if (connection != null) 
        {
            System.out.println("Fully connected.");
        } 

        else 
        {
            System.out.println("Failed to make connection!");
        }
    }
}
like image 659
user2946061 Avatar asked May 09 '26 05:05

user2946061


2 Answers

  1. It looks like you're trying to connect to a Microsoft SQLServer using a MySQL driver. You should make sure to use the right driver (http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx). The class is com.microsoft.sqlserver.jdbc.SQLServerDriver. Make sure the driver is in the classpath.

  2. To enable remote connection to the database, you must enable TCP connection on the SQLServer (normally on port 1433). Take a look at this: http://www.scrumdesk.com/Articles/HowToEnableSQLServerRemoteConnections.html

like image 106
Simon Arsenault Avatar answered May 11 '26 18:05

Simon Arsenault


You are using a MySQL driver:

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

This JDBC url is trying to connect to is pointing to a Microsoft SQL Server database:

 String url = "jdbc:sqlserver://192.168.3.223:1433;Database=STC";

Download an appropriate driver for the version of the SQL server database you want to connect to and load that one instead of the MySQL driver:

http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

Load that driver instead of com.mysql.jdbc.Driver.

like image 31
Magnilex Avatar answered May 11 '26 17:05

Magnilex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!