Connection to DB on MSSQL

Hi, guys. I no have a problem with my database application. I don't imagine how i can connect to db on ms sql server 2000, Just now I have downloaded an MSSQL JDBC Driver. Then How should i register,install this driver and can someone help me with the code, on how to connect to sql
[298 byte] By [er_nsta] at [2007-11-27 9:12:00]
# 1

The driver string for connecting to MSSQL Server is "jdbc:jtds:sqlserver://localhost:1433/tempdb".

If you know, how to connect to any database through jdbc, just replace the driver string. Also put the driver jar in your classpath. The default username for MSSQL Server is "sa" and there is not password.

For more on how to connect to a db through Jdbc, you can find documentations on sun's site.

First try yourself and post whatever you have tried.

vineet.mangala at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thanks, But how to load the driver Class.forName("?")

I have tried "com.microsoft.mssqlserver.SQLServerDriver"

it says

Class NotDefFound com.microsoft.mssqlserver.SQLServerDriver

And about the classpath. It is first time when i am dealing with JDBC, what do you mean by saying putting driver .jar in classpath

er_nsta at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Just now i have read about using jtds driver, i have downloaded that driver. Someone please tell me, what should be my next step?
er_nsta at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

> Thanks, But how to load the driver

> Class.forName("?")

> I have tried

> "com.microsoft.mssqlserver.SQLServerDriver"

> it says

> Class NotDefFound

> com.microsoft.mssqlserver.SQLServerDriver

> nd about the classpath. It is first time when i am

> dealing with JDBC, what do you mean by saying putting

> driver .jar in classpath

http://www.microsoft.com/downloads/details.aspx?familyid=07287B11-0502-461A-B138-2AA54BFDC03A&displaylang=en

The driver you are trying to load is Microsoft provided JDBC driver for SQL server 2000. You can download it at the link mentioned above.

For instructions on how to set classpath and use the driver, read http://support.microsoft.com/kb/313100

If you want to use the jTds driver, you need to add it to the classpath and use the appropriate driver class and connection url. Refer to the jTds documentation for that.

aniseeda at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Thanks, aniseed.

I have downloaded the driver and installed it. Also added msbase.jar, msutil.jar and mssqlserver.jar to the classpath. But there is some another error. Here is my code

/**

* @(#)Test.java

*

*

* @author

* @version 1.00 2007/6/29

*/

import java.sql.*;

public class Test {

static String jdbcDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

static String url = "jdbc:microsoft:sqlserver://universal:1433;DatabaseName=borej;User=sa;Password=";

static String SQLCreate =

"CREATE TABLE CONTACT_INFO ("+

"CONTACT_ID INTEGER NOT NULL PRIMARY KEY,"+

"FIRST_NAME VARCHAR(20) NOT NULL,"+

"MI CHAR(1) NULL,"+

"LAST_NAME VARCHAR(30) NOT NULL,"+

"STREET VARCHAR(50) NOT NULL,"+

"CITY VARCHAR(30) NOT NULL,"+

"STATE CHAR(2) NOT NULL,"+

"ZIP VARCHAR(10) NOT NULL"+

");";

public Test() {

try{

Class.forName(jdbcDriver);

}

catch(ClassNotFoundException e){

System.err.println(e.getMessage());

}

}

public void execute(String SQLCommand){

try{

Connection con = DriverManager.getConnection(url);

Statement stmt = con.createStatement();

stmt.execute(SQLCommand);

con.close();

}

catch(SQLException e){

System.err.println(e.getMessage());

}

}

public static void main(String[] args) {

Test test = new Test();

test.execute(SQLCreate);

}

}

ERROR is:

Couldn't establish socket.

Please tell me what is my fault, what I did wrong.

er_nsta at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Refer http://www.ftponline.com/special/sqlserver/dvohra/
dvohra09a at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
dvohra, thanksbut this reference didn't help me. What can I also do?I tried jtds.It gives an error Connect refused.What can i do else. Please someone help me.
er_nsta at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
> ERROR is: > Couldn't establish socket.Print the stack trace and post it here.
aniseeda at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

Hi, guys.

I have not found solution yet.

When I am trying with JTDS

Class.forName("net.sourceforge.jdbc.jtds.Driver")

DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/master;user=sa;password=") ;

I am getting IONetwork exception connection: Connection refused.

When I am trying on JDBC SQL Driver 2000

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

DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433","sa",""):

I am getting exception: Error establishing socket. Anyone please help me.

er_nsta at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
> I am getting exception: Error establishing socket.Did you print the stack trace? Could you post it here?
aniseeda at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11

Can u check ur connectionUrl anf Class.forName

try this out

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

connection=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;User=abc;Password=xyz")

n have to set classpath for SQLjdbc driver?

try the help web page tht is thr where u have installed/downloaded SQl JDBC drivers........

Good Luck...

hetal_giria at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12
thank you all, I have managed this problem,Problem was solved when i downloaded Service Pack 3 for MS SQL SERVER 2000.
er_nsta at 2007-7-12 21:58:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...