Database Connection in SQL Server 2000

Hi All!

I would like to ask your expertise regarding JDBC-MS SQL Server 2000.

I have here a sample code in connecting to SQL Server 2000, AgriMrpPrd Database using the account "sa" and the password "092802". I have tried running the code in my local server at home and It successfully runs, but when I tried running it on my local server in the office, I was not able to create a connection object though I already have changed the HOST, PORT, DATABASE, USERNAME, and PASSWORD values.

I have tried logging in using my local account, removed the LAN connection, start my local server and tried connecting to it but still it does not work.

I have tried turning the firewall OFF and try reconnecting but it there was no improvement.

I do not know if there is a difference if there is my computer is on a DOMAIN (althought I am only connecting to my local server).

Source Code:

import javax.swing.*;

import java.sql.*;

import com.microsoft.jdbc.sqlserver.*;

public class SQLServerConnection{

public String un, pw, db, host, port, url, driver;

public Connection conn=null;

public void connectDB(){

String msg="",title="MS SQL Server - JDBC:ODBC Connection";

int msgType=0;

driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";

host="206.101.216.95";

port="1433";

un="sa";

pw="092803";

db="AgriMrpPrd";

url="jdbc:microsoft:sqlserver://" + host + ":" + port + ";DatabaseName=" + db;

try{

Class.forName(driver);

conn=DriverManager.getConnection(url,un,pw);

System.out.println("Connected to Database!");

}catch(Exception ex){

msg="Unable to connect to MS SQL Server!";

msgType=JOptionPane.ERROR_MESSAGE;

JOptionPane.showMessageDialog(null,msg,title,msgType);

System.exit(0);

}

}

public static void main(String args[]){

SQLServerConnection ssc=new SQLServerConnection();

ssc.connectDB();

}

}

Please give me wisdom in handling this problem...

Take care always and God blesss

MADz

[2142 byte] By [khickyphutza] at [2007-11-26 16:33:17]
# 1

> Hi All!

>

> I would like to ask your expertise regarding JDBC-MS

> SQL Server 2000.

>

> I have here a sample code in connecting to SQL Server

> 2000, AgriMrpPrd Database using the account "sa" and

> the password "092802". I have tried running the code

> in my local server at home and It successfully runs,

> but when I tried running it on my local server in the

> office, I was not able to create a connection object

> though I already have changed the HOST, PORT,

> DATABASE, USERNAME, and PASSWORD values.

If the code runs in one place and not the other, it's got to be a problem with seeing the host.

> I have tried logging in using my local account,

> removed the LAN connection, start my local server and

> tried connecting to it but still it does not work.

>

> I have tried turning the firewall OFF and try

> reconnecting but it there was no improvement.

>

> I do not know if there is a difference if there is my

> computer is on a DOMAIN (althought I am only

> connecting to my local server).

I have no idea how to fix your problem, but there are several things to comment on about your code:

(1) You don't have to import the microsoft package. It's actually a bad idea.

(2) I'd name this class DbConnection, because if you wrote it properly it might have a chance of being useful wih other databases, too.

(3) You hardwire all the connection information. Better to pass it in so changing the database doesn't require a recompile.

(4) It's bad to mingle Swing and database code. Take those JOptionPane calls out.

(5) Print the stack trace out when you catch an exception. You're losing a lot of information by handling it that way.

%

duffymoa at 2007-7-8 22:57:59 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Hi.

beside of what has been written before...

check the following list:

- is the database on server listen to tcp/ip at all

(i had some installations where tcp/ip was not enabled for SQL Server)

- is the database on server listen to tcp/ip at port 1433

- does the SQL Server allows SQL Authentication

If you not know how to check this list, ask the DB-Admin.

>

> Please give me wisdom in handling this problem...

>

Here are some words addressing your prayer ;-)

JDBC has nothing to do with ODBC, so the title of your message is misleading.

i perfer the jtds driver to access MS-SQL databases

( jtds.sourceforge.net )

Franco

franco_weichela at 2007-7-8 22:57:59 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Just a follow up to this question...

I forgot to check the version of Windows that I tried the said sample program.

The program run on Windows XP Pro SP1 (Connected to SQL Server 2000) using the driver downloaded from Windows.com.

But, In Windows XP Pro SP2, I cannot connect to SQL Server and it displays and Exception that says Cannot Establish Socket Connection something...?br>

I have downloaded and tried the SQL Server 2000 JDBC Drivers from microsoft site (SP1, SP2, and SP3) but all of these drivers are not working on Windows XP SP2. The only way for me to establish a connection to SQL Server 2000 is to use ODBC connection.

Another thing, the sample program i posted is only for testing purposes and it was just used for the purpose of testing only?

I would like to ask now if you guys have a JDBC driver that will allow me to create connection for SQL Server 2000 to Windows XP Professional Service Pack 2.

khickyphutza at 2007-7-8 22:57:59 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
take a look at here http://www.akadia.com/services/sqlsrv_jdbc.htmlgood luck
gerekbolsaa at 2007-7-8 22:57:59 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...