Jdbc Insert Statement

The Tutorial give you an example from JDBC Insert Statement.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Jdbc Insert Statement

Posted on: November 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Jdbc Insert Statement  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads