?
Solved

Hi, jdbc no active Connections issue...Thanks!

Posted on 2006-06-01
21
Medium Priority
?
419 Views
Last Modified: 2012-06-22
Hi, this is a pretty lengthy question (sorry!) b/c I tried to put in as much detail about my problem as I can.  I am totally new to JDBC and this server stuff, so please bear with me.  I am getting this error:
****************
at com.devx.tradingapp.model.Connect.getConnection(Connect.java:27)
at com.devx.tradingapp.model.Connect.displayDbProperties(Connect.java:44)
at com.devx.tradingapp.model.Connect.main(Connect.java:80)
Error Trace in getConnection() : [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
Error: No active Connection
****************

1.) I've created a basic class called 'Connect' (code listed below) that connects to database using jdbc
2.) I have installed SQL Server 2000 (cd is from 2001)
3.)I downloaded Microsoft SQL Server 2000 Driver for JDBC sp3 ( I also tried sp2 but still doesn't work).
4.)I have tried tips listed on "http://support.microsoft.com/default.aspx?scid=kb;en-us;313178" but it still doesn't work.
However,the only thing I didn't perform on this page was to ping my server.  I'm not really sure how to perform this b/c I don't know what my server name is.  I'm currently running everything on my local computer.  So do they mean 'ping localhost' or 'ping local'?  Currently when I open SQL Server Enterprise Manager, this is the hierarchy that I have: Console Root > Microsoft SQL Servers > (local)(Windows NT) > Now there is the Databases, Security...etc folders.
5.) I run this java application on Eclipse: Run --> Run As --> Java Application.  After I run this, I get error message listed above.
6.) My OS is Windows XP


Connect.java code is below.  Any instructions, comments, or sample code is greatly appreciated!

Thanks so very much for your help!
noijet


***** Connect.java Start *******
package com.devx.tradingapp.model;

import java.*;
public class Connect{
     private java.sql.Connection  con = null;
     private final String url = "jdbc:microsoft:sqlserver://";
     private final String serverName= "localhost";
     private final String portNumber = "1433";
     private final String databaseName= "Northwind";
     private final String userName = "username";
     private final String password = "password";
     // Informs the driver to use server a side-cursor,
     // which permits more than one active statement
     // on a connection.
     private final String selectMethod = "cursor";
     
     // Constructor
     public Connect(){}
     
     private String getConnectionUrl(){
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     
     private java.sql.Connection getConnection(){
          try{
               Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
               con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               e.printStackTrace();
               System.out.println("Error Trace in getConnection() : " + e.getMessage());
         }
          return con;
      }

     /*
          Display the driver properties, database details
     */

     public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try{
               con= this.getConnection();
               if(con!=null){
                    dm = con.getMetaData();
                    System.out.println("Driver Information");
                    System.out.println("\tDriver Name: "+ dm.getDriverName());
                    System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println("\nDatabase Information ");
                    System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println("Avalilable Catalogs ");
                    rs = dm.getCatalogs();
                    while(rs.next()){
                         System.out.println("\tcatalog: "+ rs.getString(1));
                    }
                    rs.close();
                    rs = null;
                    closeConnection();
               }else System.out.println("Error: No active Connection");
          }catch(Exception e){
               e.printStackTrace();
          }
          dm=null;
     }    
     
     private void closeConnection(){
          try{
               if(con!=null)
                    con.close();
               con=null;
          }catch(Exception e){
               e.printStackTrace();
          }
     }
     public static void main(String[] args) throws Exception
       {
          Connect myDbTest = new Connect();
          myDbTest.displayDbProperties();
       }
}

****** Connect.java End *******



0
Comment
Question by:noijet
  • 10
  • 10
21 Comments
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 80 total points
ID: 16811844
You have "3 ways" to solve this problem :

1. Create a DSN Less Connection so that you dont have to register your driver to the windows/control panel/odbc:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20276074.html

2. Or you can add the driver on your project's class-path(right-click project>properties>build-path>libraries tab) then run your same configuration.

3. Or you can register the driver to the windows/control panel/odbc and run your same configuration.

I'm going home now make sure you do everything on my comments, If you have questions I'll get back tomorrow thanks.
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16812541
Your database server is running?
Try to telnet it.
Run>cmd >telnet localhost 1433
What does it respone?
Don't use ODBC connection for java program because of inconveniences while you can use jdbc connection.
Phuoc
0
 

Author Comment

by:noijet
ID: 16816213
Hi everybody, thank you for your comments.  
Javatm:  I've already done option #2 you listed, I have msbase.jar, mssqlserver.jar, and msutil.jar, basically all the jar files that I got from the sql server 2000 driver install lib folder.  However, it still gives me the error message I posted.

phuocnh:  I tried to do 'telnet localhost 1433' but the screen said 'Connecting to localhost...' and then disappeared, what does this mean?

I have a older version of SQL Server 2000 on windows XP, could it be that it doesn't work with sp2 or sp3 drivers?
Also, is there any way to verify that I have put in the correct information ?, can some one give me step by step instructions on how to verify this on SQL Server 2000:

     private final String url = "jdbc:microsoft:sqlserver://";
     private final String serverName= "localhost";
     private final String portNumber = "1433";
     private final String databaseName= "Northwind";
     private final String userName = "username";
     private final String password = "password";

Thank you so much for everyone's help!
noijet
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:phuocnh
ID: 16816875
It means that SQL Server is running on that port.
 So, you should try to follow this thread
http://forum.java.sun.com/thread.jspa?threadID=419214
Phuoc
0
 

Author Comment

