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

This entry is part 1 of 2 in the series Accessing MySQL on NetBeans using JDBC

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

This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J, MySQL AB’s JDBC Driver for MySQL.

I’ll divide into 2 parts:

  1. Part I : Create a connection
    This part which you’re reading shows about how to establish a connection between NetBeans and MySQL.
  2. Part II : Perform SQL Operations
    This part show how to perform some basic operations from NetBeans to MySQL. For instance, send querys as SELECT, INSERT, UPDATE to a database.

Requirements

  • MySQL Connector/J, licensed under the GPL or a commercial license
    from MySQL AB.
  • NetBeans with JRE (Java Runtime Environment).

Step-by-Step guide

  1. Installation
    • Install NetBeans.
    • Download MySQL Connector/J, name ‘mysql-connector-java-5.0.6.zip’. (The file name may differs depends on the version if you’ve downloaded from the Official Site at here.)
    • Extract the zip file to a folder, you’ll see file ‘mysql-connector-java-5.0.6-bin.jar’ which is the library file that we want. Just copy the file to the library folder, for example to “C:\Program Files\Java\jdk1.6.0_02\lib” directory.
      the library file
  2. Add JDBC Driver to the project on NetBeans (Add a library).
    Next, I create a new Java project on NetBeans named ‘TestMySQL’ and add ‘mysql-connector-java-5.0.6-bin.jar’ that I’ve just extracted from previous step to the project’s library.

    1. Create New Project called TestSQL.
      Create new NetBeans's project
    2. In Projects window, right click the project name and select Properties.
      Open 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.
      Add JAR file
    4. New Window appears, browse to the file ‘mysql-connector-java-5.0.6-bin.jar’ and click Open.
      Browse to the library file
    5. You’ll see the .jar file was added to the project. Click OK to finish.
      The library file was added
      Note: You should keep mysql-connector-java-5.0.6-bin.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.
    Now I’m going to write some code to connect to MySQL database. I have configured MySQL service on localhost.

    1. I’m going to use Connection and DriverMapper Classes so I need to import libraries.
      import java.sql.*;
      Import classes to the project
    2. I’ll connect to MySQL Server on local machine, the mysql database(a default database in MySQL). In main method, add the following code.
      try {  
                  Class.forName("com.mysql.jdbc.Driver");
                  String connectionUrl = "jdbc:mysql://localhost/mysql?" + 
                                         "user=root&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.mysql.jdbc.Driver”); means load the MySQL driver.
      • “jdbc:mysql://localhost/mysql?” + “user=root&password=123456”; is a connection string that tells to connect MySQL on localhost, select database named ‘mysql’ and user/password for MySQL server.
        If you would like to connecto to other database, simply change text ‘mysql’ after ‘localhost/’ to your database name.

      Connection code

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

Next part, I’ll show to how to perform some basic SQL operations to MySQL.

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

206 Comments

  1. Soleil Farah December 8, 2007
  2. vanessa December 8, 2007
  3. Muhammad Farooq December 20, 2007
  4. Soleil Farah January 5, 2008
  5. Soleil Farah January 5, 2008
  6. linglom January 7, 2008
  7. Soleil Farah January 7, 2008
  8. Alan February 14, 2008
  9. linglom February 15, 2008
  10. Hasan February 18, 2008
  11. kuuki February 27, 2008
  12. black Hawk March 14, 2008
  13. off March 23, 2008
  14. razmi June 4, 2008
  15. linglom June 5, 2008
  16. Arnaud June 19, 2008
  17. linglom June 20, 2008
  18. dory June 23, 2008
  19. Arnaud June 23, 2008
  20. Yasir July 20, 2008
  21. Khaled July 29, 2008
  22. sofien August 15, 2008
  23. Rani August 20, 2008
  24. Vicky September 28, 2008
  25. Sam October 27, 2008
  26. Danish Kamran October 29, 2008
  27. Keheliya December 21, 2008
  28. Arijit Chattopadhyay December 26, 2008
  29. Budhi December 27, 2008
  30. arc January 3, 2009
  31. gary January 22, 2009
  32. shili January 23, 2009
  33. T February 12, 2009
  34. linglom February 14, 2009
  35. anju March 19, 2009
  36. rani March 23, 2009
  37. linglom March 28, 2009
  38. sole April 27, 2009
  39. maguzek April 29, 2009
  40. kemal July 17, 2009
  41. Thank_you July 20, 2009
  42. inez August 2, 2009
  43. inez August 2, 2009
  44. cha August 6, 2009
  45. Maggie August 10, 2009
  46. Rani August 11, 2009
  47. Abdul September 25, 2009
  48. manoj September 30, 2009
  49. Nipuna October 4, 2009
  50. manoj October 4, 2009
  51. patrick October 4, 2009
  52. linglom October 6, 2009
  53. patrick October 11, 2009
  54. Hareesh October 17, 2009
  55. neeraj October 17, 2009
  56. linglom October 19, 2009
  57. pkmnambiar October 28, 2009
  58. Atheer October 28, 2009
  59. linglom October 30, 2009
  60. ritika November 3, 2009
  61. shola November 4, 2009
  62. ritika November 4, 2009
  63. pkmnambiar November 4, 2009
  64. vishnu December 14, 2009
  65. Ankit January 7, 2010
  66. linglom January 7, 2010
  67. Fö a betűm January 11, 2010
  68. Nissy January 21, 2010
  69. Alvin January 21, 2010
  70. Alvin January 21, 2010
  71. Nissy January 22, 2010
  72. linglom January 22, 2010
  73. Alvin January 24, 2010
  74. Alvin January 24, 2010
  75. vipul January 31, 2010
  76. Awe February 1, 2010
  77. Faisal Yousuf February 8, 2010
  78. RabiaN February 11, 2010
  79. marcos February 18, 2010
  80. Anish February 20, 2010
  81. Pallab February 21, 2010
  82. steven February 22, 2010
  83. steven February 22, 2010
  84. linglom February 23, 2010
  85. rahul March 12, 2010
  86. nagarajan March 14, 2010
  87. Terrence March 19, 2010
  88. linglom March 22, 2010
  89. Abhijeet March 25, 2010
  90. mayuran April 7, 2010
  91. jokki April 12, 2010
  92. Vikash Agarwal April 13, 2010
  93. Tomas Chabada April 21, 2010
  94. tharaka (Sri Lanka) April 26, 2010
  95. Ninku Reka April 26, 2010
  96. Kumar. S May 14, 2010
  97. Kumar. S May 14, 2010
  98. manish June 5, 2010
  99. Rika June 19, 2010
  100. shalini July 5, 2010
  101. shalini July 6, 2010
  102. linglom July 15, 2010
  103. shalini July 20, 2010
  104. abdullah July 21, 2010
  105. Noe August 11, 2010
  106. lunar August 17, 2010
  107. lunar August 17, 2010
  108. saran August 26, 2010
  109. subhalaxmi August 26, 2010
  110. subhalaxmi August 26, 2010
  111. joyce August 29, 2010
  112. balamurugan September 15, 2010
  113. balamurugan September 15, 2010
  114. linglom September 15, 2010
  115. Satya Prakash September 19, 2010
  116. Dhruv Patel September 21, 2010
  117. Gashteovski October 3, 2010
  118. James Umokoro October 6, 2010
  119. Mridula October 6, 2010
  120. Sam October 13, 2010
  121. Prateek@java October 28, 2010
  122. deepitha November 27, 2010
  123. sneha November 29, 2010
  124. sneha November 29, 2010
  125. swathi December 7, 2010
  126. beth December 10, 2010
  127. Ranjodh December 18, 2010
  128. Richa December 24, 2010
  129. venkat January 13, 2011
  130. venkat January 13, 2011
  131. Sait January 23, 2011
  132. Byron January 27, 2011
  133. Harshit January 28, 2011
  134. UDOYE SAMUEL January 31, 2011
  135. pradip garala February 4, 2011
  136. PrathapSingh February 4, 2011
  137. prem February 9, 2011
  138. PrathapSingh February 9, 2011
  139. linglom February 21, 2011
  140. Monika February 23, 2011
  141. Monika February 23, 2011
  142. Malikge March 6, 2011
  143. Lawrance March 7, 2011
  144. sana March 7, 2011
  145. Angelin March 10, 2011
  146. ASHWIN March 11, 2011
  147. Ben March 14, 2011
  148. Manish March 24, 2011
  149. polaris March 28, 2011
  150. River March 29, 2011
  151. Raj March 30, 2011
  152. linglom March 31, 2011
  153. Sourav March 31, 2011
  154. black300 April 2, 2011
  155. Biju Medayil April 3, 2011
  156. sher April 14, 2011
  157. chutaSRILANKA May 3, 2011
  158. alberto May 3, 2011
  159. javaman May 13, 2011
  160. priya August 3, 2011
  161. lakshmi August 3, 2011
  162. waleed August 10, 2011
  163. vanara September 7, 2011
  164. vanara September 7, 2011
  165. DAKSH September 20, 2011
  166. DAKSH September 20, 2011
  167. marko September 20, 2011
  168. Bob October 13, 2011
  169. Bob October 13, 2011
  170. Rakesh October 16, 2011
  171. amzela October 26, 2011
  172. Willy November 7, 2011
  173. Todd November 8, 2011
  174. ZTH November 9, 2011
  175. yashaarya November 15, 2011
  176. Gaurav November 22, 2011
  177. Rajendra Verma November 23, 2011
  178. mike December 1, 2011
  179. jakoyoski December 6, 2011
  180. sandeep.rai January 12, 2012
  181. biju medayil January 12, 2012
  182. roshni January 16, 2012
  183. deep January 17, 2012
  184. mohan January 20, 2012
  185. ammouna January 28, 2012
  186. Manas March 7, 2012
  187. Mcdonald Chogugudza April 30, 2012
  188. rajee June 6, 2012
  189. manoj June 21, 2012
  190. Ritesh sejkar July 19, 2012
  191. Superman August 23, 2012
  192. jen September 13, 2012
  193. MuthuAnanth September 15, 2012
  194. loki October 12, 2012
  195. abhi November 10, 2012
  196. Lilly December 18, 2012
  197. Shalabh January 4, 2013
  198. Manash JK June 15, 2013
  199. Manash JK June 15, 2013
  200. priya July 21, 2015
  201. linglom July 22, 2015
  202. Elie samyoun March 30, 2016
  203. linglom March 31, 2016
  204. Eddie June 17, 2016
  205. Eddie June 26, 2016
  206. Dun March 5, 2018

Leave a Reply