• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with JDBC in Windows 7

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Shubhra Jyotsna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much rob, but my SQL server is also running on my local machine itself. can you help where am I wrong?
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On port 1433?
 
Shubhra Jyotsna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I am having SQL Server Express installed on my system
 
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Shubhra Jyotsna
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check if this link could help you...

I have used SQL Express with no problem but that was 3 years ago...
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you telnet localhost on port 1433?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic