Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Insert  in JSP

Posted on 2003-02-27
4
Medium Priority
?
196 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Machine Learning is one of the profound applications of AI and therefore, just like AI, it is surrounded by myths and fears. Check out these facts about ML that demystify the related myths.
As a matter of fact, Outlook OST files are of much importance in relation to Exchange mailbox. OST files are independent as they are simply copy of data of a user’s mailbox on Exchange Server. Though, if the server’s status is changed or it is dama…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

564 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