aspose file tools*
The moose likes JDBC and the fly likes Problem with JDBC in Windows 7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with JDBC in Windows 7" Watch "Problem with JDBC in Windows 7" New topic
Author

Problem with JDBC in Windows 7

Shubhra Jyotsna
Greenhorn

Joined: May 07, 2012
Posts: 7
Hi,
I am working on 64-bit windows7. I have installed JDK1.7.0. Have downloaded and unzipped the type 4 driver (sqljdbc4.jar) and placed it in the classpath. I am using SQL Server 2005 and its configuration enables TCP/IP at port 1433. My problem is when I execute the following code



I am getting the following exception. My database name is PhoneBook.

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused:
connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the p
ort, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at CheckDB.main(CheckDB.java:15)

I even tried it in netbeans, same error. When i tried to use a JDBC-ODBC driver with a system DSN 'PhoneBook' through the following code

The error changed to

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid string or buffer length
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.buildTypeInfo(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at CheckDB.main(CheckDB.java:15)

Somebody kindly help..!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Welcome to the Ranch!

The first error states it cannot connect to the SQL Server machine on your local machine. Are you sure one is running on it? Are you perhaps connecting to the wrong machine?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shubhra Jyotsna
Greenhorn

Joined: May 07, 2012
Posts: 7
thank you so much rob, but my SQL server is also running on my local machine itself. can you help where am I wrong?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
On port 1433?
Shubhra Jyotsna
Greenhorn

Joined: May 07, 2012
Posts: 7
Yes, i believe so. When i open 'SQL Server Configuration Manager', it has two links - 'SQL server 2005 Network Configuration (32 bit)' and 'SQL Native Client Configutaion (32 bit)', both indicates TCP/IP as 'Enabled', and under 'SQL Native Client Configutaion (32 bit)', we have 'Client Protocols', that says TCP/IP enabled and default port as 1433. Am I missing something? Your help is highly appreciated, as I am new to Windows 7 and SQL server 2005.
Shubhra Jyotsna
Greenhorn

Joined: May 07, 2012
Posts: 7
Actually I am having SQL Server Express installed on my system
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Have you tried checking if you are able to connect using the microsoft enterprise manager or query analyzer?

(P.S Its been a while since I have used MSSQL so I am not sure if these tools are still there )


Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Shubhra Jyotsna
Greenhorn

Joined: May 07, 2012
Posts: 7
Yes, I have done that. It is through 'SQL server Management Studio'. And from there, I can connect to the server, have created the database and can query it to get records. The problem is in the connection through JDBC. I even tried both a 32-bit and 64-bit ODBC DSN, but nothing works. I am sure I am missing something somewhere.
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Please check if this link could help you...

I have used SQL Express with no problem but that was 3 years ago...
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Can you telnet localhost on port 1433?


Swastik
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Try "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=PhoneBook". SQL Server Express installs a named instance called SQLEXPRESS. You can access that by replacing the port with a \ (escaped in a Java string) and the instance name.
Shubhra Jyotsna
Greenhorn

Joined: May 07, 2012
Posts: 7
Thanks everyone for their kind help. @Mark Reyes, I followed the link that you suggested and was able to resolve the problem related to port 1433. But, now there is another problem coming up. The error has changed to

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. The user is not associated with a trusted SQL Server connection.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2529)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1905)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1893)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4615)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1045)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at CheckDB.main(CheckDB.java:22)

Another one, can't find anything like 'SQL Server Configuration Manager' in SQL 2000.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

You need to specify a user name and password when you connect. Right now you don't - the user name is '' (empty).
Shubhra Jyotsna
Greenhorn

Joined: May 07, 2012
Posts: 7
Thanks Rob, I actually tried with the user 'sa' also



The error is still the same

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. The user is not associated with a trusted SQL Server connection.

I tried to associate the user 'sa' with a trusted SQL connection through the following steps

1. Connected to SQL Server using Windows Authentication.
2- Expanded Security folder in Object Explorer (in left side pane).
3- Then expanded Login Folder and located the 'sa' user. Right clicked sa and opened Properties.
4- Clicked 'Status' in the left pane of properties window.
5- In the Login options in the right pane, selected Enabled
6- In the 'General' page of these properties, erased the text in Password and Confirm password text boxes; and uncheck the Enforce password policy checkbox. This will let us login through 'sa' with empty password.

Now to Change Server Authentication, did the following

1- In Object Explorer: Right clicked the Server and opened Properties
2- Selected Security in the left pane of the properties window
3- On the right side, in Server Authentication, selected "SQL Server and Windows Authentication Mode"
6- Restarted the Server

Although the SQL server documentation doesn't recommends this mixed mode. I am still getting the same exception. Kindly let me know what I am missing now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with JDBC in Windows 7