Home Jdbc Jdbc Insert Statement
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Jdbc Insert Statement

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: November 18, 2008

Related Tutorials

Ask Questions?    Discuss: Jdbc Insert Statement  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com