- Trending Categories
- 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 insert only a single column into a MySQL table with Java?
Use INSERT INTO statement in the Java-MySQL connection code to insert a column.
Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.54 sec)
Here is the Java code to insert only a single column into a MySQL table.
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertOneColumnDemo { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web?" + "useSSL=false", "root", "123456"); String query = "insert into DemoTable(Name) values(?) "; ps = con.prepareStatement(query); ps.setString(1, "Robert"); ps.executeUpdate(); System.out.println("Record is inserted successfully......"); } catch (Exception e) { e.printStackTrace(); } } }
This will produce the following output −
Now let us check the records inserted in the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+--------+ | Name | +--------+ | Robert | +--------+ 1 row in set (0.00 sec)
Advertisements