Connect SQL Server Express 2005 with NetBeans IDE

Hi, Most of you Java Folks may have encountered situations where you wish to code using Java but need to connect to a Microsoft SQL Server Database. To an experienced person, this may seem trivial, but for newbies, this can be a troublesome task.

Through this post, I would briefly describe how to connect to Microsoft SQL Server 2005 Express Edition using NetBeans 6.0.1 IDE.

STEP 1: Download the relevant JDBC driver from the microsoft site.
(A search on the internet will more than suffice)

STEP 2: Add the driver to NetBeans IDE. This can be done as follows.

On the services tab, under Databases under Drivers, you can add a new driver as follows




On the Window, click Add and select the necessary Jar file of the driver.


Then click Find. It will search the class name and display it as follows



Once the driver is installed, you can right click on it and click 'connect using'



Then you will get the following screen



Fill in your database name and host address. The port is usually 1433.

You may try to connect after giving the correct parameters, but in some cases you may get an error saying you cant connect to the server. Under such cases, you may need to take the next step and assign a port manually.

STEP 3: Assigning a port manually

Go to Start->AllProgrammes->Microsoft SQL Server 2005->Configuration Tools ->SQL Server Configuration Manager

Then travel down to the following layer and doubl click on TCP/IP



After double clicking, you should get a similar picture.


Select 'IP Addresses ' tab and enter some port values to the respected fileds as shown. You can use any unassigned port values for this. But generally using a higher number than the well known ports is good.

Then you may need to restart your server.
STEP 4: Writing queries on connection
Once connection is established, you can write SQL statements and execute them via the IDE.

Now you can use SQL Server 2005 Express Edition for your Java Work :-)

Comments

Unknown said…
hi ruchira
i want to know what is host name and port number
ruchira said…
Hi Sahil,

Since the database server is a service, it can be run on one computer and others can access it via the network. That is usually the case. There is one Database Server for a specific task and others simply access it via the network.

But in most academic cases, you may have your computer running a Database Server and your application running both on the same physical machine.

When I reffer to host, I mean the address of the computer which the Database Server is running.

If you are accessing a Database server on your machine, then you would set the host as 'localhost' or IP as '127.0.0.1' (Both represent the same. That is your local machine).
But, if you are accessing a Database server on a remote computer on the network, you need to specify the IP of the remote machine. As an example 192.142.36.25, 171.25.24.25, 124.25.24.123

A single machine could host several servers. Eg: Database Server ,Web Server, SMTP server. So, in this case, if you want to connect with that machine to get a Database service, you must have a way to signal that you need to access the Database. This is done by running a specific service on a specific port. So, if you need to access a specific computer for a specific service, you identify the machine by the IP address and the service by the port number.

I hope this answers your question.
Unknown said…
Hi,
I can't connect to sql in java netbeans,when I enter username and password,and run the program,in output this message appear:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'elnaz'. The user is not associated with a trusted SQL Server connection.
BUILD SUCCESSFUL .
what should I do?what is username and password in this case mean?
cK said…
Hi,

my name is Karthick.C

Can you please explain how to connect MS sql server 2008 to Netbeans IDE 6.9
cK said…
Hi,

I tried connecting using the steps you mentioned but I am getting this error "TCSP/IP connection refused, check connection parameters and check that no firewall is blocking the port no 1433"

I tried both 1433 and 5530 ports you mentioned in example
dev said…
hi ruchira
i want to connect sql server 2005 in net beans 6.8 using JNDI
kindly help
Unknown said…
Hi Sir.
i want to know new database acess from ms sql server with netbeans ide7.2..


Now i m working with old database connection when i want connect new database in Struct-config.Xml file Then it is showing Nullpointer Exception...

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline