Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2003
    Posts
    9

    Unanswered: JDBC and SQL Server

    I am trying to write a jsp that interfaces with sql server 2000 and when I try to connect I keep getting this message:
    Exception: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    I checked the server utility and TCP/IP is enabled and it is listening on port 1433. Anyone have any suggestions for me?
    I am doing this on my home computer using dyndns dynamic host, I don't know if that makes a difference.

  2. #2
    Join Date
    Jun 2003
    Location
    Shanghai, China
    Posts
    8

    !

    Which web server you are using?
    And how you code?Pls tell me!

  3. #3
    Join Date
    Jun 2003
    Posts
    9
    Hi, thanks for replying, I am using Tomcat and here is the jsp code I am using:

    <html>
    <head>Test Connection
    <title>Test Connection Page</title>
    </head>

    <%@ page import="sun.misc.BASE64Decoder" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.util.*" %>

    <%
    if(request.getParameter("btnLogin") != null)
    {
    String authorization = request.getHeader("Authorization");
    if (authorization == null)
    {
    response.setStatus(response.SC_UNAUTHORIZED); // Ie 401
    response.setHeader("WWW-Authenticate",
    "BASIC realm=\"privileged-few\"");
    }
    else
    {
    String userInfo = authorization.substring(6).trim();
    BASE64Decoder decoder = new BASE64Decoder();
    String nameAndPassword =
    new String(decoder.decodeBuffer(userInfo));
    int index = nameAndPassword.indexOf(":");
    String user = nameAndPassword.substring(0, index);
    String password = nameAndPassword.substring(index+1);

    //get user and password from database

    try
    {


    out.println("before for name");
    Class.forName("com.microsoft.jdbc.sqlserver.SQLSer verDriver");
    out.println("after for name");
    Connection connection = DriverManager.getConnection(
    "jdbc:microsoft:sqlserver://hostname:1433", "user",
    "pw");

    out.println("here");

    if (connection != null) {
    out.println("connected");
    }
    else {
    out.println("didn't connect");
    }
    connection.close();
    }
    catch (Exception e)
    {
    System.out.println("Exception: " + e);
    }

    }
    }

    %>

    </html>

  4. #4
    Join Date
    Jun 2003
    Location
    Shanghai, China
    Posts
    8

    !

    Connection connection = DriverManager.getConnection(
    "jdbc:microsoft:sqlserver://hostname:1433", "user",
    "pw");


    Your sever name is hostname???By the way, how you connect your SQL server ?TCP/IP?PipeName?
    Just a suggestion!Try to use the ip address in the server name of your URL!

  5. #5
    Join Date
    Feb 2002
    Location
    North Wales, UK
    Posts
    114
    I'm assuming SQL server is running on the same box tomcat is?
    J^ - web | email
    newsASP Developer

  6. #6
    Join Date
    Jun 2003
    Posts
    9
    yes, it is running on the same box
    I just have one computer running win xp and should the server name be the name of my computer

  7. #7
    Join Date
    Feb 2002
    Location
    North Wales, UK
    Posts
    114
    Yes or you can simply enter localhost
    J^ - web | email
    newsASP Developer

  8. #8
    Join Date
    Jul 2003
    Location
    Colombia
    Posts
    9

    Re: JDBC and SQL Server

    Originally posted by tscottw
    I am trying to write a jsp that interfaces with sql server 2000 and when I try to connect I keep getting this message:
    Exception: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    I checked the server utility and TCP/IP is enabled and it is listening on port 1433. Anyone have any suggestions for me?
    I am doing this on my home computer using dyndns dynamic host, I don't know if that makes a difference.
    Hi!, look I'm getting the same Exception but in a client-standalone application, so please if anyone can help I'd appreciate it.

    Here's the Exception message and the source code:

    Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    at com.microsoft.jdbc.base.BaseExceptions.createExcep tion(Unknown Source)
    at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown Source)
    at com.microsoft.jdbc.base.BaseExceptions.getExceptio n(Unknown Source)
    at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<in it>(Unknown Source)
    at com.microsoft.jdbc.sqlserver.SQLServerImplConnecti on.open(Unknown Source)
    at com.microsoft.jdbc.base.BaseConnection.getNewImplC onnection(Unknown Source)
    at com.microsoft.jdbc.base.BaseConnection.open(Unknow n Source)
    at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager .java:512)
    at java.sql.DriverManager.getConnection(DriverManager .java:193)
    at Test.<init>(Test.java:17)
    at Test.main(Test.java:38)

    -- Source Code
    import java.sql.*;
    /**
    * Microsoft SQL Server JDBC test program
    */
    public class Test {
    public Test() throws Exception {
    // Get connection
    DriverManager.registerDriver(new
    com.microsoft.jdbc.sqlserver.SQLServerDriver());
    Connection connection = DriverManager.getConnection(
    "jdbc:microsoft:sqlserver://MORPHEUS:1433");
    if (connection != null) {
    System.out.println();
    System.out.println("Successfully connected");
    System.out.println();
    // Meta data
    DatabaseMetaData meta = connection.getMetaData();
    System.out.println("\nDriver Information");
    System.out.println("Driver Name: "
    + meta.getDriverName());
    System.out.println("Driver Version: "
    + meta.getDriverVersion());
    System.out.println("\nDatabase Information ");
    System.out.println("Database Name: "
    + meta.getDatabaseProductName());
    System.out.println("Database Version: "+
    meta.getDatabaseProductVersion());
    }
    }// Test
    public static void main (String args[]) throws Exception {
    Test test = new Test();
    }
    }

  9. #9
    Join Date
    Feb 2002
    Location
    North Wales, UK
    Posts
    114
    Have you tried using 'localhost' instead of 'MORPHEUS' ?
    J^ - web | email
    newsASP Developer

  10. #10
    Join Date
    Jul 2003
    Location
    Colombia
    Posts
    9
    Originally posted by JonathanB
    Have you tried using 'localhost' instead of 'MORPHEUS' ?
    Yes, but still it doesn't work, I've even tried with the database name in the connection string but nothing happenned. Thanks for the help, if u have any other suggestion please let me know about it

  11. #11
    Join Date
    Jul 2003
    Location
    Colombia
    Posts
    9

    Thumbs up Re: JDBC and SQL Server

    Originally posted by tscottw
    Exception: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    look have u tried to stablish connection to your DB in a command line?, try this command:
    telnet <HOST> 1433

    if it doesn't throws any error of connection this means you have the TCP/IP running, if it gives u an error, it means that the server doesn't have enabled the protocol, you HAVE TO enable it through the Enterprise Manager directly through the properties of the server.

    Hope it helps... worked for me

Posting Permissions

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