Sunday, February 19, 2012

Connecting JDBC with SQL Server

I have tried everything..I cant get Java to connect to my sql
server...port 1433 is what sql server is running off of..my password
and username are correct..ive tried the ip, comp name and localhost for
server name and I cant make a connection to it...
heres the code I altered provided my microsoft to test this connection:
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "192.168.1.102";
private final String portNumber = "1433";
private final String databaseName= "KB";
private final String userName = "kbuser";
private final String password = "password";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
// Constructor
public Connect(){}
private String getConnectionUrl(){
return
url+serverName+":"+portNumber+";databaseName="+dat abaseName+";selectMethod="+selectMethod+";";
}
private java.sql.Connection getConnection(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
con =
java.sql.DriverManager.getConnection(getConnection Url(),userName,password);
if(con!=null) System.out.println("Connection
Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " +
e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriver Name: "+
dm.getDriverName());
System.out.println("\tDriver Version: "+
dm.getDriverVersion ());
System.out.println("\nDatabase Information ");
System.out.println("\tDatabase Name: "+
dm.getDatabaseProductName());
System.out.println("\tDatabase Version: "+
dm.getDatabaseProductVersion());
System.out.println("Avalilable Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcatalog: "+
rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error: No active Connection");
}catch(Exception e){
e.printStackTrace();
}
dm=null;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperties();
}
}
Heres the error messages:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error
establishing socket.Error Trace in getConnection() :
[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
Error: No active Connection
at com.microsoft.jdbc.base.BaseExceptions.createExcep tion(Unknown
Source)
at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<in it>(Unknown
Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnecti on.open(Unknown
Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplC onnection(Unknown
Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknow n Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager .java:512)
at java.sql.DriverManager.getConnection(DriverManager .java:171)
at Connect.getConnection(Connect.java:36)
at Connect.displayDbProperties(Connect.java:53)
at Connect.main(Connect.java:89)
thanks
-Jim
Jim,
There could be a number of reasons why a connection can't be
established: server not running, TCP/IP not enabled on the server, a
firewall blocking the connection etc. Check all these and until "telnet
<server_host> 1433" won't connect, the JDBC driver won't connect either
(unless it used named pipes).
Alin,
The jTDS Project.
|||Yep...I could not telnet to that port and when I did a netstat sql
server wasnt listening on that port...
To fix this I upgraded from the RTM to service pack3...rebooted and was
able to connect just fine...
Thanks
-Jim

No comments:

Post a Comment