Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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";
    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

                         

» View all related tutorials
Related Tags: c database orm table data form io sed column remove unique key field columns this tab move row for rows

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

what is the use of "+" sign?

Posted by Abhineet on Wednesday, 08.13.08 @ 22:31pm | #72696

Sir can we know the exact code of
Inserting a record on Employees Table?

Thank you

Posted by rey on Wednesday, 12.19.07 @ 13:31pm | #42940

This is my script exactly how you done it but i've got one error:"The statement SQL isn't executed General error";WHY?
import java.sql.*;
class JDBCex
{

public static void main(String[] args)
{
System.out.println("Inserting values in MS Access database table!");

Connection conn = null;


String url = "jdbc:odbc:formular";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

try
{
Class.forName(driver);
conn = DriverManager.getConnection(url,"","");

try
{
Statement stmt = conn.createStatement();
int val = stmt.executeUpdate("INSERT INTO angajati VALUES("+13+","+"'ion'"+","+"'ion'"+","+"'ion'"+" )");
System.out.println("1 row affected");
}

catch(SQLException e)
{
System.out.println("SQL Statement isn't executed " + e.getMessage());
}
}
catch(Exception e)
{
e.printStackTrace();
}



}
}

Posted by ion on Tuesday, 07.31.07 @ 15:19pm | #22302

Hi
In this example some mistake is there in sql command

Insert into tablename values() but it is Insert tablename values()..
so please correct it.

with regrds,
pavan kumar chelluri

Posted by pavan kumar chelluri on Sunday, 07.8.07 @ 10:52am | #20907

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.