| Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection |
Introduction
This tutorial show you how to use NetBeans to connect SQL Server (2000 and 2005) by using Microsoft SQL Server JDBC Driver.
I’ll divide into 3 parts:
- Part I : Create a connection
This part which you’re reading shows about how to establish a connection between NetBeans and SQL Server. In this example, I use SQL Server 2000 SP4 and NetBeans IDE 5.5 - Part II : Perform SQL Operations
This part show how to perform some basic operations from NetBeans with SQL Server. For instance, send querys as SELECT, INSERT, UPDATE to the database. - Part III: Troubleshooting
The last part is about problems and how to fix them.
Requirements
- Microsoft SQL Server JDBC Driver
To get latest version, visit Microsoft SQL Server 2005 JDBC Driver. - NetBeans with JRE (Java Runtime Environment) version 1.4 or later
You can find at NetBeans.org.
Step-by-Step Guides
- Installation
- Install NetBeans.
- Download Microsoft SQL Server 2005 JDBC Driver, name ’sqljdbc_1.1.1501.101_enu.exe’. (The file name may differs depends on the version if you’ve downloaded from the Official Site.)
- Double-click sqljdbc_1.1.1501.101_enu.exe to extract it.
- Then, you’ll see the folder that you’ve just extracted ( \Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.1\enu\ ), the file ’sqljdbc.jar’ which is the library that will be added to the library in NetBeans later.

- You can find more informations about JDBC Driver at \Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.1\enu\help\default.htm.
- Add JDBC 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.

- In Projects window, right click the project name and select Properties.

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

- New Window appears, browse to the file ’sqljdbc.jar’ and click Open.

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

Note: You should keep sqljdbc.jar in the directory that you won’t delete it (ex. not in temp folder). May be in the same directory that keep common library files. If you delete the file without delete a link from the project, the project will show error about missing library.
- Create New Project called TestSQL.
- Connect to the database
- I’m going to use Connection and DriverMapper Classes so I need to import libraries.
import java.sql.*;

- Now I will connect to my SQL Server on local machine, the Northwind database(a sample database in SQL Server 2000). In main method, add the following code.
try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=Northwind;user=sa;password=123456;"; Connection con = DriverManager.getConnection(connectionUrl); } catch (SQLException e) { System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: "+ cE.toString()); }
The code explanation:
- Class.forName(”com.microsoft.sqlserver.jdbc.SQLServerDriver”); means load the SQL Server driver.
- localhost:1433 refers to connect to SQL Server at localhost port 1433 (default port).
- databaseName=Northwind refers to the database name you want to connect to.
- user and password refer Username and 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.
Now it’s the coding time. I going to show how to connect to SQL Server. Assume that I have SQL Server 2000 running on local machine. Let continue from the project just created in previous step, in main.java.
That’s it for part I. Now you know how to connect to SQL Server on NetBeans, the part II will coming soon.



























