Home arrow Java arrow Page 4 - Traversing To and Fro with SQL Server Based Java Applications using NetBeans IDE
JAVA

Traversing To and Fro with SQL Server Based Java Applications using NetBeans IDE


This series introduces you to a step-by-step process for developing Java (or JFC) based applications with Microsoft SQL Server as the database, using NetBeans IDE. In this article (which is the last in this series), I shall introduce you to traversing through a set of rows more efficiently. This will be a bit different from any of the articles in this series.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
June 28, 2006
TABLE OF CONTENTS:
  1. · Traversing To and Fro with SQL Server Based Java Applications using NetBeans IDE
  2. · How to connect to the database during form load
  3. · The Java code from IDE
  4. · The source code for buttons
  5. · How to handle the clear memory resources when the form is closed

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Traversing To and Fro with SQL Server Based Java Applications using NetBeans IDE - The source code for buttons
(Page 4 of 5 )

This section shows you the source code to walk (or jump) through the rows in all directions. Copy the following code at the appropriate "ActionPerformed" events:

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

// TODO add your handling code here:
        try{
         if (rset.last()) {
                this.txtEmpno.setText(rset.getString(1));
                this.txtEname.setText(rset.getString(2));
                this.txtSal.setText(rset.getString(3));
                this.txtDeptno.setText(rset.getString(4));
            }  
        }
        catch(Exception e) {
            this.lblMsg.setText("Error: Please view Stack
Trace");
            e.printStackTrace();
        }
    }                                      
    private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {                                        

// TODO add your handling code here:
        try{
         if (rset.first()) {
                this.txtEmpno.setText(rset.getString(1));
                this.txtEname.setText(rset.getString(2));
                this.txtSal.setText(rset.getString(3));
                this.txtDeptno.setText(rset.getString(4));
            }  
        }
        catch(Exception e) {
            this.lblMsg.setText("Error: Please view Stack
Trace");
            e.printStackTrace();
        }
    }                                       
    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {                                           

// TODO add your handling code here:
       try{
         if (rset.previous()) {
                this.txtEmpno.setText(rset.getString(1));
                this.txtEname.setText(rset.getString(2));
                this.txtSal.setText(rset.getString(3));
                this.txtDeptno.setText(rset.getString(4));
            }  
        }
        catch(Exception e) {
            this.lblMsg.setText("Error: Please view Stack
Trace");
            e.printStackTrace();
        }
    }                                          
    private void btnNextActionPerformed
(java.awt.event.ActionEvent evt) {                                       

// TODO add your handling code here:
        try{
         if (rset.next()) {
                this.txtEmpno.setText(rset.getString(1));
                this.txtEname.setText(rset.getString(2));
                this.txtSal.setText(rset.getString(3));
                this.txtDeptno.setText(rset.getString(4));
            }  
        }
        catch(Exception e) {
            this.lblMsg.setText("Error: Please view Stack
Trace");
            e.printStackTrace();
        }        

    }                              

The above code is using methods such as previous, next, last and first.  They can be used if and only if we declare the "Statement" object as follows:

sql_stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                      ResultSet.CONCUR_READ_ONLY);

This is quite essential if you need to move freely in any direction.  According to the above statement, I also declared the result set to be read-only. That means it cannot be modified by the user any more. Since our design is not for modifications, I used it. But you can't use it if you need the result set to be updatable.


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 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials