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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Jdbc Insert Statement 
 

The Tutorial give you an example from JDBC Insert Statement.

 

JDBC Insert Statement

                         

Add a row to a existing table using insert statement in JDBC. The tutorial illustrates an example from JDBC Insert Statement. In this program, the code include a class Jdbc Insert Statement. Inside this class we have a list of method that help you to insert the new values to the existing table as given below -

Loading a driver by calling a class.forName( ),that accept driver class as parameter. The DriverManager.getConnection ( ) returns you a connection object, which built a connection between url and database. The connection object calls createStatement ( ),that returns you a sql object. This object send and execute sql statements in database. To add a rows in an existing table, we use INSERT statement.

   executeUpdate ( ) -  This method returns you the number of rows added and modified  in a table.

The interface statement object ,st,executes the INSERT statement using execute Update ( ).The executeQuery ( ) return you the retrieved record set  from a table using select statement Finally the print ln print no, name from the table. Incase there is an exception in try block, the catch block caught and handle the exception

 

JdbcInsertStatement.java

import java.sql.*;

public class JdbcInsertStatement {

    public static void main(String args[]) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";
        
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            con.setAutoCommit(false);// Disables auto-commit.

            st = con.createStatement();

            st.executeUpdate("INSERT INTO stu VALUES('1','Komal')");
            st.executeUpdate("INSERT INTO stu VALUES('2','Ajay')");
            st.executeUpdate("INSERT INTO stu VALUES('3','Santosh')");
            st.executeUpdate("INSERT INTO stu VALUES('4','Girish')");
            st.executeUpdate("INSERT INTO stu VALUES('5','Rakesh')");
            

            st.executeBatch();

            String sql = "select * from stu";
            rs = st.executeQuery(sql);

            System.out.println("No  \tName");
            while (rs.next()) {
                System.out.print(rs.getString(1) + "   \t");
                System.out.println(rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Output

No  	Name
1   	Komal
2   	Ajay
3   	Santosh
4   	Girish
5   	Rakesh

Download code

                         

» View all related tutorials
Related Tags: java sql mysql c database api ide jdbc data batch application io get vi exec state int bat this id

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.