File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes SQL Database to Java using JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL Database to Java using JDBC" Watch "SQL Database to Java using JDBC" New topic
Author

SQL Database to Java using JDBC

Owen Martin
Greenhorn

Joined: Mar 05, 2010
Posts: 14
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
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

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

Joined: Mar 05, 2010
Posts: 14
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
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Owen Martin
Greenhorn

Joined: Mar 05, 2010
Posts: 14
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

Integrated security should be enough, if you only make sure the right DLL file is available.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQL Database to Java using JDBC
 
Similar Threads
Try/catch variable scope
connecting to Webtrends reporting DB using JDBC-ODBC
J2EE + MS SQL SERVER
Need help in Connecting my MySQL Database to JTable in Eclipse.
ClassNotFoundException after setting env variable