query

query

code that manage updation n deletion in one page.... that means i wants update query n delete query that work in one page at onclick update and delete button...
View Answers

August 5, 2010 at 4:32 PM

Hi Friend,

Try the following code:

1)user.jsp
<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function editRecord(id){
window.open('http://localhost:8080/examples/jsp/edituser.jsp?id='+id,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes');
}
function deleteRecord(id){
window.open('http://localhost:8080/examples/jsp/deleteuser.jsp?id='+id,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes');
}
</script>
</head>
<body>

<br><br>
<table border="1">
<tr><th>FirstName</th><th>LastName</th><th>Address</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 register";
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:green;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td>
<td><input type="button" name="edit" value="Delete" style="background-color:red;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>

August 5, 2010 at 5:43 PM

Hi Friend,

Try the following code:

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

<html>
<body>
<form method="post">
<table border="1">
<tr><th>FirstName</th><th>LastName</th><th>Address</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 register";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>

<%
while(rs.next()){
%>
<tr><td><input type="hidden" value="<%=rs.getString(1)%>" name="id"><input type="text" value="<%=rs.getString(2)%>" name="fname"></td>
<td><input type="text" value="<%=rs.getString(3)%>" name="lname"></td>
<td><input type="text" value="<%=rs.getString(4)%>" name="address"></td>
<td><input type="text" value="<%=rs.getString(5)%>" name="email"></td>
<td><input type="submit" name="edit" value="Edit" style="background-color:green;font-weight:bold;color:#ffffff;" ></td>
<td><input type="submit" name="edit" value="Delete" style="background-color:red;font-weight:bold;color:#ffffff;" ></td>
</tr>
<%
}
%>
<%
}
catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>
<%
String id=request.getParameter("id");
String firstname=request.getParameter("fname");
String lastname=request.getParameter("lname");
String address=request.getParameter("address");
String email=request.getParameter("email");

String butt=request.getParameter("edit");
if(id!=null){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
Statement stmt=conn.createStatement();
if(butt.equals("Edit")){
stmt.executeUpdate("update register set firstname='"+firstname+"',lastname='"+lastname+"',address='"+address+"',email='"+email+"' where id='"+id+"'");
out.println("Data is updated successfully");
}
else if(butt.equals("Delete")){
stmt.executeUpdate("DELETE FROM register WHERE id = '"+id+"'");
out.println("Record is deleted successfully");

}
}
catch(Exception ex){}
}
%>

Thanks









Related Tutorials/Questions & Answers:
query in JDBC
query in JDBC  What are the steps required to execute a query in JDBC
Query conversion
Query conversion  can please convert the following mysql query to hibernate query select * from table1 where field1 not in (select field_1 from table3
Advertisements
query optimization
query optimization  hai ! i need a code in java for database query optimization.plz help
query string
query string  Need some examples of query strings.send me as soon as possible. Regards, R.Santhosh
Query Question
Query Question  How would I write a query to display the name, department of the employee that earns the highest salary in the Employees table
HQL Query
HQL Query  Hibernate query language: How to use a left join in Hibernate query language. Can somebody help with an example. I am getting a unexpected token error in HQL Query tool If I use joins
Sql Query
Sql Query  Is this query work in Db2 or not ? Select * from FORUM LIMIT 2
Sql Query
Sql Query  Is this query work in Db2 or not ? Select * from FORUM LIMIT 2
jdbc query
jdbc query  write a query of how to print the values of any column present in database in reverse order
database Query
database Query  i have to find common columns from two tables fron 2 differnt schemas what query to use? i used intersect but i am not getting how it is comapring two tables from different schema because in query we have
database Query
database Query  i have to find common columns from two tables from 2 different schemas what query to use? i used intersect but i am not getting how it is comparing two tables from different schema because in query we have
sql query
sql query  two tables are emp,loc write a query to display all the emp information whose locname not equal to bangalore
session into a query
session into a query  Hi all this am Sreedhar from Hyderabad i am calling one session variable in mysql query but its not working can any one help me please... here is the query.. $query = 'SELECT * FROM post WHERE post_date <
query problem
query problem  how write query in jsp based on mysql table field? i have employee table it contain designation field, how write query in jsp... to write this query in jsp please anybody help me, and send me that code
HQL query
HQL query  how to select only password based on username using HQL query from login table where uname and paswrd are two column names
SQL Query
SQL Query  Selecting the third highest salary of Employee without using any subquery?   Here is a query: SELECT salary FROM employee ORDER BY salary DESC LIMIT 2,1
Hibernate SQL Query/Native Query
This tutorial describes Hibernate SQL Query, which is also known as Native Query
query string
query string  on my servlet page i take the values of the field in a variable and that variable is a primary key so on the basis of that i am sorting... servlet page to different servlet jsp page and that i want to do with query string so
need query
need query  hi sir , i am beginner to sql.i need a query so that it can be helpful to my project. i have four tables each containing different fruits with different amounts. i need a query in such a way that when i say some
sql query
query plz help to me
sql query
query plz help to me
sql query
query plz help to me
Sql Query
Sql Query  Hi M Navjeet Ghai I want to know How to Show A name(Like Navjeet Ghai And Manpreet Singh Sodhi) of employees from a employee table as N.Ghai & M.S.Sodhi by using sql query
select query
select query  how to write select query with where clause having multiple variables. example: i want to select the data from DB in which i want to check againest two variable in where cluase. String sql = "select * from
SQL QUERY
Gopal oracle C Now i need to write a query "which
Query in jsp
Query in jsp  I have one textbox in jsp. when i will write any letter in that textbox i want all values which are starting from that letter in option from database. same as we are getting in google search textbox. Thank
dynamic query
dynamic query  DECLARE QUERY VARCHAR2(32767); FIN_QUERY VARCHAR2...; PORTFOLIO_LEVEL ARRAY; IDX NUMBER :=1; BEGIN QUERY..._LEVEL; IDX := IDX + 1; END IF; END LOOP; QUERY := QUERY
Query
query
query
Query
query
query
query
query
query
Query
Query
query
Query
query
query
query
query
query
query - SQL
tell me how to write the query in database but not jsp code. thank u
select query in mysql
select query in mysql  Select query in mysql. Can anyone
upgrading sql query join
upgrading sql query join  upgrading sql query join
Query to Test MySQL Performance
Query to Test MySQL Performance  Query to Test MySQL Performance
Hibernate Query - Hibernate
Hibernate Query  Hibernate Query

Ads