Home arrow Java arrow Page 6 - Developing SQL Server based Java Apps using NetBeans IDE
JAVA

Developing SQL Server based Java Apps using NetBeans IDE


This article introduces you to developing Java (or JFC) based applications with Microsoft SQL Server as the database, using NetBeans IDE. In this first article in a series, I will show you how to establish a connection from your Java programs to a Microsoft SQL Server database using JDBC.

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 27
May 24, 2006
TABLE OF CONTENTS:
  1. · Developing SQL Server based Java Apps using NetBeans IDE
  2. · What do we need to connect to Microsoft SQL Server from Java (or JDBC)?
  3. · How to configure CLASSPATH to connect to Microsoft SQL Server database
  4. · How to establish a Microsoft SQL Server database connection using JDBC
  5. · Creating a Microsoft SQL Server database connection using NetBeans IDE: Form Design
  6. · Creating a Microsoft SQL Server database connection using NetBeans IDE: the nucleus

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Developing SQL Server based Java Apps using NetBeans IDE - Creating a Microsoft SQL Server database connection using NetBeans IDE: the nucleus
(Page 6 of 6 )

After completing all the steps in previous section, we need to add Microsoft SQL Server related JDBC drivers to our application.  This can be achieved with the following steps:

  • Within the “Projects” view, right-click on “Libraries” and go to “Add Jar/Folder” (Fig 03).


  • Move to your JDBC driver path (in my case, it is at “C:Program FilesMicrosoft SQL Server 2000 Driver for JDBClib”), and select all of the “.jar” files available (Fig 04).


  • After selecting “.jar” files, click “open” and your “Projects” view should look something like the following (Fig 05).

Once you complete all the above steps, you can copy the following code in to your “btnConnectActionPerformed” event:

  try {
            //Load and register SQL Server driver
            Class.forName("
               com.microsoft.jdbc.sqlserver.SQLServerDriver");
            //Establish a connection
            Connection conn = DriverManager.getConnection("
               jdbc:microsoft:sqlserver://serverjag:1433","sa","");
            //Create a Statement object
            Statement sql_stmt = conn.createStatement();
            //Create a ResultSet object, execute the query and return a
            // resultset
            ResultSet rset = sql_stmt.executeQuery("SELECT * FROM
Northwind..orders ");
            int i=0;
            while (rset.next()){
                i++;
            }
            //Close the ResultSet and Statement
            rset.close();
            sql_stmt.close();
            //Close the database connection
            conn.close();
            this.txtMsg.setText(Integer.toString(i) + " rows found");
        }
        catch(Exception e) {
            this.txtMsg.setText("Failed to connect; Please view Stack
Trace");
            e.printStackTrace();
        }

You can hit F5 to execute your solution and click on the button to show you the number of rows available in “Orders” table.

Any doubts, bugs, errors, suggestions, feedback etc. are highly appreciated at jag_chat@yahoo.com.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials