Results 1 to 6 of 6
Thread: Connect Problems
- 02-01-2011, 09:23 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
Connect Problems
I am trying to connect to a sql server database.
I have tcp/ip enabled.
I keep on getting the following error message:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dc.main(dc.java:16)
My Code:
Java Code:import java.sql.*; public class dc { public static void main(String[] args) { String myuser = "admin"; String mypass = "pwd"; String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:sqlserver://localhost\\SQLEXPRESS;user=myuser;password=mypass;"; // Connect the server and the database Connection dbConn; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL,myuser,mypass); System.out.println("Connection Successful!"); // if successful print Connection Successful! } catch (Exception e) { e.printStackTrace(); } } }
- 02-01-2011, 09:37 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,641
- Rep Power
- 10
You must add the driver to your classpath. Search the forums, as this question has been asked and answered many times and you can probably get some pretty good solutions and descriptions of the problem in those older posts.
- 02-03-2011, 05:03 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
I added the driver, thanks.
There are two things that I am having trouble understanding:
1. How to connect to the server. I do not know if I should use localhost or not? (I am linking to a database that is not on my local machine, if that matters)
2. How to connect to the correct database, as there are a couple of databases under that server
This is what I have so far:
MAIN -> My Server
1433: TCP/IP Port
DB_Accounts -> My Database I want to connect to
String dbURL = "jdbc:sqlserver://MAIN\\SQLEXPRESS:1433/DB_Accounts;user=myuser;password=mypass;"; // Connect the server and the database
thanks for any help givenLast edited by ace84; 02-03-2011 at 06:33 PM.
- 02-04-2011, 08:40 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 25
I think it's this:
"jdbc:sqlserver://MAIN:1433;DatabaseName=DB_Accounts"
You supply the username and password to the getConnection() method.
It's one of those things I always have to try out, but that's the basic format.
- 02-04-2011, 06:59 PM #5
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
Thanks for you help.
Connect:
Java Code:String dbURL = "jdbc:sqlserver://MAIN\\sqlexpress:1433;DatabaseName=DB_Accounts";
After I made that changed, I now get the following error:
[/CODE]
The TCP/IP connection to the host MAIN, 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.".
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host MAIN, 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.ma keFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.c onnectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.l ogin(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.c onnect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.conne ct(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
[/CODE]
- 02-05-2011, 01:24 AM #6
Member
- Join Date
- Oct 2010
- Location
- Canada
- Posts
- 20
- Rep Power
- 0
Is the server connected to the internet via a router? If so, make sure you have forwarded port 1433. I'm not sure about all routers, but I know on a linksys you also have to specify the IPv4 address that you want to forward the ports to. You can get this by typing ipconfig/all into a command line and scrolling down until you find the 'IPv4 Address' bit under the 'Wireless LAN Adapter...' heading. It should be in the format 192.168.1.xxx.
I had this error before and that was the solution. Hope it works in your case, these connection issues can be so frustrating they make you yearn for the coming frustration of debugging the program!Last edited by providence; 02-05-2011 at 01:26 AM.
Similar Threads
-
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
Connect Four
By chick in forum New To JavaReplies: 3Last Post: 05-04-2010, 04:33 AM -
Connect 4
By pansylea in forum New To JavaReplies: 0Last Post: 12-11-2009, 03:36 PM -
connect to ftp
By anki.gulsvart in forum NetworkingReplies: 21Last Post: 10-20-2009, 08:19 PM -
Problems when I try to connect to data base
By gabriel in forum JDBCReplies: 1Last Post: 08-06-2007, 05:38 PM
Bookmarks