JAVA JDBC – Create, Update and Delete Table in MySQL Database

In this chapter you will learn:

1. How to create table using JDBC in Java?
2. How to Update, delete or rename table in JDBC?
3. Programming Example

After creating database using JDBC in Java, next is creating and working with MySQL table in JDBC. Here, I am going to write a program which will create, update, rename and delete the table. Inserting and manipulating data will be discussed in next chapter so, keep focus on creating, updating and deleting table in this chapter.

Programming Example

In the previous chapter we created a database STOREDB. We will use this database to store table.

Output

TABLE ITEM Created Successfully
Closing the connection.
_

Updating Table

Several times you need to alter table definition mostly when you want to add more field or remove a column or sometimes changing column range. Here is an example of Table Update in JDBC is given below.

Programming Example

Output

Column Quantity Added Successfully
Table Price Removed
Changed PRODUCT varchar(50) to PRODUCT nvarchar(200)
Closing the connection.

Rename Table

When you need to rename MySQL Table using JDBC in your java program you can use the following program to rename table.

Output

TABLE ITEM Renamed to COSMETIC Successfully
Closing the connection.

Deleting Table

Finally when you no longer need to Table you can delete it using DROP command. Here, is the complete program.

Programming Example

Output

TABLE COSMETIC Deleted Successfully
Closing the connection.
Notes
NOTE: You must recreate ITEM Table because this table will be used to storing or updating data in next chapter.

Summary

In this tutorial I explained how can you create, delete, update or rename table using JDBC. You must recreate ITEM table because this table will be used for demo example in next chapter. Hope, you wouldn’t have face any difficulties learning this tutorial. The next chapter is Inserting, Updating, Selecting or Deleting Data in Table.


Leave a Reply

Your email address will not be published. Required fields are marked *