Results 1 to 6 of 6
  1. #1
    ace84 is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Exclamation 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();
          }
    }
    	}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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.

  3. #3
    ace84 is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    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 given
    Last edited by ace84; 02-03-2011 at 06:33 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,854
    Rep Power
    19

    Default

    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.

  5. #5
    ace84 is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks for you help.

    Connect:
    Java Code:
    String dbURL = "jdbc:sqlserver://MAIN\\sqlexpress:1433;DatabaseName=DB_Accounts";
    I can ping the server successfully and when I type the address into the 'run' command, it takes me there.

    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]

  6. #6
    providence is offline Member
    Join Date
    Oct 2010
    Location
    Canada
    Posts
    20
    Rep Power
    0

    Default

    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

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Connect Four
    By chick in forum New To Java
    Replies: 3
    Last Post: 05-04-2010, 04:33 AM
  3. Connect 4
    By pansylea in forum New To Java
    Replies: 0
    Last Post: 12-11-2009, 03:36 PM
  4. connect to ftp
    By anki.gulsvart in forum Networking
    Replies: 21
    Last Post: 10-20-2009, 08:19 PM
  5. Replies: 1
    Last Post: 08-06-2007, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •