Services | Updates | Contact
Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Stored Procedures and Functions
Stored Routines (Procedures and Functions) are supported in version MySQL 5.0. Stored Procedure is a set of statements,
 
Struts Tutorials - Jakarta Struts Tutorial
This complete reference of Jakarta Struts shows you how to develop Struts applications using ant and deploy on the JBoss
 
More Tutorials...


    Loan Information     Struts     Open Source

Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

 
 
JDBC

 
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. 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

                         

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

Current Comments

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

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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.