tia_kamakshi
asked on
Connecing MSSQl with Java
Hi,
I am working on java after a long time
I need to connect MSSQl with java version "1.6.0_11"
I am making standalone application . I t is not web application
Can you please help me what jar file I need.
Also I need connection string to connect MSSQL 2005 with windows authentication and without windows authentication
Thanks
I am working on java after a long time
I need to connect MSSQl with java version "1.6.0_11"
I am making standalone application . I t is not web application
Can you please help me what jar file I need.
Also I need connection string to connect MSSQL 2005 with windows authentication and without windows authentication
Thanks
You should probably try to use the JDBC driver that comes with the DB.
As for authentication, you can use a Trusted Connection for Win auth. See
http://www.datadirect.com/developer/jdbc/topics/winauth/index.ssp
http://www.datadirect.com/developer/jdbc/topics/winauth/index.ssp
ASKER
Hi,
I copied sqljdbc.jar file in directory C:\Program Files\Java\jdk1.6.0_11\lib
and in classpath I wrote
%classpath%;C:\Program Files\Java\jdk1.6.0_11\lib \sqljdbc.j ar
In connection string I wrote
Class.forName("com.microso ft.sqlserv er.jdbc");
I got an exception
Got an exception!
com.microsoft.sqlserver.jd bc
Press any key to continue . . .
When I write
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
I get an error
Got an exception!
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Press any key to continue . . .
My servers details are below
host = 172.17.1.65
DBServer = ABCD-PBHA23
database = Workflow
user = "sa"
password = "sa"
I need windows authentication connection string
Please guide
Thanks
I copied sqljdbc.jar file in directory C:\Program Files\Java\jdk1.6.0_11\lib
and in classpath I wrote
%classpath%;C:\Program Files\Java\jdk1.6.0_11\lib
In connection string I wrote
Class.forName("com.microso
I got an exception
Got an exception!
com.microsoft.sqlserver.jd
Press any key to continue . . .
When I write
Class.forName("sun.jdbc.od
I get an error
Got an exception!
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Press any key to continue . . .
My servers details are below
host = 172.17.1.65
DBServer = ABCD-PBHA23
database = Workflow
user = "sa"
password = "sa"
I need windows authentication connection string
Please guide
Thanks
// Establish a connection to an Interbase database using JDBC.
import java.sql.*;
class JdbcTest1 {
public static void main (String[] args) {
try {
// Step 1: Load the JDBC driver.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Class.forName("com.microsoft.sqlserver.jdbc");
// Step 2: Establish the connection to the database.
String url = "jdbc:odbc:contact_mgr";
//jdbc:msql://www.myserver.com:1114/contact_mgr
Connection conn = DriverManager.getConnection(url,"user1","password");
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
Try
String url = "jdbc:odbc:ABCD-PBHA23/Wor kflow";
...
Connection conn = DriverManager.getConnectio n(url,"sa" ,"sa");
String url = "jdbc:odbc:ABCD-PBHA23/Wor
...
Connection conn = DriverManager.getConnectio
ASKER
But i need to give server address where sql server is running
Its on 172.17.1.65
And I need to use windows authentication.
I dont want to use sql authetication connection
Thanks
Its on 172.17.1.65
And I need to use windows authentication.
I dont want to use sql authetication connection
Thanks
>>I copied sqljdbc.jar file in directory C:\Program Files\Java\jdk1.6.0_11\lib
Put that in
C:\Program Files\Java\jdk1.6.0_11\jre \lib\ext
Put that in
C:\Program Files\Java\jdk1.6.0_11\jre
>>Its on 172.17.1.65
Doesn't ABCD-PBHA23 resolve to that?
Doesn't ABCD-PBHA23 resolve to that?
ASKER
Getting the same exception
Got an exception!
[Microsoft][ODBC Driver Manager] Data source name not found and no default drive
r specified
Press any key to continue . . .
My host is at 172.17.1.65
Got an exception!
[Microsoft][ODBC Driver Manager] Data source name not found and no default drive
r specified
Press any key to continue . . .
My host is at 172.17.1.65
// Establish a connection to an Interbase database using JDBC.
import java.sql.*;
class JdbcTest1 {
public static void main (String[] args) {
try {
// Step 1: Load the JDBC driver.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Class.forName("com.microsoft.sqlserver.jdbc");
// Step 2: Establish the connection to the database.
String url = "jdbc:odbc:ABCD-PBHA23/Workflow";
//jdbc:msql://www.myserver.com:1114/contact_mgr
Connection conn = DriverManager.getConnection(url,"user1","password");
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
ASKER
No
>>Its on 172.17.1.65
It doesn't ABCD-PBHA23 resolve to 172.17.1.65
>>Its on 172.17.1.65
It doesn't ABCD-PBHA23 resolve to 172.17.1.65
Try
jdbc:odbc:127.17.1.65/Work flow
in that case
jdbc:odbc:127.17.1.65/Work
in that case
ASKER
Same error
Connection conn = DriverManager.getConnectio n(url,"use r1","passw ord");
Also, it is using windows authentication, so there will be no username and password
Anyway, I am still getting the same error
Thanks for your co-operation
Connection conn = DriverManager.getConnectio
Also, it is using windows authentication, so there will be no username and password
Anyway, I am still getting the same error
Thanks for your co-operation
But you should really be using the JDBC driver, so the connection string should be
final String CONNECT_STRING = "dbc:sqlserver://172.17.1. 65\\Workfl ow";
final String CONNECT_STRING = "dbc:sqlserver://172.17.1.
For Windows authentication, that should be
final String CONNECT_STRING = "dbc:sqlserver://172.17.1. 65\\Workfl ow;integra tedSecurit y=true";
final String CONNECT_STRING = "dbc:sqlserver://172.17.1.
ASKER
Thanks then where should I use CONNECT_STRING in my above code
Also, I think I need JDBC jar file to do so.
If yes, I need to download that as well.
Also, I think I need JDBC jar file to do so.
If yes, I need to download that as well.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have downloaded file sqljdbc_2.0.1607.1_enu.exe from the given URL
When I extracted the files, the jar file has the following packages
com.microsoft.sqlserver.jd bc
Classpath added
C:\me\Pers\Java\JDBC\Windo ws\sqljdbc _2.0.1607. 1_enu\sqlj dbc_2.0\en u.jar;C:\m e\Pers\Jav a\JDBC\Win dows\sqljd bc_2.0.160 7.1_enu\sq ljdbc_2.0\ enu\sqljdb c4.jar
In the
dbc:sqlserver://172.17.1.6 5\\Workflo w;integrat edSecurity =true
Where is the DBServer name PBMS-PBHA23. This is the DB server which has the database in it
Below code gives same error
Got an exception!
com.microsoft.sqlserver.jd bc
Press any key to continue . . .
When I extracted the files, the jar file has the following packages
com.microsoft.sqlserver.jd
Classpath added
C:\me\Pers\Java\JDBC\Windo
In the
dbc:sqlserver://172.17.1.6
Where is the DBServer name PBMS-PBHA23. This is the DB server which has the database in it
Below code gives same error
Got an exception!
com.microsoft.sqlserver.jd
Press any key to continue . . .
import java.sql.*;
class JdbcTest1 {
public static void main (String[] args) {
try {
// Step 1: Load the JDBC driver.
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("com.microsoft.sqlserver.jdbc");
// Step 2: Establish the connection to the database.
//String url = "jdbc:odbc:PBMS-PBHA23/DocumationWorkflow";
//String url = "jdbc:odbc:172.17.1.65/DocumationWorkflow";
String CONNECT_STRING = "dbc:sqlserver://172.17.1.65\\Workflow;integratedSecurity=true";
//jdbc:msql://www.myserver.com:1114/contact_mgr
//Connection conn = DriverManager.getConnection(url,"user1","password");
Connection conn = DriverManager.getConnection(CONNECT_STRING);
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
>>String CONNECT_STRING = "dbc:sqlserver://172.17.1. 65\\Workfl ow;integra tedSecurit y=true";
should be
String CONNECT_STRING = "jdbc:sqlserver://172.17.1 .65\\Workf low;integr atedSecuri ty=true";
should be
String CONNECT_STRING = "jdbc:sqlserver://172.17.1
Assuming that you dont require windows authentication, use the following code,-
You havent mentioned any port in the connection url.
try following.
try{
java.lang.Class.forName("c om.microso ft.sqlserv er.jdbc.SQ LServerDri ver");
Connection c = java.sql.DriverManager.get Connection ("jdbc:sql server://1 72.17.1.65 :1433;Data baseName=m ydb;user=t estadmin;p assword=ad min");
}catch(Exception ex){
//exception here
}
You havent mentioned any port in the connection url.
try following.
try{
java.lang.Class.forName("c
Connection c = java.sql.DriverManager.get
}catch(Exception ex){
//exception here
}
Merging those, try
String CONNECT_STRING = "jdbc:sqlserver://172.17.1 .65:1433;D atabaseNam e=Workflow ;integrate dSecurity= true";
String CONNECT_STRING = "jdbc:sqlserver://172.17.1
ASKER
Got an exception!
The TCP/IP connection to the host has failed. java.net.ConnectException: Connec
tion refused: connect
Press any key to continue . . .
If I ping 172.17.1.65
Pinging 172.17.1.65 with 32 bytes of data:
Reply from 172.17.1.65: bytes=32 time=20ms TTL=128
Reply from 172.17.1.65: bytes=32 time=23ms TTL=128
Reply from 172.17.1.65: bytes=32 time=19ms TTL=128
Reply from 172.17.1.65: bytes=32 time=19ms TTL=128
Ping statistics for 172.17.1.65:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 19ms, Maximum = 23ms, Average = 20ms
The TCP/IP connection to the host has failed. java.net.ConnectException:
tion refused: connect
Press any key to continue . . .
If I ping 172.17.1.65
Pinging 172.17.1.65 with 32 bytes of data:
Reply from 172.17.1.65: bytes=32 time=20ms TTL=128
Reply from 172.17.1.65: bytes=32 time=23ms TTL=128
Reply from 172.17.1.65: bytes=32 time=19ms TTL=128
Reply from 172.17.1.65: bytes=32 time=19ms TTL=128
Ping statistics for 172.17.1.65:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 19ms, Maximum = 23ms, Average = 20ms
// Establish a connection to an Interbase database using JDBC.
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
import com.microsoft.sqlserver.*;
class JdbcTest2 {
public static void main (String[] args) {
try{
java.lang.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Connection c = java.sql.DriverManager.getConnection("jdbc:sqlserver://172.17.1.65:1433;DatabaseName=DocumationWorkflow;integratedSecurity=true");
Connection c = java.sql.DriverManager.getConnection("jdbc:sqlserver://172.17.1.65:1433;DatabaseName=DocumationWorkflow;user=test;password=test");
}catch(Exception e){
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
ASKER
Also, we have not added dbserver name
DBServer = ABCD-PBHA23
I think we require that as well some where
Thanks for your co-operation
DBServer = ABCD-PBHA23
I think we require that as well some where
Thanks for your co-operation
Pinging it won't tell you much. You need to know:
a. that the MSSQL server is running on that host
b. what port it's running on
Check your server config
a. that the MSSQL server is running on that host
b. what port it's running on
Check your server config
>>DBServer = ABCD-PBHA23
Are you sure that's not the hostname? What does the following do?
Are you sure that's not the hostname? What does the following do?
ping ABCD-PBHA23
ASKER
IP: 172.17.1.65 --> It is the computer IP where MSSQL server is hosted
DBServer = ABCD-PBHA23 --> When you click on SQL management studio, MSSQl first asks for screen where you put your user name password or do windows authenitication. This is that screen. Please see screen attached
dbscreen.bmp
DBServer = ABCD-PBHA23 --> When you click on SQL management studio, MSSQl first asks for screen where you put your user name password or do windows authenitication. This is that screen. Please see screen attached
dbscreen.bmp
That could be the 'instance name' then, so try
String CONNECT_STRING = "jdbc:sqlserver://172.17.1 .65:1433\\ ABCD-PBHA2 3;Database Name=Workf low;integr atedSecuri ty=true";
String CONNECT_STRING = "jdbc:sqlserver://172.17.1
ASKER
Error message changed to
Got an exception!
Invalid integratedSecurity property value:true
Press any key to continue . . .
Got an exception!
Invalid integratedSecurity property value:true
Press any key to continue . . .
ASKER
When I use my connection string like this
Connection c = java.sql.DriverManager.get Connection ("jdbc:sql server://1 72.17.1.65 :1433\\ABC D-PBHA23;D atabaseNam e=Documati onWorkflow ;user=test ;password= test");
I get the error
Got an exception!
The port number 1433\PBMS-PBHA23 is not valid.
Press any key to continue . . .
Connection c = java.sql.DriverManager.get
I get the error
Got an exception!
The port number 1433\PBMS-PBHA23 is not valid.
Press any key to continue . . .
You must ascertain what port it's running on, or try leaving the port out of the string
ASKER
I just came to know that PBMS-PBHA23 is the 172.17.1.65 itself
Also, I have not registered sqljdbc_xa.dll file which come with sqljdbc_1.0.809.102_enu.ex e
Do I need to register this??
Also here is the current error
Connection c = java.sql.DriverManager.get Connection ("jdbc:sql server://1 72.17.1.65 :1433;Data baseName=D ocumationW orkflow;in tegratedSe curity=tru e");
Got an exception!
Invalid integratedSecurity property value:true
Press any key to continue . . .
Connection c = java.sql.DriverManager.get Connection ("jdbc:sql server://1 72.17.1.65 :1433;Data baseName=D ocumationW orkflow;us er=test;pa ssword=tes t");
Got an exception!
The TCP/IP connection to the host has failed. java.net.ConnectException: Connec
tion refused: connect
Press any key to continue . . .
Thanks for all your co-operation
Also, I have not registered sqljdbc_xa.dll file which come with sqljdbc_1.0.809.102_enu.ex
Do I need to register this??
Also here is the current error
Connection c = java.sql.DriverManager.get
Got an exception!
Invalid integratedSecurity property value:true
Press any key to continue . . .
Connection c = java.sql.DriverManager.get
Got an exception!
The TCP/IP connection to the host has failed. java.net.ConnectException:
tion refused: connect
Press any key to continue . . .
Thanks for all your co-operation
You need to make sure you've installed the driver properly. Read it's help files and see the following:
http://www.devx.com/dbzone/Article/33681
http://www.devx.com/dbzone/Article/33681
Once you're sure you've got it installed properly, the following shows how to make the connection string:
http://msdn.microsoft.com/en-us/library/ms378428.aspx
http://msdn.microsoft.com/en-us/library/ms378428.aspx
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many Many Thanks.
Connection c = java.sql.DriverManager.get Connection ("jdbc:sql server://1 72.17.1.65 :1433;Data baseName=D ocumationW orkflow;in tegratedSe curity=tru e;");
It works
Connection c = java.sql.DriverManager.get
It works
:-)
let me know if you have any more questions, I should be able to save you having to guess what the right answer is.