Accessing SQL Server on NetBeans using JDBC, Part 1: Create a connection

This entry is part 1 of 3 in the series Accessing SQL Server on NetBeans using JDBC

Accessing SQL Server on NetBeans using JDBC, Part 1: Create a connection

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:

  1. 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
  2. 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.
  3. Part III: Troubleshooting
    The last part is about problems and how to fix them.

Requirements

  1. Microsoft SQL Server JDBC Driver
    To get latest version, visit Microsoft SQL Server 2005 JDBC Driver.
  2. NetBeans with JRE (Java Runtime Environment) version 1.4 or later
    You can find at NetBeans.org.

Step-by-Step Guides

  1. 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.
      Extract the file 'sqljdbc_1.1.1501.101_enu.exe'
    • 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.
      sqljdbc.jar
    • You can find more informations about JDBC Driver at \Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.1\enu\help\default.htm.
  2. 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.

    1. Create New Project called TestSQL.
      Create New Java Application Project
    2. In Projects window, right click the project name and select Properties.
      Project's Properties
    3. Project Properties window appears. The Categories on left side, select Libraries. And on right side in Compile tab, click Add JAR/Folder.
      To add library file(s)
    4. New Window appears, browse to the file ‘sqljdbc.jar’ and click Open.
      Select sqljdbc.jar file
    5. You’ll see the .jar file was added to the project. Click OK to finish.
      A library was added
      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.
  3. Connect to the database
  4. 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.

  1. I’m going to use Connection and DriverMapper Classes so I need to import libraries.
    import java.sql.*;
    import java.sql.*;
  2. 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.

    Connect to Northwind

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

That’s it for part I. Now you know how to connect to SQL Server on NetBeans, the part II will coming soon.

Series NavigationAccessing SQL Server on NetBeans using JDBC, Part 2: Perform SQL Operations >>

150 Comments

  1. Chatsiri Ratana April 28, 2007
  2. amcp July 2, 2007
  3. linglom July 5, 2007
  4. amcp July 9, 2007
  5. shreya August 17, 2007
  6. Cesar August 18, 2007
  7. Maricelys August 24, 2007
  8. linglom August 24, 2007
  9. Maricelys September 5, 2007
  10. Jaime October 30, 2007
  11. linglom October 31, 2007
  12. Aliaa December 8, 2007
  13. linglom December 8, 2007
  14. Aliaa December 8, 2007
  15. Aliaa December 8, 2007
  16. linglom December 9, 2007
  17. David Chacon January 18, 2008
  18. linglom January 21, 2008
  19. linglom January 30, 2008
  20. weincruz January 30, 2008
  21. linglom January 31, 2008
  22. Steve Shaffer February 20, 2008
  23. Steve Shaffer February 20, 2008
  24. linglom February 20, 2008
  25. Steve February 21, 2008
  26. MMahmoud April 4, 2008
  27. linglom April 8, 2008
  28. dxider April 12, 2008
  29. linglom April 12, 2008
  30. MMahmoud April 14, 2008
  31. DavidR April 22, 2008
  32. linglom April 22, 2008
  33. DavidR April 23, 2008
  34. AB May 13, 2008
  35. PriyankaD May 13, 2008
  36. linglom May 13, 2008
  37. SoulKeeper May 29, 2008
  38. linglom May 30, 2008
  39. Kinjal June 4, 2008
  40. Maina July 31, 2008
  41. linglom July 31, 2008
  42. Max August 7, 2008
  43. linglom August 10, 2008
  44. Max August 12, 2008
  45. linglom August 14, 2008
  46. sidharth August 29, 2008
  47. jmocke September 13, 2008
  48. linglom September 14, 2008
  49. vampire September 30, 2008
  50. vampire September 30, 2008
  51. linglom September 30, 2008
  52. Tez! October 6, 2008
  53. laura October 7, 2008
  54. laura October 7, 2008
  55. vampire October 8, 2008
  56. vampire October 8, 2008
  57. vampire October 9, 2008
  58. linglom October 9, 2008
  59. linglom October 9, 2008
  60. vampire October 9, 2008
  61. linglom October 10, 2008
  62. vampire October 10, 2008
  63. vampire October 10, 2008
  64. linglom October 10, 2008
  65. vampire October 11, 2008
  66. Alee October 11, 2008
  67. linglom October 11, 2008
  68. laura October 13, 2008
  69. vampire October 13, 2008
  70. vampire October 13, 2008
  71. linglom October 14, 2008
  72. sania October 15, 2008
  73. linglom October 15, 2008
  74. lanre October 21, 2008
  75. alex November 26, 2008
  76. alex November 26, 2008
  77. linglom November 26, 2008
  78. elijah January 12, 2009
  79. Ramona Nico January 15, 2009
  80. linglom January 15, 2009
  81. dhafer January 30, 2009
  82. linglom February 1, 2009
  83. dhafer February 1, 2009
  84. felix February 6, 2009
  85. linglom February 8, 2009
  86. linglom February 8, 2009
  87. dhafer February 8, 2009
  88. linglom February 10, 2009
  89. bob March 7, 2009
  90. linglom March 10, 2009
  91. saurabh March 25, 2009
  92. saurabh March 25, 2009
  93. saurabh March 25, 2009
  94. linglom March 28, 2009
  95. alex March 30, 2009
  96. linglom April 7, 2009
  97. alex April 7, 2009
  98. Rahul April 9, 2009
  99. Chris May 20, 2009
  100. Hans Kristanto May 24, 2009
  101. linglom May 25, 2009
  102. sasakae June 23, 2009
  103. sasakae June 25, 2009
  104. linglom June 26, 2009
  105. marcha August 3, 2009
  106. linglom August 5, 2009
  107. binodya talawatta September 10, 2009
  108. Trong September 20, 2009
  109. pOpOy jIN September 24, 2009
  110. pOpOy jIN September 24, 2009
  111. Sharry October 13, 2009
  112. Sharry October 14, 2009
  113. linglom October 15, 2009
  114. Praveen November 19, 2009
  115. jthan_col November 21, 2009
  116. NeoBones November 22, 2009
  117. linglom November 25, 2009
  118. Thiri February 18, 2010
  119. sreenivas February 19, 2010
  120. linglom February 23, 2010
  121. townguy April 2, 2010
  122. Amna April 27, 2010
  123. adabs June 2, 2010
  124. linglom June 7, 2010
  125. Narendra Singh June 23, 2010
  126. hakan July 22, 2010
  127. nafiseh November 17, 2010
  128. radhika December 11, 2010
  129. linglom December 18, 2010
  130. Zaher December 26, 2010
  131. Zaher December 26, 2010
  132. asda110 May 12, 2011
  133. cencio May 26, 2011
  134. Nitin August 22, 2011
  135. Suyash October 28, 2011
  136. Harish December 10, 2011
  137. Yahya December 14, 2011
  138. Yahya December 14, 2011
  139. Jason December 20, 2011
  140. Kashif March 24, 2012
  141. Desh April 13, 2012
  142. Antropusx May 15, 2012
  143. hena July 23, 2012
  144. Rajesh Kumar Gupta July 23, 2012
  145. Hariharan November 5, 2012
  146. Jennifer December 2, 2012
  147. Arslan December 6, 2012
  148. raja January 18, 2013
  149. Jai Mata Di February 15, 2013
  150. luis August 2, 2023

Leave a Reply