JDBC Installation

searchcoilΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 3 μήνες)

278 εμφανίσεις

Islamic University

of Gaza (IUG)

Faculty of Engineering

Computer Engineering Department

Database Lab.

ECOM 4113









Accessing SQL Server 2005

on

NetBeans using JDBC









Prepared by



Eng.

Eman R
.
Al
-
Kurdi











November 22, 2010

Step
-
by
-
Ste
p Guides:

Be sure of the following
configurations

:

1
-

G
o to start
----
all program
---
SQL server
---

Configuration tools

SQL server Configuration Manager

2
-

C
lick on the SQL server services
----

be sure that the SQL browser
and the SQL Server instance are running

3
-

C
lick on the SQL network configuration

click on the protocol for
SQL Server.

4
-

C
lick on the TCP/IP and enable it.

5
-

R
eturn to SQL server servi
ces and stop the SQL server

then start it
again
.

Accessing SQL Server on NetBeans:

1.

Extract Microsoft SQL Server 2005

JDBC Driver on C:
\
Program
Files



Then, you’ll see the folder that you’ve just extracted (
\
Microsoft SQL Server 2005 JDBC Driver
\
sqljdbc_1
.0
\
enu
\

),
the file ’sqljdbc.jar’ which is the library


that will be added to the library in NetBeans later.


2.

Add JDB
C Driver to the project on NetBeans. (Add a library)

I will show by create New Java Application Project called
TestSQL and add ’sqljdbc.jar’ that just get from previous step to
the project’s library.









Create New Project called TestSQL.





















I
i
n Projects window, right click the project name and select
Properties.
























3.

Project Properties window appears. The Categories on left side,
select Libraries. And on right side in Compile tab, click Add
JAR/Folder.




New Window app
ears, browse to the file ’sqljdbc.jar’ and
click open.



You’ll see the .jar file was added to the project. Click OK to finish.


4.

Connect to the database


Now it’s the coding time. I
am
going to show how to connect to
SQL Server. A
ssume that I have
SQL Serve
r Management Studio
Express

running on local machine. Let continue from the project
just created in previous step, in main.java.



T
o use Connection and DriverMapper Classes
,

you

need to import
libraries.

import java.sql.*;




The code explanation:



Class.for
Name(”com.microsoft.sqlserver.jdbc.SQLServerDriver
”); means load the SQL Server driver.



localhost: refers to connect to SQL Server at localhost
(your pc).



databaseName:

refers to the database name you want to connect
to.



user and password refer Username a
nd Password that used to
connect to the SQL Server.


Compile and run the project. If no error occurs, it means that the
connection has established successfully.