Thursday, October 7, 2010

JSP Insert Query using JDBC


JSP- Insert Query.

Problem:

              Using insert Query, to insert the data to table using jsp and mysql.

  We follow some steps to connect jsp with mysql database.


1). Load the Driver

            Class.forName(“com.mysql.jdbc.Driver”);

            This statement load the mysql Driver.

2). Create the Connection

String s=”jdbc:mysql://localhost/Database_Name”,”username”,”password”
           
Connection con=DriverManager.getConnection(s);

3). Create Statement

            Statement stmt=con.createStatement();

            String sql=”insert into table_name values(‘suresh’,’kumar’);

Stmt.executeUpdate(sql);

Requirements:

            Netbean 6.8

            Mysql 6.0

            Knowledge from mysql.
 
Procedure:
                         Open mysql 6.0

             Step 1:
                        
                        Create DataBase named as example.

How to Create Database

Mysql > create database example;














Successfully database is created.

Step 2:

                Use database.

How to activate example database?

Mysql > use example;













Step 3:

create table named exam.

Mysql > create table exam(student_name varchar(40),rollno varchar(20));















Step 4:

Create jsp file name as InsertExample

Include following coding into InsertExample


< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< %@page import="java.sql.*" % >
< html >
< head >
< title > Java DataBase Connectivity-Insert Example < /title >
< /head >
< body >
< %!
Connection con=null;
ResultSet rs=null;
Statement stmt=null;
% >
< %
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e)
{
out.println(e.getMessage());
}
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost/example","root","sure");
stmt=con.createStatement();
int i=stmt.executeUpdate("insert into exam values('sureshkumar','2k2ccs37')");
if(i==1)
{
out.println("Successfully Inserted");
}
else
{
out.println("Error For Inserted");
}
}

catch(Exception e)
{
out.println(e.getMessage());
}
finally
{
con.close();
stmt.close();
}
% >
< /body >
< /html >


Step 5:

           
             Download mysql-connector-java-5.1.6-bin.jar;

            Extract mysql-connector-java-5.1.6-bin.jar  zip file.

            Import mysql-connector-java-5.1.6-bin.jar  to our project.

            If don’t import mysql-connector-java-5.1.6-bin.jar , you’ll get error.

            How to import mysql-connector-java-5.1.6-bin.jar file

            Right Click on DataBasePro and select properties.























            Project Properties-DataBasePro window will open.


             Select Libraries under categories



Click Add JAR/Folder





Select mysql-connector.jar that you have installed in your system and open.
            
Step 6:

            Run the InsertExample














Get Output
























Step 7:

How to check that the record is inserted into exam table in mysql database.

Open mysql 6.0

To activate our database.

Using following command

mysql > use database_name;

mysql > use example;

mysql > select * from exam;




















Note we insert sureshkumar as student_name and 2k2ccs37 as rollno



That’s all.

You have any doubt, post comments.

Have a nice day.

No comments:

Post a Comment