?
Solved

Insert  in JSP

Posted on 2003-02-27
4
Medium Priority
?
192 Views
Last Modified: 2010-04-01
hi all,

Can anybody give me the code snippet of insert A record into the MySQL database using JSP.

Thanks
Fathima
0
Comment
Question by:fathimasulthan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Accepted Solution

by:
kaushikgmehta earned 150 total points
ID: 8039697

<%@ page import ="java.sql.*" %>
<jsp:useBean id="conntodatabaseobject" scope="session" class="ConnectionToDatabase"/>
<%

try
{
String suppliernamestr=request.getParameter("suppliername");
String supplieraddressstr = request.getParameter("supplieraddress");
String phoneno1str=request.getParameter("phoneno1");
String phoneno2str=request.getParameter("phoneno2");
String phoneno3str=request.getParameter("phoneno3");
String phoneno4str=request.getParameter("phoneno4");


if(phoneno1str.length()==0)
{
phoneno1str="0";
}
if(phoneno2str.length()==0)
{
phoneno2str="0";
}

if(phoneno3str.length()==0)
{
phoneno3str="0";
}

if(phoneno4str.length()==0)
{
phoneno4str="0";
}

Connection conn=conntodatabaseobject.getConnection();
Statement stmt=conntodatabaseobject.getStatement(conn);

stmt.executeUpdate("insert into supplier_master values('"+suppliernamestr+"','"+supplieraddressstr+"',"+Integer.parseInt(phoneno1str)+","+Integer.parseInt(phoneno2str)+","+Integer.parseInt(phoneno3str)+","+Integer.parseInt(phoneno4str)+");");

conntodatabaseobject.closeConnectionToDatabase(conn,stmt);

}catch(Exception ex)
 {
      out.println("Exception in Done Add Branch "+ex);
 }
%>

<jsp:forward page="Main.jsp"/>


import java.sql.*;
public class ConnectionToDatabase
{

     public ConnectionToDatabase()
     {
     }

     public Connection getConnection() throws SQLException,ClassNotFoundException,InstantiationException,IllegalAccessException
     {
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     Connection conn=DriverManager.getConnection("jdbc:mysql://hostname:3306/DBNAME?user=USERNAME&password=PASSWORD");
     return conn;
     }

     public Statement getStatement(Connection conn) throws SQLException
     {
     Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
     return stmt;
     }

     public void closeConnectionToDatabase(Connection conn,Statement stmt)throws SQLException
     {
     stmt.close();
     conn.close();
     }
}

0
 

Expert Comment

by:kaushikgmehta
ID: 8039702
ConnectionToDatabase is a separate prg. Its a bean
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8041110
The pts should goto kaushikgmehta since he is the one that posted the right code first, here is some good refs:
http://oneworldhosting.com/sup_jspdb.html
http://www.eng.mu.edu/corlissg/Software/jspMysql.html

I prefer to use PreparedStatments when inserting data:

<%@ page import ="java.sql.*" %>
<%

try
{
    // assuming first field in an int
    int col1 = Integer.parseInt(request.getParameter("formfield1"));
    // assuming second field in a String
    String col2 = request.getParameter("formfield2");
    // assuming third field in an int
    int col3 = Integer.parseInt(request.getParameter("formfield3"));
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn=DriverManager.getConnection("jdbc:mysql://hostname:3306/DBNAME?user=USERNAME&password=PASSWORD");
    PreparedStatement ps = conn.prepareStatement("insert into yourtable (col1,col2,col3) values (?,?,?)");
    ps.setInt(col1);
    ps.setString(col2);
    ps.setInt(col3);
    int rowsInserted = ps.executeUpdate();
    ps.close();
    conn.close();

}catch(Exception ex)
{
     out.println("Exception:"+ex);
}
%>

CJ
0
 

Author Comment

by:fathimasulthan
ID: 8078207
THANKS
FAthima
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Check out the latest tech news, community articles, and expert highlights in August's newsletter.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question