April 28th, 2007 at 10:26 am
hey!,Did you have article about netbeans connect to mysql?
July 2nd, 2007 at 9:31 pm
Very Good.
When we get the others parts of tutorial.
Thanks.
July 5th, 2007 at 9:29 pm
I’ll write others soon. May be next week.
July 9th, 2007 at 9:55 pm
Ok. Thanks for your good work.
August 17th, 2007 at 4:41 pm
the page wa usefull
August 18th, 2007 at 1:25 pm
Thank you so much. This was so helpful. This type of information is invaluable.
August 24th, 2007 at 2:54 am
hI
Thanks for your blog.
I´ve a question. I did all you say to, but I ´ve got this message: SQL Exception: java.sql.SQLException: No suitable driver
I dont´knoew why. I used driver 1.0 and I fallowed yours steps. Please can yoy tell me what could be?
This is the code:
/*
* Main.java
*
* Created on 16 de agosto de 2007, 04:38 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication1;
import java.sql.*;
/**
*
* @author maricelys
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
Class.forName(”com.microsoft.sqlserver.jdbc.SQLServerDriver”);
String connectionUrl = “jdbc:sqlserver:\\sidsvr_001;databaseName=siim_chacao_modelo;user=sa;password=”;
Connection con = DriverManager.getConnection(connectionUrl);
System.out.println();
}catch (ClassNotFoundException cE) {
System.out.println(”Class Not Found Exception: “+ cE.toString());
}catch (SQLException e) {
System.out.println(”SQL Exception: “+ e.toString());
}
}
}
Thanks
August 24th, 2007 at 11:41 pm
I think you typed incorrect connectionUrl string.
It should be in this format “jdbc:sqlserver://serverName”.
Try to change ‘jdbc:sqlserver:\\sidsvr_001′ to ‘jdbc:sqlserver://sidsvr_001′.
And let me know the result,too.
September 5th, 2007 at 12:36 am
Hi, thanks!
I also had tu put the SQLJDBC_AUTH.DLL in the bin from the jdk.
Thank you very much. Goed work.
Grettings!
October 30th, 2007 at 6:58 am
Hello… I am having problems with connecting NetBeans 5.5.1 with a SQL Server 2005 database.
I have this in the imports section:
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
The second import is recognized well, meaning that the SQL Server classes are correctly reachable (I have added the JAR file to the library of the project)
When I write in code: Class.forName(”com.microsoft.sqlserver.jdbc.SQLServerDriver”);, NetBeans immediately says throws a classNotFoundexception.
What is wrong?
Thanks
Jaime
October 31st, 2007 at 7:12 pm
To Jaime,
I think your library file (sqljdbc.jar) may be corrupted, try to download and add the library file on NetBeans again.
And on this line, “import com.microsoft.sqlserver.jdbc.*;”
Try to change to “import com.microsoft.sqlserver.jdbc.SQLServerDriver;” and check that NetBeans show any error message on that line or not.
If the problem persists, you can mail your source code and the captured error message to me.
December 8th, 2007 at 11:06 pm
the program produced that error
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
December 8th, 2007 at 11:12 pm
You need to enable TCP/IP on the SQL Server to accept connection.
December 8th, 2007 at 11:19 pm
I am using SQL 2000 and i don’t know how to enable TCP/IP on the SQL Server
December 8th, 2007 at 11:21 pm
its enabled and the port is 1433
December 9th, 2007 at 12:52 am
I’m not sure about SQL Server 2000.
Try read this page about SQL Server JDBC Connection Errors at
http://www.websina.com/bugzero/errors/sql-server-connection-error.html
There are many errors and how to solve that problems including SQL Server 2000.
January 18th, 2008 at 2:05 pm
Y try This code into my netBeans 6 but in debug mode I look that line Class.forName(”com.microsoft.sqlserver.jdbc.SQLServerDriver”); view ERROR !!!!!
January 21st, 2008 at 10:58 pm
I haven’t test on NetBeans 6.
What’s the error message?
January 30th, 2008 at 11:04 am
To David Chacon,
I have tested on NetBeans 6 with the same code. It works fine. If you still got error on that line, may be you miss to add JDBC library.
January 30th, 2008 at 12:33 pm
I use NetBean 6 and try to use Database services with SQL Server 2005 JDBC Driver. my Database URL: jdbc:sqlserver://LocalHost:1433;databaseName=pubs.
It end up with error Unable to add connection. Cannot establish a connection to jdbc:sqlserver://LocalHost:1433;databaseName=pubs using com.microsoft.sqlserver.jdbc.SQLServerDriver (The TCP/IP connection has failed. javanet.ConnectionException: Connection refused: connect)
I checked TCP/IP and server already run
January 31st, 2008 at 4:25 pm
Can you show me your complete connection string? and what database service you try connecting to? SQL Server 2000?
If you’re connecting to SQL Server 2000, try to visit the URL in 16th comment. The error message quite the same in that page.
February 20th, 2008 at 8:03 am
I’m also using Netbeans 6.0.1 and can’t get rid of the
“java.language.ClassNotFoundException”
package SqlServerCon;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Class.forName(”com.microsoft.sqlserver.jdbc.SQLServerDriver”);
}
}
In the prject window libraries listing is sqljdbc.jar; I can browse down the full tree to com.microsoft.sqlserver.jdbc.SQLServerDriver
BTW Netbeans tells me the import is unused.
help!
February 20th, 2008 at 8:26 am
One other maddening thing - I can connect to the database using the database services inside Netbeans and I can build a desktop (Swing) database application that works. The sqljdbc.jar file is included in the library path for that project but because it is called from the Oracle persistence provider I can’t see the code used.
February 20th, 2008 at 9:58 am
To Steve,
According from your code, does the error message is about uncaught exception like this “unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown”?
To solve this, you can either throws exception or put code into try-catch block on main method.
Also, you don’t need to import this library “import com.microsoft…”. But you need to “import java.sql.*;” if you coding further.
February 21st, 2008 at 1:12 am
Ling Lom,
Thank you for helping out a Java noob! I’ve added the “thows” statement to the main class and it now builds correctly.
I should have paid more attention to your example above. I’m a but surprised “Just Java 2″ doesn’t warn readers about using a try/catch block.
April 4th, 2008 at 5:05 am
i am using netbeans6 and the following error appeared
“The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
BUILD SUCCESSFUL “
April 8th, 2008 at 4:30 pm
To MMahmoud,
You need to enable TCP/IP on the SQL Server to accept connection.
Do you use SQL Server Express Edition? By default, it will deny remote connection.
April 12th, 2008 at 6:56 am
Same problem as comment 12 and 26. For the error string, is something of configuration of SQL Server, I have 5 days searching how to solve it, but no answers. The documentation of the driver is so poor. If I find something, I’m sure I will post the answer. Even I have reinstalled 2 times and nothing.
April 12th, 2008 at 7:11 am
If you use SQL Express Edition, have you try to enable remote access for it? I wrote one last year, how to enable remote connection to SQL Server 2005 Express.
April 14th, 2008 at 4:15 am
i solve the problem
it was not related to SQL as i enabled the TCP/IP connrection, i just forget to start the Netbeans server
April 22nd, 2008 at 2:54 am
The following bit of code is intended to insert some data in a database. For wome reason I am getting the error about unreported exception ClassNotFoundException must be caught or declared…
I know you addressed this above, but I must have missed something. Can you tell me what I am doing wrong?
public int StoreData(String db, String user, String password, String table,
int freq, long time, double open, double high, double low,
double close, long volume, double wap) throws ClassNotFoundException, SQLException {
try {
Class.forName(”com.mysql.jdbc.Driver”);
String connectionUrl = “jdbc:” + db + “://localhost/mysql?” +
“user=” + user + “&password=” + password + “;”;
conn = DriverManager.getConnection(connectionUrl);
String strSQL = “INSERT INTO ” + table + ” (time,open,high,low,close,” +
“volume,wap) VALUES (” + time + “,” + open + “,” + high + “,” +
low + “,” + close + “,” + volume + “,” + wap + “)”;
rowsEffected = stmt.executeUpdate(strSQL);
conn.close();
} catch (SQLException e) {
System.out.println(”SQL Exception: ” + e.toString());
} catch (ClassNotFoundException cE) {
System.out.println(”Class Not Found Exception: ” + cE.toString());
}
April 22nd, 2008 at 9:27 am
To DavidR,
Your code seems to be OK. Can you capture what exactly error message is shown?
By the way, have you try to run the simpler code as the same as in my example before?. It may help you address the problem easier.
April 23rd, 2008 at 8:23 pm
Thanks for the help.
I found the problem. There was a class of interfaces that I had missed. I did not understand the full tree.
May 13th, 2008 at 12:36 pm
Hello there..
I m trying to connect netbeans with postgreSQL.
But I found an error:
“java.lang.ClassNotFoundException”
Could you please help me out of this.
Thanks.
May 13th, 2008 at 1:20 pm
Hi.
I am facing a problem while connecting.
The SQL Server (am using 2005) is installed on a remote server with the login as Windows authentication. Have checked the port number, remote connection and TCP/IP settings and they are all in place.
However, when I try to create a new database connection from Net Beans 6 on my computer, it gives me a error saying: Cannot establish a connection to ….. Login failed for user…… The user is not associated with a trusted SQL Server Connection.
Don’t quite know what to do. Do I have to change the login on the SQL server to SQL Server authentication?
Thanks!
May 13th, 2008 at 2:07 pm
To, AB
I had never used postgreSQL before. I think you should find PostgreSQL JDBC driver instead. The error likes that means NetBeans can’t find the library you reference to.
To, PriyankaD
The error seems to be that you use SQL account to connect to the SQL Server that doesn’t enable SQL Server authentication. If you need to use SQL accounts, you have to change authentication mode to “SQL Server and Windows Authentication mode”.
For detail steps, visit Enable remote connection to SQL Server 2005 Express
May 29th, 2008 at 4:10 pm
I have a problem while connect:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ’sa’. Reason: Not associated with a trusted SQL Server connection.
I use SQL server 2000 sp4 and netbean 6.1
Please help!
May 30th, 2008 at 9:35 am
The problem seems to be the same as on comment #35 except that it is SQL Server 2000. In SQL Server 2000, You need to change the Authentication Mode of the SQL server from “Windows Authentication Mode (Windows Authentication)”
to “Mixed Mode (Windows Authentication and SQL Server Authentication)”.
June 4th, 2008 at 12:04 pm
Thank you…..Excellent job.
July 9th, 2008 at 3:59 pm
[...] STEP 4At first I couldn’t get the code to build and needed to add a few import statements (e.g. this should have been a clue that I was using the wrong SQL library but there you go). Then I found Nipawit Luangaroon’s excellent post all about accessing SQL Server from NetBeans in which he gives clear simple instructions on adding the correct library to the project’s CLASSPATH: http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connec... [...]
July 31st, 2008 at 6:18 pm
I’m using jre 1.6 and NetBeans 6.1 I get error:
EVERE: Java Runtime Environment (JRE) version 1.6 is not supported by this driver. Please use the JDBC 4 driver (sqljdbc4.jar) instead.
java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.6 is not supported by this driver. Please use the JDBC 4 driver (sqljdbc4.jar) instead.
Must it revert to older jre??
July 31st, 2008 at 9:57 pm
What’s the JDBC Driver version you are using? Try to use the latest one on the Microsoft site.
August 7th, 2008 at 10:59 pm
Hi,
I’m using NetBeans 6.1 and SQL Server Express Edition 2005 and I’m unable to connect them. Here’s the error code:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: La conexión TCP/IP al host ha fallado. java.net.ConnectException: Connection refused: connect
It’s the same TCP/IP connection error that almost everyone has. I have already enabled the TCP/IP connections but I still get that error.
Your help would be really appreciated.
Thanks