• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Can't connect to sql server database with java

Hi,

i downloaded the drivers from microsoft and the jars are in the classpath of my project but it is thowing an exception when i try to connect to the sql server database, heres my code:

try{
               Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
               con = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433;databaseName=database;selectMethod=cursor;",userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               e.printStackTrace();
               System.out.println("Error Trace in getConnection() : " + e.getMessage());
         }

The exception i am getting is:
[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
      at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
      at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
      at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
      at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
      at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
      at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
      at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
      at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at net.mainsequence.DBConnector.getConnection(DBConnector.java:103)
      at net.mainsequence.pcr.imagemanager.LuceneTest.main(LuceneTest.java:11)

The username, password and port is correct and the same code worked on someone else's computer.
Any suggestions?

Thanks
0
csalem05
Asked:
csalem05
  • 2
1 Solution
 
olgavillamizarCommented:
Try creating a datasource, you have to import
import javax.sql.DataSource;


protected Connection            conn;

try {
            
      SQLServerDataSource ds = new SQLServerDataSource();
      ds.setServerName(HostName);
      ds.setDatabaseName(SID);
      ds.setPortNumber(port);
      ds.setUser(username)
      ds.setPassword(password);
               conn = ds.getConnection();
               if(con!=null)
                   System.out.println("Connection Successful!");
    }
    catch (SQLException sqlex) {
     sqlex.printStackTrace();
}


0
 
olgavillamizarCommented:
Or you can use

try {
      DriverManager.registerDriver(new SQLServerDriver());
      Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433;"
                                                                               + " databaseName=databaseName","user", "password");
      if (conn!=null)
           System.out.println("Connection Successful!");
}
catch (SQLException sqle) {
   System.out.println(sqle.getMessage());
}
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now