This article 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.
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):
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.