Home arrow Java arrow Page 3 - Finding and Traversing Rows using NetBeans IDE
JAVA

Finding and Traversing Rows using NetBeans IDE


This series introduces you to a step-by-step process to develop Java (or JFC) based applications with Microsoft SQL Server as the database, using NetBeans IDE. In this article, I shall introduce you to finding and traversing rows using Java.

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
June 21, 2006
TABLE OF CONTENTS:
  1. · Finding and Traversing Rows using NetBeans IDE
  2. · The code generated
  3. · The heart of the application
  4. · Explaining the code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Finding and Traversing Rows using NetBeans IDE - The heart of the application
(Page 3 of 4 )

This section shows you the code to search for the rows in the table.  Copy the following code into the "buttonActionPerformed" event:

private void btnSearchActionPerformed(java.awt.event.ActionEvent
evt) {
TODO add your handling code here:
        try {
           
            if (this.txtEmpno.getText().trim().length()==0) {
                this.lblMsg.setText("Provide Employee Number");
                return;
            }               

           Class.forName
("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
           Connection conn = DriverManager.getConnection
("jdbc:microsoft:sqlserver://serverjag:1433","sa","");       

            Statement sql_stmt = conn.createStatement();
            ResultSet rset = sql_stmt.executeQuery( "SELECT
ename, sal, deptno " +
                                  " FROM Northwind..emp " +
                                  " WHERE empno=" +
this.txtEmpno.getText() +
                                  " ORDER BY ename");
            String str = "";
            if (rset.next()) {
                this.txtEname.setText(rset.getString(1));
                this.txtSal.setText(rset.getString(2));
                this.txtDeptno.setText(rset.getString(3));
                this.lblMsg.setText("");
            }
            else {
                this.lblMsg.setText("Not Found");
            }
            rset.close();
            sql_stmt.close();
            conn.close();
        }
        catch(Exception e) {
            this.lblMsg.setText("Error: Please view Stack
Trace");
            e.printStackTrace();
        }
    }     
             

The above code works only for one row. If we want to work with more than one row, we need to use "jTable" or some other, better strategy.  My upcoming articles will give you the information you need to work with more than one row at any time.

The next section will explain the above code.


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