We help IT Professionals succeed at work.

JDBC Connection with SQL Server

chaiti_c
chaiti_c asked
on
My OS is Windows2000 Advanced Server I have SQL Server on my machine I have loaded jdk 1.4 in c:\java. I have set the CLASSPATH, JAVA_HOME and TOMCAT_HOME.
I want to access the data from a database in my SQL Server. I am unable to access the database from any Java program. jd.java is not getting compiled and is giving error.

The code is as follows-

import java.awt.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;
class jd {
public static void main(String args[]) {

String url = "jdbc:odbc:tt";
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection(url, "sa", "sa");
Statement stmt = con.createStatement();

stmt.close();
con.close();


}
}

The error is
jd.java:10: cannot access Connection
bad file name: c:\java\sql\Connection.class
class file contains wrong class: java.sql.Connection
Please remove or make sure it appears in the correct subdirectory of the classpath
Connection con;
^
1 error

I have expanded jdbc-3_0-pfd3-classes in c:. It has formed a directory sql in c:\java. Where am I going wrong in the set up
Please help as I am new to Java and cannot proceed due to this error
Comment
Watch Question

Commented:
You have declared the variable con twice! Also, you need try/catch blocks for your database statements.

public static void main(String args[]) {
   String url = "jdbc:odbc:tt";
   try {
     //  Connection con; <----------------- Remove this line
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   } catch (ClassNotFoundException) {
      System.out.println(exception);
   }

   try {
       Connection con = DriverManager.getConnection(url, "sa", "sa");
       Statement stmt = con.createStatement();
       stmt.close();
       con.close();
   } catch (SQLException e) {
       e.printStackTrace();
   }
}

Commented:
You don't have to expand the jdbc-3_0-pfd3-classes file. Just add it to your classpath.

Author

Commented:
No even if I am using con once the problem is occuring.

Do I give the file name in the CLASSPATH of the Environment variable of the file jdbc-3_0-pfd3-classes .

Then I am compiling from the root javac jd.java To run I would be using java jd. Although the patha are set I have to keep the program in c:\java\bin.

Commented:
Is this file jdbc-3_0-pfd3-classes a JAR file?
hi chaiti,

check your classpath.I mean is your rt.jar is set in your classpath and the remaining required jar files.And check your database username and password (is it sa and sa ????).

change your code like this
**********************************************************
 
import java.awt.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;

class jd {
public static void main(String args[]) {

String url = "jdbc:odbc:tt";
Connection con;


try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "sa", "sa");
Statement stmt = con.createStatement();
}
catch(ClassNotFoundException cnfe)
{
System.out.println("exception"+cnfe.toString());
}

try {
stmt.close();
con.close();
}
catch(Exception ee)
{
System.out.println("exception in closing connection"+ee.toString());
}

}
}
***********************************************************

If you have any queries please feel free to contact me
Asta CuTechnical consultant & graphic design
CERTIFIED EXPERT
Top Expert 2004

Commented:
Hopefully you've already been helped with this question, but thought you'd appreciate knowing this.  It would be great if you could bring this question to a conclusion, awarding the experts above who helped you with points or a comment to them on your status today.

WindowsUpdate has new updates for .NET users; Details follow - Microsoft .NET Framework
The .NET Framework is a new feature of Windows. Applications built using the .NET Framework are more reliable and secure. You need to install the .NET Framework only if you have software that requires it.

For more information about the .NET Framework, see http://www.microsoft.com/net. (This site is in English.)

System Requirements
The .NET Framework can be installed on the following operating systems:
Windows 98
Windows 98 Second Edition (SE)
Windows Millennium Edition (Windows Me)
Windows NT 4.0® (Workstation or Server) with Service Pack 6.0a
Windows 2000 with the latest service pack installed (Professional, Server, Datacenter Server, or Advanced Server)
Windows XP (Home Edition and Professional)
You must be running Internet Explorer version 5.01 or later for all installations of the .NET Framework.

To install the .NET Framework, your computer must meet or exceed the following software and hardware requirements:

Software requirements for server operating systems:
MDAC 2.6
Hardware requirements:
For computers running only a .NET Framework application, Pentium 90 mHz CPU with 32 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 mHz CPU with 128 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
Recomended software:
MDAC 2.7 is recommended.
Recommended hardware: For computers running only a .NET Framework application, Pentium 90 MHz CPU with 96 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 MHz CPU with 256 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.

How to use -> Restart your computer to complete the installation. No other action is required to run .NET Framework applications. If you are developing applications using the .NET Framework, you can use the command-line compilers or you can use a development environment, such as Visual Studio .NET, that supports using the .NET Framework.

How to uninstall
To uninstall the .NET Framework: Click Start, point to Settings, and then click Control Panel (In Windows XP, click Start and then click Control Panel.).
Click Add/Remove Programs.
Click Microsoft .NET Framework (English) v1.0.3705 and then click Change/Remove.
More here  http://www.microsoft.com/net/

The .NET topic is being considered for addition to our All Topics link soon, so this may interest you as well:
http://www.experts-exchange.com/newtopics/Q.20276589.html

EXPERTS POINTS are waiting to be claimed here:  http://www.experts-exchange.com/commspt/Q.20277028.html

":0)
Asta


Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for yongsing

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.