- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the datatype of a column in an existing table using JDBC API?
You can change the datatype of a column in a table using the ALTER TABLE command.
Syntax
ALTER TABLE Sales MODIFY COLUMN column_name column_new_datatuype
Assume we have a table named Sales in the database with 7 columns namely ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location and, ID with description as:
+--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName | varchar(255) | YES | | NULL | | | CustomerName | varchar(255) | YES | | NULL | | | DispatchDate | date | YES | | NULL | | | DeliveryTime | time | YES | | NULL | | | Price | int(11) | YES | | NULL | | | Location | varchar(255) | YES | | NULL | | | ID | int(11) | NO | | NULL| | | +--------------+--------------+------+-----+---------+-------+
Following JDBC program establishes connection with MySQL database, and changes the data-type of the column location from varchar to text.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class ChangingDatatype { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Query to alter the table String query = "ALTER TABLE Sales MODIFY COLUMN Location Text"; //Executing the query stmt.executeUpdate(query); System.out.println("Column datatype changed......"); } }
Output
Connection established...... Column datatype changed......
Since we have altered the type of location column, if you get the description of the Sales table using the describe command you can observe that the data-type of the column named location is changed from varchar to text.
mysql> describe sales; +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName | varchar(255) | YES | | NULL | | | CustomerName | varchar(255) | YES | | NULL | | | DispatchDate | date | YES | | NULL | | | DeliveryTime | time | YES | | NULL | | | Price | int(11) | YES | | NULL | | | Location | text | YES | | NULL | | | ID | int(11) | NO | | NULL | | +--------------+--------------+------+-----+---------+-------+ 7 rows in set (0.00 sec)
- Related Articles
- How to add a new column to an existing table using JDBC API?
- How to delete a column from an existing table in a database using JDBC API?
- How to set auto-increment to an existing column in a table using JDBC API?
- How to get the datatype of a column of a table using JDBC?
- How to retrieve a record from an existing table in a database using JDBC API?
- How to remove a record from an existing table in a database using JDBC API?
- How to retrieve a record from an existing table in oracle database using JDBC API?
- How to remove a record from an existing table in oracle database using JDBC API?
- How to call an existing function in a database using JDBC API?
- How to call an existing stored procedure in a database using JDBC API?
- How to drop constraint on a column of a table in a database using JDBC API?
- How add a unique key constraint to a column of a table in a database using JDBC API?
- How to create a table in a database using JDBC API?
- How to add a primary key constraint to a column of a table in a database using JDBC API?
- How to add a NOT NULL constraint to a column of a table in a database using JDBC API?

Advertisements