Java
  Home arrow Java arrow Page 3 - Working with DML and DDL using NetBeans ID...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Working with DML and DDL using NetBeans IDE
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-06-14

    Table of Contents:
  • Working with DML and DDL using NetBeans IDE
  • How to work with “PreparedStatement” in Java using NetBeans IDE: source code
  • How to work with “PreparedStatement” in Java using NetBeans IDE: explanation
  • How to create tables dynamically using Java with NetBeans IDE

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with DML and DDL using NetBeans IDE - How to work with “PreparedStatement” in Java using NetBeans IDE: explanation


    (Page 3 of 4 )

    This section explains the code provided in the previous section.  I would like to explain the code part by part.  Let us look at the first statement:

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");         

    The above statement will simply load the Microsoft SQL Server 2000 driver into memory.  Further proceeding, we have the following:

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

    The above statement will connect to the default SQL Server instance available at the server named “serverjag,” at the default port “1433,” with the user name “sa” and with a blank password.  Further proceeding, we have the following:

                PreparedStatement stmt = conn.prepareStatement("UPDATE Northwind..emp " +
                                  "SET sal = ? " +
                                  "WHERE empno = ?");

    The above statement is a bit different from what we discussed in my previous article.  In my previous article, I worked with the “createStatement” method, which gives us a “Statement” object.

    Now, we are working with the “prepareStatement” method, which gives us a “PreparedStatement” object.  A “PreparedStatement” is cached in memory for frequent execution.  But normally a “Statement” would never be cached in memory (and therefore you would see a slow performance, even when you issue the same type of statements very frequently).

    In general, if you are issuing different DML statements, “Statement” would be helpful.  If you are issuing the same DML statement with a difference in values, “PreparedStatement” is better.  In the above statement, you can observe that the UPDATE statement is prepared with missing values (values with a question mark).  In fact, at this moment, the UPDATE statement would not be executed at all.  It is simply “prepared” in memory.

    Further proceeding we have the following:

                stmt.setInt(1, 5000);
                stmt.setInt(2, 1002);
                stmt.executeUpdate( );

    The first line replaces the first question mark in the UPDATE statement with 5000.  Similarly, the second line replaces the second question mark with 1002. Once you have provided the values for both missing quantities, you can execute the statement by issuing the “executeUpdate” command (with no parameters).

    In the above code, I am not using the same “PreparedStatement” more than once (because it is a simple demonstration).  If you need to use the same “PreparedStatement” by applying new values (by replacing old values with new values), you need to issue the following statements (before applying the new values):

    stmt.clearParameters( );
    conn.commit( );

    This is critical when dealing with the “PreparedStatement” multiple times, because you must clear the cache of existing values. 

    Even though I provided only an UPDATE statement, you are free to work with any INSERT or DELETE with the same kind of method.

    More Java Articles
    More By Jagadish Chaterjee


       · Hllo guys, now I extended the previous contributions to work with Java and SQL...
     

    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...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek