Mysql/jsp Update query - mysql

what iam trying to do is get field parameter and run mysql query to update(decrement) value from field quantity, can any one tell me how to do that?
<form method="post">
<table class="bordered">
<thead>
<tr>
<th>No</th>
<th>Drug Name</th>
<th>Strength</th>
<th>Quantity</th>
<th>Dis Qty</th>
</tr>
</thead>
<% try {
String query="select * from drugs";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(query);
while(rs.next()) {
%>
<tr>
<td><%=rs.getInt("drugid") %></td>
<td><%=rs.getString("drugname") %></td>
<td><%=rs.getString("strength") %></td>
<td><%=rs.getInt("drugquant") %></td>
<td><INPUT TYPE="TEXT" NAME="getint" SIZE="7">
<INPUT TYPE="SUBMIT" VALUE="Dispense" NAME="B1"></td> </tr>
<%
} %> </table>
<%
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
![enter image description here][1]
%>
</form>
I tryed to use code similar to this but it doesnt work for me :(
<%
String getint = request.getParameter("getint");
String getsize = "";
// check if Update button is clicked
if(request.getParameter("B1") != null) {
// get what user enterd in intbox
getint = request.getParameter("getint");
// DEBUG
PreparedStatement pstmt4;
pstmt4 = conn.prepareStatement("UPDATE drugs SET drugquant=drugquant - "+getint+" WHERE drugid=1");
pstmt4.executeUpdate();
}
%>

you should add and change some following line:
pstmt4 = conn.prepareStatement("UPDATE drugs SET drugquant=?"+"WHERE drugid=?");
INT drugquant = request.getParameter("drugquant");
INT drugid = request.getParameter("drugid");
pstmt4.setString(1, drugquant);
pstmt4.setString(2, drugid);
pstmt4.executeUpdate();
if you had any doubt you should follow this link and give me comment.

Related

How to get checkboxes values for JSP page?

I can get a single value for an item in jsp page by id. But I can't able to get checkboxes value. How can I get checkboxes value by id or anything else. when i will click in order page it will collect all products name and product price
Screenshot
Code is:
<table class="table table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Choose Product</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%
String Host = "jdbc:mysql://localhost:3306/shopbilling";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(Host, "root", "");
statement = connection.createStatement();
String query = request.getParameter("q");
String data;
if(query != null)
{
data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
}
else
{
data = "select * from products_tbl";
}
rs = statement.executeQuery(data);
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("id")%></td>
<td> <input type="checkbox" /> </td>
<td><%=rs.getString("product_name")%></td>
<td><%=rs.getString("product_price")%></td>
<td class="text-center" width="250">
Edit
Delete
</td>
</tr>
<%
}
%>
</tbody>
</table>
Order
Here you can use value tag of <input type="checkbox"> to put the data to user.Then you need to give a attribute 'name' to all of these check-boxes, which you can keep same and then you can grab all the selected values as an array.
Here is a changes you need to make in this code::
<table class="table table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Choose Product</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<%
String Host = "jdbc:mysql://localhost:3306/shopbilling";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(Host, "root", "");
statement = connection.createStatement();
String query = request.getParameter("q");
String data;
if(query != null)
{
data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
}
else
{
data = "select * from products_tbl";
}
rs = statement.executeQuery(data);
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("id")%></td>
<td> <input type="checkbox" name="products" value ="<%=rs.getString("id")%>" /> </td>
<td><%=rs.getString("product_name")%></td>
<td><%=rs.getString("product_price")%></td>
<td class="text-center" width="250">
Edit
Delete
</td>
</tr>
<%
}
%>
</tbody>
</table>
Order
And while receiving the result you need to use something like this::
<%
String products[] = request.getParameterValues("products");
if (products!= null && products.length != 0) {
out.println("You have selected: ");
for (int i = 0; i < products.length; i++) {
out.println(products[i]);
}
}
%>
Hope this helped you!!
Happy Coding:)

Best way to check jQuery POST method parameters for reducing jsp prepared statement query extra function

How can I make Submit TRUE result I mean from 2 INPUT field how to POST only one field of value to same JSP page like
<% string param=req.......
The most important thing is my PreparedStatement of jdbc have to be written twice for reason of two fields ... for checking Oracle DB of EMPLOYEE NO || NAME
By using Name field it's working fine and after changing of code by using Number it's working fine but I want you to help me is there any good way to code this stuff
this is HTML Form
<form id="contact" method="get">
<b>Employee No: </b> <input type="text" id="contact_number" size="15" name="number"/>
<b>Employee Name:</b> <input type="text" id="contact_name" size="25" name="name"/>
<input type="submit"/>
</form>
This is validation jQuery
$(document).ready(function() {
$("#contact").submit(function() {
if (($('#contact_number').val().length !== 0) && ($('#contact_name').val().length !== 0)) {
return false;
} else if (($('#contact_number').val().length === 0) && ($('#contact_name').val().length === 0)) {
return false;
} else {
return true;
}
});
});
This is complete JSP & String sql="Oracle query" & JDBC
<%String number = request.getParameter("number");
String name = request.getParameter("name");
if (name != null) { %>
<h3 id="myDiv"> Search results for <i> <%= name %> </i> </h3>
<% }
else { %>
<h4> Enter any Information on above field ... </h4>
<% } %>
<br>
<%#page import="java.sql.*" %>
<%Class.forName("oracle.jdbc.driver.OracleDriver");
String sql="SELECT * FROM RWEMP WHERE ENAME= ?";
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","hari","root");
PreparedStatement stat=con.prepareStatement(sql);
stat.setString(1,name);
ResultSet rs=stat.executeQuery();
try {
if(rs!=null) {
%>
<table class="gridtable" border=1 cellspan=60 cellpadding=16>
<tr>
<th> Emp ID </th>
<th> Emp Name </th>
<th> Emp Dept </th>
<th> Emp D.o.B </th>
<th> Show Record </th>
<th> Record Update </th>
<th> Compassionate </th>
</tr>
<%
while(rs.next()) {
%>
<tr>
<td><%= rs.getString("EID")%> </td>
<td><%= rs.getString("ENAME") %> </td>
<td><%= rs.getString("EDEPT")%> </td>
<td><%= rs.getString("EDOB")%> </td>
<td><input class="ui-button" type="button" onSubmit="result.jsp" target="destination" value="Show"></td>
<td><input class="ui-button" type="button" onclick="update.jsp" target="destination" value="update"></td>
<td><input class="ui-button" type="button" onclick="compassionate.jsp" target="destination" value="Compassionate"></td>
</tr>
<%
}
}
}
catch(SQLException e) {
e.printStackTrace();
}
con.close();
%>
</table>
For more Viewing Look at this Fiddle's HTML output only
How can i make Submit TRUE result ... i mean from 2 INPUT field .. how
to POST only one Field of Value to same JSP page ... like :- <% string
param=req.......
To do that put a dropdown which say select the criteria of search in your case is number and name and then provide him with a input field so when you submit form you have both columname and data to the next page and you dont have write 2 queries.Form should be like this
If possible help me that if user Click on Particular userS table row
... how to SEND that user row details to others jsp page ... for
complete record viewing or record editing purpose
Solution :Pass id of that row to other page
Create a column on each row name say Detail and do
<a href="/detailed.jsp?user_id="<%=user_id%>>Detail</a>
On detailed.jsp get value of user_id from get parameter and the apply the query with user_id to get all the details of that row

Retrieve values from jsp table

