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.

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table

Ads

Ads

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";
  try{
  Class.forName(driver);
  con = DriverManager.getConnection(url+db,"root","root");
  try{
  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){
  e.printStackTrace();
  }
  }
} 

Download this example.

Output of program:

C:\vinod\jdbc\jdbc\jdbc-mysql>javac InsertValues.java

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

Advertisements

Share on Google+Share on Google+

Inserting values in MySQL database table

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

Advertisements

Ads
 

Discuss: Inserting values in MySQL database table   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
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.
karthikeyan
May 31, 2011
mysql program

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

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(rs.next()) { 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
suresh
December 30, 2011
java

good
Ankit
January 14, 2012
issue with this code

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

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

when i execute it gives this error while executing java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:164) at insert.main(insert.java:12)
anon
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.

Ads

 

Ads