MSSQL database connection problem

Hi,

I got problems trying to connect to database using JDBC. Problem is following:

Code:

String sql ="select * from table";

Connection conn =null;

try

{

String url ="jdbc:sqlserver://dbserver:1433/databasename";

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

conn = DriverManager.getConnection(url,"usename","password");

}

catch(Exception e)

{

System.out.println(e);

}

Trying to run this, I get error:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

Now I think this tells that classpath is not set right, but I have commanded:

SET CLASSPATH = C:\path\to\sqljdbc.jar but it still won't work. Any ideas?

My database is MS SQL Server 2005 running on another server than my java application.

[1190 byte] By [Akulaaria] at [2007-11-27 9:43:40]
# 1
or you can put the jar file in the java foldere.g.C:\j2Se1.6\jre\lib\ext\mssql.jar;
Yannixa at 2007-7-12 23:49:21 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Thank you =)
Akulaaria at 2007-7-12 23:49:21 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> or you can put the jar file in the java folder

>

> e.g.

> C:\j2Se1.6\jre\lib\ext\mssql.jar;

Take care. As long as you know nothing about the running environment and the purposes of the Java program, don't suggest those nasty hacks.

The classpath used depends on the running environment created/used. The environment variable $classpath will only be used if you execute the Java class as an Java Application using java.exe in the operating system's command console. If you're executing it as a JAR file, then you need to add the path to the MSSQL JAR to the class-path entry of the manifest.mf file. If you're executing it inside an IDE, then you need to add the path to the MSSQL JAR to the Build Path of the project. Etcetera ..

BalusCa at 2007-7-12 23:49:21 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

>Take care. As long as you know nothing about the running environment and the purposes >of the Java program, don't suggest those nasty hacks.

I just made easy for him...

but if Akulaari is using windows the easiest way is to right click my computer

find environment variable button and edit classpath.. if your mssql.jar is located in

c:\mssql.jar, add ;c:\mssql.jar to your classpath. save!!!

Yannixa at 2007-7-12 23:49:21 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Akulaari already said that SET CLASSPATH didn't work for him. Or he was doing something wrong, or he was running the class as a JAR or in an IDE or another Java environment.
BalusCa at 2007-7-12 23:49:21 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
My purpose is to develop a java program, which uses database connection. After I finish my program, I'm actually going to use it on a server which this database is installed on. It would be great if I could compile my program so, that I would not have to copy any extra jar files on it.
Akulaaria at 2007-7-12 23:49:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
Clear.In production environment, add this JAR to the classpath of the appserver.In development environment, only for unit-testing, you can safely add this JAR to the classpath of your JRE. However I'd rather to use an IDE and just add the JAR to the project's build path.
BalusCa at 2007-7-12 23:49:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...