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!");
}
}
}
3 Answers 3
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.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
Comments
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.
Comments
Seems you are loading a wrong driver for SQLServer. Try with Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
In case you need MySQL, the connection URL would look like jdbc:mysql://..
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");