954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to update database using mysql and jsp

can somebody prove me the coding to update the database (mysql) with JSP

nikhita
Newbie Poster
15 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Google is your friend! There are many examples over the internet, for example this site http://www.java2s.com/Tutorial/Java/0340__Database/Catalog0340__Database.htm

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 

U can write all code using scriptlet. Simply start scriptlet,create connection, write and execute update query, close connection and scriptlet.
For Ex
<%
create connection
Create query
resultset
execute query
close connection
%>
Hope it provides u some help!!

U can find example code in google.

Shubhra
Newbie Poster
7 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

U can write all code using scriptlet. Simply start scriptlet,create connection, write and execute update query, close connection and scriptlet. For Ex <% create connection Create query resultset execute query close connection %> Hope it provides u some help!!

U can find example code in google.

Which is, of course, exactly the wrong way to do it, reagardless of whether it works or not.

You can also scratch your back with a butcher knife, but I wouldn't suggest it.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Ya OK.
What I provided was actually the short cut.
Correct way is to do all such stuff in class. Create object in jsp and simply execute the method!!

Am I correct now??

Shubhra
Newbie Poster
7 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Well, if you knew the right way, why did you suggest the wrong way?

As I said:

You can also scratch your back with a butcher knife, but I wouldn't suggest it.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Actually I wrote what came first to my mind.
Your comment stroke me to give a2nd thought.
I apologise.

Shubhra
Newbie Poster
7 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

the link you provided was helpful. thanks a lot.

sorry for the delay in reply..

One more help needed....

can u tel me how make a jsp page printer friendly

nikhita
Newbie Poster
15 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
can somebody prove me the coding to update the database (mysql) with JSP

-----------------------html.jsp---------------------------

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="post" action="om.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="22%">&nbsp;</td>
    <td width="78%">&nbsp;</td>
    </tr>
  <tr>
    <td>Name Student </td>
    <td> to<input type="text" name="studentName"></td>
    <td>from<input type="text" name="studentName1"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
</table>
</form>
</body>
</html>

-----------------------------om.jsp-----------------------

<html> 
<head> 
<title>result:</title> 


<%@ page import="java.io.*"%> 
<%@ page import="java.io.File"%> 
<%@ page import="java.util.*"%> 
<%@ page import="java.sql.*"%> 
<%@ page import="java.sql.Blob"%> 
<%@ page import="java.sql.PreparedStatement"%> 
<%@ page import="java.sql.BatchUpdateException"%> 
<%@ page import="javax.servlet.*"%> 
<%@ page import="javax.servlet.http.*"%> 

<% 

try 
{ 

boolean autho;

//String driver = "org.gjt.mm.mysql.Driver"; 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
Connection con=null; 
ResultSet rs=null; 
Statement stmt=null; 


String url="jdbc:mysql://localhost:3306/om?user=root&password=omshakthi"; 

out.println("no1");
con=DriverManager.getConnection(url); 

stmt=con.createStatement(rs.TYPE_SCROLL_SENSITIVE,rs.CONCUR_UPDATABLE);


String name=request.getParameter("studentName");

String name1=request.getParameter("studentName1");

//stmt.executeUpdate("insert into om1(name) values('"+name+"')"); //insert


//stmt.executeUpdate("update om1 set name='"+name+"' WHERE name='ykuy'");//update


//name: is where u enter as get parameter
//ykuy: is some store value in mysql


//rs = stmt.executeQuery("SELECT * FROM om1"); // select all

rs = stmt.executeQuery("SELECT * FROM om1 WHERE name='"+name+"'");//use of where


if(!rs.next()) 
{
autho=false;
//out.println("not autorized");
}
else
{
autho=true;
//out.println("autorized");
}


rs = stmt.executeQuery("SELECT * FROM om1 WHERE name='"+name1+"'");


if(!rs.next()) 
{
out.println("not autorized");
}
else if(autho==false) 
{
out.println("not autorized");
}
else
{
out.println("autorized");
out.println("<h1>new mail from   "+name1+"   to   "+name+"</h1>"); 
}



/*

while (rs.next()) 
{
out.println("no2");
out.println(rs.getString("name")+"");
}*/


rs.close();
}


catch (Exception e) 
{ 
out.println("no3");
//out.println("<center><font size=5> Error in inserting the data</font></center>"); 
out.println("the error is:"+e.toString()); 
} 


%> 
</body> 
</html>
jagadesh_pesit
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

@jagadesh_pesitYou are just some 3 years to late with your reply
You used no code tags
You show database connectivity from JSP that is frown upon (bad practice)
I can go on, but I rather close this thread as unproductive and sample of bad practises. You want to learn better? Then check 2 posts on the top of JSP section for more info...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You