• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Database to Java using JDBC

 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I am a beginner but am currently trying to connect a SQL database I have made in SSMSE using SQL Server 2005 with my Java front end in the Eclipse IDE.

Firstly I see many tutorials for mySQl using JDBC but not SQL, is there a reason for this or are they much of the same?

I started off by downloading the driver, which I thought was the right one; http://www.microsoft.com/download/en/details.aspx?id=21599

And to test it I wrote the following code;



This gives me an exception("com.sql.jdbc.Driver")

I think I might have the driver name wrong, but what specifically should it be called as its just a .jar file called sqljdbc4

Thanks?
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDBC is the Java technology for connecting Java to SQL-based databases. SQL is the standardized language ("Structured Query Language") which programmers use to access those databases. There are a variety of SQL-based databases available -- you chose SQL Server 2005 but there are many others, notably Oracle's databases, MySQL, Postgres, and so on.

As far as your program is concerned, your first error was to process the exception by discarding most of its information and substituting your own. As a result you don't understand what it's trying to tell you. So replace your catch-clause by this:


As for the possible reason for the exception, it's possible that you used the wrong class name. The documentation which comes with the JDBC driver will tell you the correct class name to use. It's also possible you just failed to put the JDBC driver jar into the class path.
 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply that has helped.

However I am not getting this error, but I have went into the Configuration Manager and set TCP/IP to be enabled and disabled the firewall;

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 port, 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 DatabaseConnect.main(DatabaseConnect.java:20)

That is after using the connection String;



?

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And did you verify that the database server was in fact listening on port 1433? The error message says it wasn't.
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SQL Server may not allow TCP connections out of the box. Open the "SQL Server Configuration Manager", open the "SQL Server 2005 Network Configuration" tree node on the left (or 2008), select "Protocols for MSSQLSERVER" and double click on "TCP/IP" on the right. Make sure it's enabled. Also make sure that the port is 1433 (the default). If the port is different you need that in your connection String. For example: "jdbc:sqlserver://localhost:1234;databaseName=MyDatabase;integratedSecurity=true;". Note that, if I recall correctly, you need to include "sqljdbc_auth.dll" on your library path for integrated security to work.
 
Owen Martin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't find out which port it was using so I manually set it to 1433 and thats that sorted.

Butt...I use Windows User Authentication, how do I use that in the connection string?
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integrated security should be enough, if you only make sure the right DLL file is available.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic