Home arrow Java arrow Page 4 - Working with DML and DDL using NetBeans IDE
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Working with DML and DDL using NetBeans IDE - How to create tables dynamically using Java with NetBeans IDE
(Page 4 of 4 )

In the previous sections, I explained how to work with “PreparedStatements” in Java.  Now, we shall discuss issuing DDL statements such as CREATE, ALTER, and so on.

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).  I currently named it “DBSample08.” Make sure that you change the code behind it to the following:

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

You design the form exactly as in previous sections. Copy the following code into 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.execute("CREATE TABLE Northwind..dept
(deptno int, dname varchar(20), loc int);");
            sql_stmt.close();
            conn.close();
            this.txtMsg.setText("created table successfully");
        }
        catch(Exception e) {
            this.txtMsg.setText("Error: Please view Stack
Trace");
            e.printStackTrace();
        }
    }             

In the above code, instead of using “executeQuery” or “executeUpdate,” I used simply “execute.”  The “executeQuery” returns a “ResultSet” object from which we need to extract information. The DML statement does not return any “ResultSet.”  To work with “DDL” statements, it is better to use the “execute” method.

Even though I demonstrated a simple CREATE TABLE in the above example, you can replace the same with other DDL statements such as ALTER TABLE, DROP TABLE, and so forth.

To modify the “start-up” form before execution, you can follow the instructions in my previous article.  Once you complete the “start-up” form configuration, you can press F5 to execute, and you should be able to see your 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 and very easy to understand (unless it is a different platform).

The final point you should 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 a bit more 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 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials