Insert text into database table using JSP & jQuery


 

Insert text into database table using JSP & jQuery

In this tutorial , the text is inserted into database table using JSP & jQuery.

In this tutorial , the text is inserted into database table using JSP & jQuery.

Insert text into database table using JSP & jQuery

In this tutorial , the text is inserted into database table using JSP & jQuery. In the below example, the first JSP page is use to display text box in which we type data. It also contain jQuery script ,to connect to another JSP page and show result using "fadeIn" effect. The second JSP page contains code for connectivity and fetching data from database table.

insert.jsp

<%@ page language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>GET VALUE FROM TABLE</title>
<script src="jquery-1.4.2.js" type="text/javascript"></script>
<script src="insert.js" type="text/javascript"></script>
</head>
<body>
<form id="form" method="post">
<h3>Send message to Administrator(Plz Type below)</h3>
<textarea cols="30" rows="2" type="text" id="content" maxlength=
"145" >
</textarea><br />
<input id="submit" type="submit" value="Send Message"/>
<input id="reset" type="reset" value="Reset"/>
</form>
<p id="result"></p>
<body>
</html>

insert.js

$(document).ready(function() {
$('#form').submit(function(){
var content= $('#content').val();
$.ajax({type:"post",url:"demo.jsp",data:"content="
+content,success:function(msg){$('#result').hide();
    $("#result").html("<h3>" + msg + "</h3>").fadeIn("slow"); } });
return false;
});
});

demo.jsp

<%@ page language="java" import="java.sql.*" errorPage="" %>
<h2>Showing Messages Stored in database</h2>
<%
String cont=request.getParameter("content");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection

("jdbc:mysql://192.168.10.13:3306/ankdb", "root", "root");
Statement st = con.createStatement();
int i=st.executeUpdate("insert into messages(msg)values

('"+request.getParameter("content")+"')");
ResultSet rs=st.executeQuery("select * from messages");
if(rs.next())
{
while(rs.next())
{
String message=rs.getString("msg");
%>

<li>
<div align="left">
<span >
<%
out.println(message);
%> </span>
</div>
</li>
<%
}
}
else{
out.println("No Records Found");}
}
catch(Exception e){
Exception ex = e;
out.println("Mysql Database Connection Not Found");
}
%>

OUTPUT

After typing message and pressing button "Send Message" :

Download Source Code

Ads