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.
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):
// 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.