I'm new to jsp and I'm trying to present the results of my database to a table in my jsp page. This looks like it works well as it presents all the elements correctly ! The problem is that I also want to to have a separate button and when I'm clicking it , I want the satatus of the line the button belongs to , to be converted from "unconfirmed" to "confirmed", but when I'm pressing it all the records turned to "confirmed" and not only the one the button belongs to !
My jsp code:
<table border="1">
<tr>
<th>Username</th>
<th>Role</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Phone</th>
<th>Status</th>
</tr>
<%
while(rs.next()){%>
<td> <%=rs.getString("username") %></td>
<td> <%=rs.getString("role") %></td>
<td> <%=rs.getString("firstname") %></td>
<td> <%=rs.getString("lastname") %></td>
<td> <%=rs.getString("email") %></td>
<td> <%=rs.getString("phone") %></td>
<td> <%=rs.getString("status") %></td>
<% if(rs.getString("status").equals("unconfirmed")){%>
<td><input type="button" name="users" onclick="<%Bean.changeStatus(rs.getString("username"));%>"</td>
<% }
else{ %>
<td>Check</td>
<% } %>
</tr>
<% } %>
</table>
And .java code:
public void changeStatus(String us){
ResultSet rs = null;
Statement state = null;
String query = null;
try{
state=this.getConn().createStatement();
query="update users set status='confirmed' where username='"+us+"'";
int rowsEffected = state.executeUpdate(query);
}catch(SQLException e){
}
}
Put a single listener on the table. When it gets a click from an input with a button that has a name of "unconfirmed" and value "unconfirmed", change its value to "confirmed". Get rid of the input's id (they aren't used for anything here), or make them all unique.
<script type="text/javascript">
function handleClick(evt) {
var node = evt.target || evt.srcElement;
if (node.name == 'unconfirmed') {
node.value = "confirmed";
}
}
</script>
<table id="table1" border="1" onclick="handleClick(event);">
<thead>
<tr>
<th>Select
</thead>
<tbody>
<tr>
<td>
<form name="f1" action="#" >
<input id="UnConfirmed1" type="submit" name="unconfirmed" value="UnConfirmed">
</form>
<tr>
<td>
<form name="f2" action="#" >
<input id="UnConfirmed2" type="submit" name="unconfirmed" value="UnConfirmed">
</form>
<tr>
<td>
<form name="f3" action="#" >
<input id="UnConfirmed3" type="submit" name="unconfirmed" value="onfirmed">
</form>
</tbody>
</table>

Pass dynamic checkbox values to next JSP page

