Image saved into database not showing - java

So i'm tryna save a blob image into database and display it once i click"view" href. But I don't know why the pic cannot be seen. It saves in mysql database tho...
retrieve_image.jsp:
<%# page import="java.sql.*,model.Upload,java.io.*,java.util.*" %>
<HTML>
<table border="1">
<tr><th>ID</th><th>Image</th></tr>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/backup","root","mysql");
Statement stmt=con.createStatement();
String strQuery = "select * from contacts";
ResultSet rs = stmt.executeQuery(strQuery);
while(rs.next()){
%>
<tr>
<td><%=rs.getInt("contact_id")%></td>
<td>
<img src="RetrieveImages.java?id=<%=rs.getInt(1)%>" width="100" height="100">
</a></td>
</tr>
<%
}
rs.close();
con.close();
stmt.close();
}
catch(Exception e)
{
e.getMessage();
}
%>
</table>
</HTML>
RetrieveImages is a servlet which has request dispatcher that goes to...
<%#page import="java.sql.Blob"%>
<%#page import="java.io.OutputStream"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Connection"%>
<%# page import="model.Upload" %>
<%
String contact_id = request.getParameter("contact_id");
String dbURL = "jdbc:mysql://localhost/backup";
String dbUser = "root";
String dbPass = "mysql";
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(dbURL, dbUser, dbPass);
PreparedStatement ps = con.prepareStatement("select * from contacts where contact_id=?");
ps.setString(1, contact_id);
ResultSet rs = ps.executeQuery();
if(rs.next()){
Blob blob = rs.getBlob("photo");
byte byteArray[] = blob.getBytes(1, (int)blob.length());
response.setContentType("image");
OutputStream os = response.getOutputStream();
os.write(byteArray);
os.flush();
os.close();
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(con != null){
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
%>
In my java bean i have contact_id and photo as attribuites

Related

request.getParameter in jsp returns null or empty string

I made an html textbox for an input and want to connect this to database.
But everytime I make an input to the textbox and move on to the result page, the result only shows the name of the attribute but not any tuples.
I think the request.getParameter() returns null or empty string. I did several tries but cannot find any solution.
Here's my code.
This's selectTestForm.jsp
<%# page contentType="text/html; charset=utf-8" %>
<%# page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Select the game</title>
</head>
<body>
<p>Input opponent team</p>
<form name="form1" method="get" action="result.jsp">
<p>Opponent team : <input type="text" name="oppon"></p>
<p><input type="submit" name="Submit" value="send"></p>
</form>
</body>
</html>
And this's the result.jsp
<%# page contentType="text/html; charset=utf-8" %>
<%# page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Find the game</title>
</head>
<body>
<table width="500" border="1">
<tr>
<td width="100">Game ID</td>
<td width="100">Opponent Team</td>
<td width="100">Start Date</td>
</tr>
<%
String opponent = (String) request.getParameter("oppon");
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
System.out.println("Driver loading error");
}
try{
String jdbcUrl = "jdbc:oracle:thin:#localhost:1521:xe";
String userId = "sports_booking";
String userPass = "jade";
con = DriverManager.getConnection(jdbcUrl, userId, userPass);
String sql = "select * from game where opponent=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "opponent");
rs = pstmt.executeQuery();
while( rs.next() ) {
String game_id = rs.getString("game_id");
String start_date = rs.getString("start_date");
%>
<tr>
<td width="100"><%= game_id %></td>
<td width="100"><%= opponent %></td>
<td width="100"><%= start_date %></td>
</tr>
<%
}
}catch(SQLException e){
e.printStackTrace();
if(rs != null) {
try {
rs.close();
}catch(SQLException sqle) {}
}
if(pstmt != null) {
try {
pstmt.close();
}catch(SQLException sqle) {}
}
if(con != null) {
try {
con.close();
}catch(SQLException sqle) {}
}
}
%>
</table>
</body>
</html>
I'd appreciate if you help my problem thanks!
The problem is line pstmt.setString(1, "opponent");, as you set a const "opponent" string instead of the variable.
pstmt.setString(1, "opponent"); -> pstmt.setString(1, opponent); should work now.

how to use datatable in jsp ? after retriving data in table form from database

I have created a jsp file which fetchs data from the database in table form now i have to use datatable so please someone suggest how to do it . in output it shows table which contains data in form of row and column but my sir asked to use datatable in it . so it will be a great help if someone can answer
CODE -
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.Connection"%>
<%
String id = request.getParameter("userId");
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "xyz";
String userId = "1234";
String password = "1234";
try {
Class.forName(driverName);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<!DOCTYPE html>
<html>
<body>
<h2 align="center">Fetch Data From Database in Jsp</h2>
<table id="details" class="display" align="center" cellpadding="5" cellspacing="5" border="1">
<thead>
<tr>
<th><b>First Name</b></th>
<th><b>Last Name</b></th>
<th><b>Address</b></th>
</tr>
</thead>
<%
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="SELECT * FROM details";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tbody>
<tr>
<td><%=resultSet.getString("firstname")%></td>
<td><%=resultSet.getString("lastname") %></td>
<td><%=resultSet.getString("Address") %></td>
</tr>
</tbody>
<%
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
%>
</table>
</body>
</html>

INSERT query not woking

<body>
<%# page import ="java.sql.*" %>
<%# page import="java.sql.Statement" %>
<%# page import="javax.sql.*" %>
<%# page import="java.sql.Connection" %>
<%# page import="java.sql.DriverManager" %>
<%
String pwd = request.getParameter("pass");
String email = request.getParameter("email");
Statement st,stt = null;
Connection con = null;
int i=0;
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql://hostname","username", "Password");
stt = con.createStatement();
st = con.createStatement();
ResultSet rs;
rs = stt.executeQuery("select * from users where email='"+email+"'");
if (!rs.next()) {
ResultSet rss;
rss = st.executeQuery("insert into users(email,password) values ('"+email+"','"+pwd+"')");
if(rss.next()) {
response.sendRedirect("index.jsp");
out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
}else {
response.sendRedirect("reg.jsp");
out.print("Registration was not Successfull!"+"<a href='reg.jsp'>Go to Registration</a>");
}
}else {
response.sendRedirect("reg.jsp?error=Email already exsist!");
}
}catch (Exception e) {
e.printStackTrace();
}
%>
</body>
Use executeUpdate for inserts, updates & delete,
executeQuery for select operations

java - jsp update database row via form on submit

Hi im trying to update data from database using submit button , witch is located on a jsp page that shows data based on name and surname that you entered in login.jsp..
validationForDatabase.jsp shows data from the table pacijent,based on name and surname are entered in another jsp page. Fields name and surname are readonly so you can chage everything except those fileds, and when you make chages and press button Update it should redirect you to update.jsp. If the update is successful you sholud be redirected back to validationForDatabase.jsp but instead of doing that it shows me Badly entered data from else section.
Could you please guide me on how to fix this problem or maybe another way to do this.
Thanks in advance
validationForDatabase.jsp
`
<%
try{
String name= request.getParameter("name");
String surname= request.getParameter("surname");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/sth","root","");
PreparedStatement pst = conn.prepareStatement("Select name,surname from pacijent where name=? and surname=?");
pst.setString(1, name);
pst.setString(2, surname);
ResultSet rs = pst.executeQuery();
if(rs.next())
{
out.println("Name of the patient : "+name);
out.println("<br/>Surname of the patient : "+surname);
HttpSession sesija = request.getSession();
sesija.setAttribute("name", name);
sesija.setAttribute("surname", surname);
%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.Connection"%>
<%
String connectionUrl = "jdbc:mysql://localhost:3306/sth";
try {Class.forName("com.mysql.jdbc.Driver");}
catch (ClassNotFoundException e) {e.printStackTrace();}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<form action="Update.jsp" method="get">
<%
try{
connection = DriverManager.getConnection ("jdbc:mysql://localhost:3306/sth","root","");
statement=connection.createStatement();
String sql ="SELECT name,father,surname,birth,adress,phone,email,notes "+ "FROM pacijent where name='"+sesija.getAttribute("name")+"'and surname='"+surname+"';";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<p align="center"><%= sesija.getAttribute("name")%><%= sesija.getAttribute("surname")%></p>
<p><label class="uname">name</label><input name="name" readonly value="<%=sesija.getAttribute("name") %>"/></p>
<p><label class="uname">father</label><input name="father" value="<%=resultSet.getString("father") %>"/></p>
<p><label class="uname">surname</label><input name="surname" readonly value="<%=sesija.getAttribute("surname") %>"/></p>
<p><label class="uname">birth</label><input name="birth" value="<%=resultSet.getString("birth") %>"/></p>
<p><label class="uname">Adress</label><input name="adress" value="<%=resultSet.getString("adress ") %>"/></p>
<p><label class="uname">phone</label><input name="phone" value="<%=resultSet.getString("phone") %>"/></p>
<p><label class="uname" > E-mail</label><input name="email" value="<%=resultSet.getString("email") %>"/></p>
<p><label for="usernamesignup" class="uname">info</label>
<textarea name="notes" id="a" class="a" rows="4" cols="60"
textareaObject.value="text" ><%= resultSet.getString("notes") %></textarea></p>
<input type="submit" name="Submit" value="Update" />
</form>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
sesija.invalidate();
%>
<%
}
else{
%>
<br/><br/><br/><br/><br/><h1 align="center">Badly entered data</h1>
<%}
}
catch(Exception e){
out.println("Error "+e);
}
%>`
Update.jsp works in redirecting back but it wont update data.
Update.jsp
`
<%
String name=request.getParameter("name");
String father=request.getParameter("father");
String surname=request.getParameter("surname");
String birth=request.getParameter("birth");
String adress =request.getParameter("adress ");
String phone=request.getParameter("phone");
String email=request.getParameter("email");
String notes=request.getParameter("notes");
try {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sth", "root", "");
Statement st1 = null;
st1 = conn.createStatement();
HttpSession sesija = request.getSession();
sesija.setAttribute("name", name);
System.out.println(sesija.getAttribute(name));
st1.executeUpdate("UPDATE pacijent SET name= '"+name+"',father= '"+father+"',"
+ "surname= '"+surname+"',birth= '"+birth+"',adress = '"+adress +"',"
+ "phone= '"+phone+"',email = '"+email+"',notes = '"+notes+"'"
+ "WHERE name="+sesija.getAttribute(name));
response.sendRedirect("validationForDatabase.jsp");
} catch (Exception e) {
out.println("Error : " + e);
}
%>`
Thanks a lot.

No output on browser from my jsp page?

second.jsp
<%#page import="java.sql.*" %>
<html>
<body>
<%
try{
String id2=(request.getParameter("ID1"));
String url="jdbc:ucanaccess://C:\\Users\\Asim Iqbal\\Documents\\IT.accdb";
Connection conn = DriverManager.getConnection(url);
String sql= "SELECT * FROM Student WHERE ID=?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,id2);
ResultSet rs = stmt.executeQuery();
if (rs.next())
{
int i=rs.getInt("ID");
String nam=rs.getString("Name");
String clas=rs.getString("Class");
String ph=rs.getString("Phone");
%>
<h1><%=i%></h1>
<h1><%=nam%></h1>
<h1><%=clas%></h1>
<h1><%=ph%></h1>
<%
}
} catch (SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>
No output comes from DB in the browser..
Please give me suggestion where i am getting wrong...

Categories

Resources