I need to connect to a Microsoft SQL server using Java.
I downloaded the driver, an no matter what I did elipse and netbeans could not find the driver.
When I got frustrated I downloaded also MySql driver, and again I get the same exception.
I added the drivers path in the environmental variables and also included the jar files in my project library.
Here is a picture of my project:
pic http://i56.tinypic.com/1ekple.jpg
What am I doing Wrong?
Thank you very much, Idan.
3 Answers 3
A few problems:
- Your JDBC url should be jdbc:mssql instead of jdbc:msql
- The port for SQL Server is usually 1433, not 8888 but can be configured.
- The driver specified is for MySQL and won't work.
Start to correct by downloaded a MS SQL JDBC driver, there are 2 popular varients:
Open source: http://jtds.sourceforge.net/
Microsoft: http://msdn.microsoft.com/en-us/sqlserver/aa937724
I've used both and haven't had much problems in either case.
EDIT
The only example I have currently is using the microsoft driver, here it is:
DRIVER: com.microsoft.sqlserver.jdbc.SQLServerDriver
URL: jdbc:sqlserver://localhost:1433;database=<MyDB>
2 Comments
There is a typo in the url in order to connect to mysql.
It should be jdbc:mysql://127.0.0.1:8888
Also I would double check if your mysql server is actually running on port 8888 since normally a mysql answers on 3306.
Also there are multiple JDBC drivers for Microsofts' SQL server, which have different url. For the microsoft driver the url looks like jdbc:microsoft:sqlserver://localhost:1433
Check the documentation of the driver for an example url to start from.
2 Comments
Try with jTDS. It is an open source JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000 and 2005). Place jar file into your application classpath. java.sql package along with above driver helps connecting to database.
import java.sql.*;
public class testConnection
{
public static void main(String[] args)
{
DB db = new DB();
db.dbConnect(
"jdbc:jtds:sqlserver://localhost:1433/tempdb","sa","");
}
}
class DB
{
public DB() {}
public voidn dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);
System.out.println("connected");
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
com.mysql.jdbc.Driverfor anmsql:connection will definitely not work. What's the error message with the MS SQL JDBC driver?