- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 set auto-increment to an existing column in a table using JDBC API?
You can add/set an auto increment constraint to a column in a table using the ALTER TABLE command.
Syntax
ALTER TABLE table_name ADD id INT PRIMARY KEY AUTO_INCREMENT
Assume we have a table named Dispatches in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location with description as shown below:
+--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | ProductName | varchar(255) | YES | UNI | 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 | PRI | NULL | | +--------------+--------------+------+-----+---------+-------+
Following JDBC program establishes connection with MySQL database ,adds a column named Id and sets the values to the id column as auto-increment.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class SettingAutoIncrement { 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(); //Setting the column id auto-increment String query = "ALTER TABLE Sales ADD id INT PRIMARY KEY AUTO_INCREMENT"; stmt.execute(query); stmt.executeBatch(); System.out.println("Table altered......"); } }
Output
Connection established...... Table altered......
If you retrieve the contents of the Sales table using the Select command you can observe that a column named id added to the table with auto-incremented integer values.
mysql> select * from Sales; +-------------+--------------+--------------+--------------+-------+----------------+----+ | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location | id | +-------------+--------------+--------------+--------------+-------+----------------+----+ | Key-Board | Raja | 2019-09-01 | 08:51:36 | 7000 | Hyderabad | 1 | | Earphones | Roja | 2019-05-01 | 05:54:28 | 2000 | Vishakhapatnam | 2 | | Mouse | Puja | 2019-03-01 | 04:26:38 | 3000 | Vijayawada | 3 | | Mobile | Vanaja | 2019-03-01 | 04:26:35 | 9000 | Vijayawada | 4 | | Headset | Jalaja | 2019-03-01 | 05:19:16 | 6000 | Vijayawada | 5 | +-------------+--------------+--------------+--------------+-------+----------------+----+ 5 rows in set (0.00 sec)
Advertisements