I have to get the images from the database (oracle 10g) and display them, but I am unable to display the images. My code is:
<%
String[] salespersons;
String[] name;
String[] photoid;
String tempid="", tempname="", tempphoto="";
ResultSet rs = statement.executeQuery("select * from Employees where ManagerID=" + managerid);
while(rs.next()) {
tempid += rs.getInt("id") + ";";
tempname += rs.getString("firstname") + ";";
tempphoto += rs.getString("photoid") + ";";
}
salespersons = tempid.split(";");
name = tempname.split(";");
photoid = tempphoto.split(";");
%>
<input type="button" value="CLOSE" onclick="window.close()" />
<table border="5" cellpadding="2" align="center">
<%
for(int ite=0; ite < name.length; ite++) {
%>
<tr>
<td><%=salespersons[ite]%></td>
<td><%=name[ite]%></td>
<td><img src="<%=photoid[ite]%>" alt="image not available"/></td>
<td></td>
<%
}
%>
</tr>
</table>
Image has a wrong path than only image not showing first print image path and then put it as a image url...
Related
I am displaying data is a jsp page from "resultset.getString("column_name")" attribute which works fine for my columns.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# page import="java.io.*"%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%# page import="java.text.*"%>
<%# page import="javax.servlet.*"%>
<%# page import="javax.servlet.http.*"%>
<%# page import="javax.servlet.http.HttpSession"%>
<%# page language="java"%>
<%# page session="true"%>
<%# page import="java.sql.*"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.Connection"%>
<%#page import="javax.servlet.ServletOutputStream"%>
<style>
th, td {
padding: 15px;
}
</style>
<%
String id = request.getParameter("name");
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "employee";
String userId = "root";
String password = "root";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String iurl1=null;
Blob employee_dp=null;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<h2 align="center"><font><strong>Retrieve data from database in jsp</strong></font></h2>
<%
try {
connection = DriverManager.getConnection(connectionUrl + dbName, userId, password);
statement = connection.createStatement();
String sql = "SELECT * FROM employee_details where ID='" + session.getAttribute("ID") + "'";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
%>
<table border="0" class="display" >
<tbody>
<tr>
<td>ID : </td>
<td><%=resultSet.getString("ID")%></td>
</tr>
<tr>
<td>First Name : </td>
<td><%=resultSet.getString("name")%></td>
</tr>
<tr>
<td>Last Name : </td>
<td><%=resultSet.getString("Last_Name")%></td>
</tr>
<tr>
<td>D.O.B. : </td>
<td><%=resultSet.getString("DOB")%></td>
</tr>
<tr>
<td>image : </td>
<td><img alt="Smiley face" src="<%=resultSet.getString("employee_dp") %>" width="500" height="500"/></td>
</tr>
<tr>
<td>Attach address proof </td>
<td> <input type="file" name="txtfile"> </td>
</tr>
</tbody>
</table>
<%}}
catch (Exception e) {
out.println("DB problem");
return;
}
finally {
try {
resultSet.close();
statement.close();
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
%>
Here is the code for my jsp displaying page.
Driver code to this is a basic login page which redirects to my this jsp page.
I changed the code a bit and tuned it to see if my image is available or not.
I tried by just giving the direct destination under "src" tag , it worked but when i retrieve it through resultSet.getString(column_name) it shows bits of symbols.
If i use .getBlob I get a empty image box
If i use .getString I get a lot of symbols
I am able to float the two tables, one is on the left side and one is on the right side. However, the table size on the left is not symmetrical to the table on the right. I am unable to display the table size equally. What should i do to display the table side by side equally?
html
html file
<div class="floatleft">
<div class="left">
<div class="row">
<div class="col-md-3">
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String connURL = "jdbc:mysql://localhost/test?user=1234&password=1234";
conn = DriverManager.getConnection(connURL);
String sql = "select * from test";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
%>
</div>
</div>
<div>
<p<u>Arm</u></a></font></p>
<table>
<tr>
<th >Arm Exercises</th>
<th >Number of times</th>
<th colspan='1'> </th>
<%
String sql1="";
{
sql1 = "select * from test";
PreparedStatement pstmt1=conn.prepareStatement(sql1);
ResultSet rs1 = pstmt1.executeQuery();
while(rs1.next()){
String id = rs1.getString("ID");
out.println("<tr>");
out.println("<td style = 'width: 85%'>");
out.println(arm);
out.println("</td>");
%>
<td>
<textarea cols="2"></textarea>
</td>
<%
out.println("</tr>");
}
conn.close();
}
%>
</table><br>
</div>
Just put the table in the div element with a class name.
<div class="floatleft">
<table>
</table>
<div class="floatright">
<table>
</table>
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.
<%# 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;}
}
%>
I have a CSV file which has lastname and first name, i have a jsp file to to retrieve the data from CSV file. This is what I have done so far:
<body>
<%
String file = "C:\\Users\\user\\Desktop\\file.csv";
String line;
int count = 0;
int i = 0;
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
%>
<table border=0>
<%
while((line = dis.readLine())!=null) {
%>
<tr>
<%
String[] str = line.split(",");
for(int j=0; j<str.length; j++) {
%>
<td>
<%
out.print(" " + str[j] + " ");
%>
</td>
<%
}
%>
</tr>
<%
//out.println("<br>");
i++;
}
%>
</table>
</body>
</html>
But I need to sort the table based on last name, so what should I do in jsp file?
You may want to store first and last name into a List and then can sort that List Using Collection.sort method and displaying that list straight away in your jsp.
BTW you should not do this thing inside scriplet tag ,instead you can do all this inside a servlet class.In place of hardcoding filepath you should use something like property files .