by:noijet
ID: 16816878
Hi phuocnh, I actually get a message after performing 'telnet localhost 1433', I get:
"Could not open connection to the host, on port 1433: Connect failed.

Thanks so much,
noijet
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16816966
Oh, the server isnot running on that port.
Dis you start the MS SQL server?
Phuoc
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16816993
If you have started it, please check your firewall if it enables that port.
Phuoc
0
 

Author Comment

by:noijet
ID: 16817278
Hi phuocnh, thank you for your response.  I see the 'green' arrow next to my server icon, so I assume it is running.  
Can you please show me how I can check my firewall if it enables that port and how to enable it if it is not?

Thanks so much,
noijet
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16817331
What os are your server running on?
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16817382
Xp. OK.
Start > Control Panel >Windows Firewall >Exceptions Tab > Add port
Name: Any thing you like
Port:1433
Select TCP
Press on "OK" to save it.
Now try to check again by
telnet local host 1433
if it respones the same
Please try
Run > Cmd > netstat -a
and copy the respone and patse here.
Phuoc


0
 

Author Comment

by:noijet
ID: 16817483
Hi phuocnh, I still cannot get the connection, here is the entire output:

C:\>telnet localhost 1433
Connecting To localhost...Could not open connection to the host, on port 1433: C
onnect failed

C:\>netstat -a

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    noijet:epmap         0.0.0.0:0              LISTENING
  TCP    noijet:microsoft-ds  0.0.0.0:0              LISTENING
  TCP    noijet:1031          0.0.0.0:0              LISTENING
  TCP    noijet:3306          0.0.0.0:0              LISTENING
  TCP    noijet:1026          0.0.0.0:0              LISTENING
  UDP    noijet:microsoft-ds  *:*
  UDP    noijet:isakmp        *:*
  UDP    noijet:4500          *:*
  UDP    noijet:9370          *:*
  UDP    noijet:ntp           *:*
  UDP    noijet:1900          *:*

C:\>

Thanks so much!
noijet
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16817663
No program is listening on port 1433 or 1434
noijet! I don't think your SQL Server is running. Please restart it!
Then try to use netstat command again to check if there is a program running on port 1433.
Phuoc
0
 

Author Comment

by:noijet
ID: 16817791
Hi phuocnh, I check on the little icon on the lower right hand side of desktop.  I open it and the "SQL Server Services Manager" pops up, it has a green arrow and the Start/Continue button is disabled.  I also have my SQL Server Enterprise Manager and Query Analyzer up too.  Does this mean my SQL Server is up, or is there another way?

Thanks,
noijet
0
 

Author Comment

by:noijet
ID: 16819499
Hi phuocnh, I actually talked with a friend, she also has the same problem, but when she is connecting to a remote machines sql server, there is no problem.  Did you ever try it on your own machine?

noijet
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16821550
What do you see when you execute the following command:
ping localhost
if your pc cannot resolve localhost into 127.0.0.1
please open your "hosts" file which locate at C:\WINDOWS\system32\drivers\etc
and add following line

127.0.0.1       localhost
then save it.
Test again.
Phuoc
0
 

Author Comment

by:noijet
ID: 16824549
Hi Phuoc, thanks for your relentless help.  This is the complete output that I get once I executed the command:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\ping localhost
*****************
Pinging noijet [127.0.0.1] with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\
**********************

Thanks so much,
noijet
0
 
LVL 6

Accepted Solution

by:
phuocnh earned 452 total points
ID: 16825337
I am using SQL Server 2005 Express Edition. When I installed it,I cannot asscess it because it disabled TCP/IP protocol in default. JBDC connection need TCP/IP protocol.
To enable TCP/IP protocol in Microsoft SQL Server 2005 Express Edition, I did as a following "recipe":
Start >All Programs >Microsoft SQL Server 2005 > Configuration Tools >SQL Server Configuration Manager
Then "SQL server configuration manager " window appears. Under "SQL Server 2005 Network Configuration" select "Protocol for SQLEXPRESS" and double click on "TCP/IP" item on the right panel. "TCP/IP properties" dialog appears:
+Protocol tab: Enabled property, select "Yes".
+IP Addresses tab: IP All group, TCP Port property, please enter "1433".
Press "OK" button to save configuration and close "TCP/IP properties" dialog.
Now restart your "Microsoft SQL server" service.
Please test it by:
telnet localhost 1433
If a black windows appear.It is ok.
Press "Ctrl+]" the enter "Quit" press "Enter".
Now you can use JBDC with SQL Server.
This is comment for Microsoft SQL server 2005 Express Edition , I think you can do the same with Microsoft SQL Server 2000.
Phuoc

0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16825350
When you use command "netstat -a" in CMD windows, you must see the following line:
 TCP    <your hostname>:ms-sql-s    <your hostname>:0           LISTENING
The SQL Server is listening on port "ms-sql-s".
Phuoc
0
 

Author Comment

by:noijet
ID: 16832446
Hi Phuoc, thanks for your reply, I'm going to go home and try this out on my machine.  I'll get right back to you after I have done so.

Thanks,
noijet
0
 

Author Comment

by:noijet
ID: 16849196
Hi Phuoc, I don't seem to have a Configuration Tools section in my sql server 2000 install, I'm going to look for another way to get to TCP/IP configuration. I'll get back to you.

Thanks,
noijet
0
 

Author Comment

by:noijet
ID: 16898243
Hi Phuoc, thanks so much for your great help!  I want to reward you points first as I work on a different project, I will post a new comment if I find new results!

Thanks,
noijet
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question