Home arrow Java arrow Page 4 - Working with DML to Develop SQL Server based Java Applications using NetBeans IDE
JAVA

Working with DML to Develop SQL Server based Java Applications using NetBeans IDE


This article introduces you to a step-by-step process for developing Java (or JFC) based applications with Microsoft SQL Server as database, using NetBeans IDE. The third in a series, this article introduces you to working with DML statements such as SELECT, INSERT, UPDATE and DELETE.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
June 07, 2006
TABLE OF CONTENTS:
  1. · Working with DML to Develop SQL Server based Java Applications using NetBeans IDE
  2. · How to write the output of a SELECT statement to a text file using Java with NetBeans IDE: source code
  3. · How to write the output of a SELECT statement to a text file using Java with NetBeans IDE: explanation
  4. · How to INSERT, UPDATE or DELETE rows using Java

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Working with DML to Develop SQL Server based Java Applications using NetBeans IDE - How to INSERT, UPDATE or DELETE rows using Java
(Page 4 of 4 )

In the previous sections, I explained how to retrieve information using the SELECT statement and writing the same to a text file.  Now, we shall discuss issuing DML statements such as INSERT, UPDATE and DELETE.

Let us work with a new form in the same application.  Within the "Projects" view, open "Source Packages," right-click on "MyDBPack," and go to "new->JFrame form" (Fig03).


Fig 3

I currently named it "DBSample04;" make sure that you change the code behind it to the following:

public DBSample04() {
        initComponents();
        this.setSize(300,200);
    }

You design the form exactly as in previous sections and copy the following code in the "Button click" event (or "ActionPerformed" event):

private void btnConnectActionPerformed(java.awt.event.ActionEvent
evt) {                                        

// TODO add your handling code here:
        try {
           Class.forName
("com.microsoft.jdbc.sqlserver.SQLServerDriver");          
            Connection conn = DriverManager.getConnection
("jdbc:microsoft:sqlserver://serverjag:1433","sa","");
            Statement sql_stmt = conn.createStatement();
            sql_stmt.executeUpdate("UPDATE Northwind..emp SET sal
= sal - 500");
            sql_stmt.close();
            conn.close();
            this.txtMsg.setText("updated successfully");
        }
        catch(Exception e) {
            this.txtMsg.setText("Failed to connect; Please view
Stack Trace");
            e.printStackTrace();
        }
    }  

In the above code, instead of using "executeQuery," I used "executeUpdate."  The "executeQuery" returns a "ResultSet" object, and we need to extract information from that.  The UPDATE statement does not return any "ResultSet." 

Even though I demonstrated a simple UPDATE in the above example, you can replace the same with other DML statements such as INSERT, DELETE and so on.

Before executing the application, you need to specify that the "DBSample04" is the form to get executed first.  Otherwise, you will see "DBSample03" every time.  This is a bit different from our other applications (in my previous series).

To specify your own form as the "start-up" form using NetBeans IDE:

· Right-click on the project ("SampleJavaApp1") and go to "properties." 

· Select "Run" in the "categories" and specify "MyDBPack.DBSample04" as the "Main Class" (Fig04)


Fig 4

· Finally click "OK."

Press F5 to execute and you should be able to see our new form.

Remarks

The entire code for this article is freely available in the form of a zip file.  That downloadable solution was developed using NetBeans 4.1 IDE and tested with Microsoft SQL Server 2000 database Enterprise Edition (with Service Pack 3).  I didn't really test it in any other version.  If you are unable to connect to the respective database, please post in the discussion section, so that I may guide you.

I used Microsoft Windows 2003 Standard Edition to work with this sample.  I request that you follow the steps according to the operating system installed on your computer. Most of them will be very similar to follow and very easy to understand (unless it is a different platform).

The final issue you need to know is that none of my articles in this series are optimized for performance.  Tuning/improving the performance of a Java application is beyond the scope of this article.  I simply wanted to explain the concepts clearly. 

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