I have aproblem. Here I am showing some bills from Invoice table in mysql. And for single bill I am keeping a checkbox. I want to pass selected checkbox values to next jsp page. Please help me to do it. Thanks in advance.
<%
String company_name=request.getParameter("c_name");
try
{
Integer count=0;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_fleet","root","aadi");
Statement st = con.createStatement();
String sql="select invoice_no, invoice_date, gross_amount from tbl_invoice where client='"+company_name+"'";
ResultSet rs=st.executeQuery(sql);
%>
<center>
<table id="show_table">
<tr>
<td>PNR No</td>
<th>Date</td>
<th>Amount</td>
<th>Select</td>
</tr>
<%
while(rs.next())
{
%>
<tr>
<td><%=rs.getInt(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getInt(3)%></td>
<td><input type="checkbox" name="ck"/></td>
</tr>
%>
</table>
</center>
<%
}
catch(Exception e)
{
out.println(e);
}
%>
Hey I found the solution. On action page where I am going to receive values I will store all checkbox names in a array of string.
String selected_Checkboxes=request.getParameterValues("ck");
I will just change "td" tag of checkbox. I will put value of PNR NO which is coming from database and will put in checkbox.
<td><input type="checkbox" value="<%=rs.getInt(1)%>" name="ck"/></td>
I will use indexes of "selected_Checkboxes" to do further coding.
--Thanks Santino 'Sonny' Coleone

How to export JSP table to EXCEL (XLSX Format)

<%# page language="java" contentType="application/vnd.ms-excel"%>
<% response.setHeader("Content-Disposition:","attachment;filename=Sample.xls"); %>
<TABLE>
<TR><TD>2</TD></TR>
<TR><TD>3</TD></TR>
<TR><TD>5</TD></TR>
</TABLE>
How to export the JSP table in XLSX format and how to suppress the following exception
The file you are trying to open, name.ext, is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
use this
html code
<!DOCTYPE html>
<html>
<head>
<title>tableToExcel Demo</title>
<script>
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
</head>
<body>
<h1>tableToExcel Demo</h1>
<p>Exporting the W3C Example Table</p>
<input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel">
<table id="testTable" summary="Code page support in different versions of MS Windows." rules="groups" frame="hsides" border="2"><caption>CODE-PAGE SUPPORT IN MICROSOFT WINDOWS</caption><colgroup align="center"></colgroup><colgroup align="left"></colgroup><colgroup span="2" align="center"></colgroup><colgroup span="3" align="center"></colgroup><thead valign="top"><tr><th>Code-Page<br>ID</th><th>Name</th><th>ACP</th><th>OEMCP</th><th>Windows<br>NT 3.1</th><th>Windows<br>NT 3.51</th><th>Windows<br>95</th></tr></thead><tbody><tr><td>1200</td><td style="background-color: #00f; color: #fff">Unicode (BMP of ISO/IEC-10646)</td><td></td><td></td><td>X</td><td>X</td><td>*</td></tr><tr><td>1250</td><td style="font-weight: bold">Windows 3.1 Eastern European</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1251</td><td>Windows 3.1 Cyrillic</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1252</td><td>Windows 3.1 US (ANSI)</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1253</td><td>Windows 3.1 Greek</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1254</td><td>Windows 3.1 Turkish</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td></tr><tr><td>1255</td><td>Hebrew</td><td>X</td><td></td><td></td><td></td><td>X</td></tr><tr><td>1256</td><td>Arabic</td><td>X</td><td></td><td></td><td></td><td>X</td></tr><tr><td>1257</td><td>Baltic</td><td>X</td><td></td><td></td><td></td><td>X</td></tr><tr><td>1361</td><td>Korean (Johab)</td><td>X</td><td></td><td></td><td>**</td><td>X</td></tr></tbody><tbody><tr><td>437</td><td>MS-DOS United States</td><td></td><td>X</td><td>X</td><td>X</td><td>X</td></tr><tr><td>708</td><td>Arabic (ASMO 708)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr><tr><td>709</td><td>Arabic (ASMO 449+, BCON V4)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr><tr><td>710</td><td>Arabic (Transparent Arabic)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr><tr><td>720</td><td>Arabic (Transparent ASMO)</td><td></td><td>X</td><td></td><td></td><td>X</td></tr></tbody></table>
</body>
</html>
Use this way:
<%# page import="java.sql.* " %>
<%
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=SimDetails.xls");
Connection con = null;
PreparedStatement ps=null;
ResultSet rs = null;
%>
<%# include file="directconnection.jsp"%>
<%
int simid=0;
int net=0;
String sim="";
String mob="";
String contnum="";
String couridate="";
String sendaddr="";
String networkName="";
String contpe="";
try{
%>
<table>
<tr>
<th width="5%">SNo</th>
<th width="30%;">Network</th>
<th width="30%;">Sim Number</th>
<th width="30%;">Mobile Number</th>
<th width="30%;">Sender Address</th>
<th width="30%;">Contact Person Number</th>
<th width="30%;">Contact Person Name</th>
</tr>
<%
String strSimExportQuery=" SELECT SM_SIM_ID,SM_PROVIDER_ID,SM_SIM_NUMBER,SM_MOBILE_NUMBER,"+
" SM_SENDER_ADDRESS,SM_CONTACT_PERSON_NO,SM_CONTACT_PERSON FROM RS_SIM_MASTER ";
ps=con.prepareStatement(strSimExportQuery);
rs=ps.executeQuery();
while(rs.next()){
simid++;
net=rs.getInt("SM_PROVIDER_ID");
if(net==1){
networkName="Vodafone";
}
sim=rs.getString("SM_SIM_NUMBER");
mob=rs.getString("SM_MOBILE_NUMBER");
sendaddr=rs.getString("SM_SENDER_ADDRESS");
contnum=rs.getString("SM_CONTACT_PERSON_NO");
contpe=rs.getString("SM_CONTACT_PERSON");
%>
<tr>
<td><%=simid%></td>
<td><%=networkName%></td>
<td><%=sim%></td>
<td><%=mob%></td>
<td><%=sendaddr%></td>
<td><%=contnum%></td>
<td><%=contpe%></td>
</tr>
<%}%>
</table>
<%
}catch(Exception e){
out.println(e);
}
finally {
if (rs!= null){ rs.close(); rs=null;}
if(ps!=null){ps.close();ps=null;}
if (con != null) {con.close();con = null;}
}
%>