Share on Google+Share on Google+

Inserting values in MySQL database table

After making the table in the database, we need to insert the values in the database. Here we are going to see, how we can insert values in the MySQL database table.

Inserting values in MySQL database table


After making the table in the database, we need to insert the values in the database. Here we are going to see, how we can insert values in the MySQL database table. We know that tables store data in rows and column format. After creating a database table, you need to insert the values in it. In this section, we are providing an example with code that provides the facility for inserting the values in MySQL database table.

Description of program:

First of all this program establishes the connection with MySQL database through the JDBC driver, after only that we will be able to insert the values in specific table with the help of some APIs and methods. If any values get inserted in the table then shows a message "1 row affected" but if any problems comes while inserting the data  in the table then it will displays the message "SQL statement is not executed!".

Description of code:

INSERT table_name VALUES(field_values):
Above code is used, when you want to insert values in the database table with appropriate value.

Here is the code of program:

import java.sql.*;

public class InsertValues{
 public static void main(String[] args) {
 System.out.println("Inserting values in Mysql database table!");
  Connection con = null;
  String url = "jdbc:mysql://localhost:3306/";
  String db = "jdbctutorial";
  String driver = "com.mysql.jdbc.Driver";
  con = DriverManager.getConnection(url+db,"root","root");
  Statement st = con.createStatement();
  int val = st.executeUpdate("INSERT employee VALUES("+13+","+"'Aman'"+")");
  System.out.println("1 row affected");
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  catch (Exception e){

Download this example.

Output of program:


C:\vinod\jdbc\jdbc\jdbc-mysql>java InsertValues
Inserting values in Mysql database table!
1 row affected


Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Inserting values in MySQL database table   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
suraj kumar
April 14, 2011
insert statement with where statement

hi.. I m stopped here i cant find the write coding for the next step of my work. I will be thankful to if u send me the answer coding. i want to fill data into a predefined datatable by knowing the primary key of the particular column.
May 31, 2011
mysql program

wow! what a simple and understandable coding
August 20, 2011

import java.sql.*; public class dj { public static void main(String args[]) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con= DriverManager.getConnection("jdbc:odbc:ashish1","",""); Statement st=con.createStatement(); st.executeQuery("Select * from ashish1"); st.executeUpdate("INSERT ashish1 VALUES('nit',85)"); System.out.print("test"); ResultSet rs=st.getResultSet(); while( { System.out.print(rs.getString(1)); System.out.println(rs.getString(2)); //rs.executeUpdate(); } //con.close(); } catch(ClassNotFoundException e) { System.err.println(e); } catch(SQLException e) { System.err.println(e); } } } \\ Is this above code corretc how to insert the new data.
Venkat Santosh
November 23, 2011
i am getting an error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
December 30, 2011

January 14, 2012
issue with this code

This code does not close statement and connection . thats a serious resource leak.
April 30, 2012
no comments

your code got no comments... It's hard to understand. Anyway, it's usable anyway.
September 17, 2012

when i execute it gives this error while executing java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at$ at Method) at at java.lang.ClassLoader.loadClass( at sun.misc.Launcher$AppClassLoader.loadClass( at java.lang.ClassLoader.loadClass( at java.lang.ClassLoader.loadClassInternal( at java.lang.Class.forName0(Native Method) at java.lang.Class.forName( at insert.main(
October 9, 2012
i have a problem

sir, can you help me? when i execute insert commands my values in the textfield are not stored into the table. instead, it saves BLANK data. my program says it recorded the data but when i look at the table there is no data, only blank field.