java code to insert select at run time....????

java code to insert select at run time....????

java database code to retrieve data at runtime and please give codes for insert delete too.... using jsp and withot jsp

View Answers

March 15, 2012 at 5:37 PM

1)application.jsp:

<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function editRecord(id){
    var f=document.form;
    f.method="post";
    f.action='edit.jsp?id='+id;
    f.submit();
}
function deleteRecord(id){
    var f=document.form;
    f.method="post";
    f.action='delete.jsp?id='+id;
    f.submit();
}
</script>
</head>
<body>

<br><br>
<form method="post" name="form">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="root";

int sumcount=0;
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db,userName,password);
String query = "select * from employee";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>

<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td>
<td><input type="button" name="delete" value="Delete" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>

2)edit.jsp:

<%@page language="java"%>
<%@page import="java.sql.*"%>
<form method="post" action="update.jsp">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String query = "select * from employee where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
%>
<tr>
<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
<td><input type="text" name="address" value="<%=rs.getString("address")%>"></td>
<td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td>
<td><input type="text" name="email" value="<%=rs.getString("email")%>"></td>
<td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
</tr>
<%
}
}
catch(Exception e){}
%>
</table>
</form>
</form>

March 15, 2012 at 5:38 PM

continue..

3)update.jsp:

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

<%
String ide=request.getParameter("id");
int num=Integer.parseInt(ide);
String name=request.getParameter("name");
String address=request.getParameter("address");
int contact=Integer.parseInt(request.getParameter("contact"));
String email=request.getParameter("email");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement st=null;
st=conn.createStatement();
st.executeUpdate("update employee set name='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'");
response.sendRedirect("/examples/jsp/application.jsp");
}
catch(Exception e){
System.out.println(e);
    }
%>

4)delete.jsp:

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

String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM employee WHERE id = '"+no+"'");
response.sendRedirect("application.jsp");
}
catch(Exception e){}
%>

March 15, 2012 at 5:40 PM

If you want the application Java Swing then

Visit Here


March 15, 2012 at 5:44 PM

You can also visit the following link:

http://www.roseindia.net/jsp/user-search.shtml


March 15, 2012 at 5:49 PM

thanx..... i ll try it out









Related Tutorials/Questions & Answers:
java code to insert select at run time....????
Run this code..plzz - Java Beginners
Advertisements
Java code to run unix command - Java Beginners
insert code jsp to access
code for insert button
run php code online
Run a simple EJB code
Run a simple EJB code
insert code using spring - Spring
help in insert code - JSP-Servlet
please let me get code how to insert a data to mysql using setter and getter method by using java
please let me get code how to insert a data to mysql using setter and getter method by using java
code for insert the value from jsp to access database
Code to insert date in to database - Development process
how to insert, retrieve data from,to db(code)....
Java code
I am new to java applets. When i run this code on applet viewer status is displayed that applet not initialized
how to insert, retrieve data from,to db(code)....
to run html code in web browser - JSP-Servlet
Applet run with appletviewer but not in browser, any code problem.
select with preparedstatement in java
Java Multiple Insert Query
can i know the error in this code... am unable to run this code
insert multiple selection - Java
Insert a row in 'Mysql' table using JSP Code
Mysql Nested Select
insert
java run time error - JDBC
Java run time polymorphism
best os to run java
how to run - Java Beginners
java not run on my pc
Insert Image into Mysql Database through Simple Java Code
compile and run java program
java pages run
how to run java program
this is my code but i cant able to run it wt is the problem
this is my code but i cant able to run it wt is the problem
How to run ActionListenters in two different thread for the following code...
How to run ActionListenters in two different thread for the following code...
How to run ActionListenters in two different thread for the following code...
How to run ActionListenters in two different thread for the following code...
java-select dynamic files. - Java Beginners
ATM Java Code - Java Beginners
Program to find no of metrics in a file ----- Java code
Java error code
java code
Insert or retrieve image into oracle 10g by using java
insert checkbox in cell using POI api in java
run a java application - Java Beginners

Ads