Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Connecting to SQLServer 2000 using JDBC

Sat Apr 07, 2007 2:59 pm

Connecting to SQLServer 2000 take from me 2 days until i discovered the problem,
I tried all the solutions on the net until i installed the Service Pack 3,
In fact the TCP server of SQL-Server 2000 was not working . u can check that using :
Code:
Telnet  localhost portnumber

in the command line

But after installation of Service pack3 finally TCP server work fine and i connected to the Database Server,So the first thing to do is to check the TCP server. Second if it not working try to install Service pack3 from Microsoft.

Here is the code to Open A connection to SQL-server 2000 using Microsoft JDBC
java code
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("Driver Exits !!!");

connection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://computer_name:port","username","password");

} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
catch (SQLException ex) {
ex.printStackTrace();
}




Re: Connecting to SQLServer 2000 using JDBC

Fri Jun 08, 2007 12:50 am

Thanks for Code

as JDK1.6 no need for Class.forName(..etc)

Re: Connecting to SQLServer 2000 using JDBC

Fri Jun 08, 2007 10:42 am

Xline wrote:Thanks for Code

as JDK1.6 no need for Class.forName(..etc)


But , I think we may need Class.forName to specify the Driver you are using :?: . For Example

For Mysql:
Code:
Class.forName("com.mysql.jdbc.Driver").newInstance();

For PostGrelsql :
Code:
Class.forName("org.postgresql.Driver").newInstance();


Re: Connecting to SQLServer 2000 using JDBC

Mon Jun 18, 2007 12:56 am

This Code learn you how to connect your java application with sql server using JDBC-ODBC:

Code for Database Connection in Java

java code
Import java.sql.*;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:WSD");
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("SELECT * from student");
statement.execute("insert into student values (1,12)");
}
catch (Exception e)
{
}


Notes :
WSD is the name of the connection name you made in the ODBC.
Line 7 is used if you want to execute a select statement and you can read data using string return = java code
result.getString("here you put the columns index you want to get its value");   // 1 as an example

To move the pointer of reading you write result.next();
Don't forget the import statement.
Line 8 is used to execute any non- select statement.
To make a connection name called "mydb" do the followings:
Control Panel -> Administrative Tools - >Data Sources (ODBC) -> add
Search for SQL Server then - > choose it and then click Finish
Write a name, description and name of your server - > name is -- and server is your SQL Server Name Then click next, then another next.
Change the default database to -- and choose your one , example : "NorthWind" then next and then finish.
You may test Data Source , then click ok.

Now, you made a Data Source that is connected to your database, for my example it is "NorthWind".
And now Line 7 and 8 must be from the tables in NorthWind and now you may change WSD in line 2 to be mydb.

Re: Connecting to SQLServer 2000 using JDBC

Mon Jun 18, 2007 5:38 pm

Thanks for your code zahar

Post a reply
  Related Posts  to : Connecting to SQLServer 2000 using JDBC
 need help in Sqlserver     -  
 Timeout error while updating record in SQLServer in ASP.NET     -  
 $2000 Per Month Typing From Home(TI ID 17507)     -  
 Problem to connect to Active Directory on windows 2000 serve     -  
 What is JDBC?!!!     -  
 JDBC     -  
 need help in jdbc with mysql     -  
 connecting to pc from mobile using bluetooth in java     -  
 Connecting to PC from mobile using bluetooth in java     -  
 Connecting two computers on a wirreless network.     -  

Topic Tags

Java JDBC