0 Replies - 1412 Views - Last Post: 03 December 2009 - 03:03 AM

#1 chandumalur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-November 09

Unable to upload a picture to the mysql database using jsp and servlet

Post icon  Posted 03 December 2009 - 03:03 AM

Hello,

I tried to upload a picture to the mysql database and i had written code to fetch picture from the database. but I am not able to upload a picture. can anybody help me? The code is as follows
<code>
<!--html to get picture from the user-->
<html>
<head>
<title>first page</title>
</head>
<body>
<form name="first" action="http://localhost:8080/chandu/addvalues">
<label>regno</label>
<input type="text" name="reg" value=""><hr>
<label>name</label>
<input type="text" name="name" value=""><hr>
<label>add picture</label>
<input type="file" name="filepic" value="">
<hr>
<input type="submit" name="add" value="add">
</form>
</body>
</html>
</code>
<code>
//servlet to add picture to the database
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.lang.String;
public class addvalues extends HttpServlet
{
static Connection conn;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","root");
response.setContentType("text/html");
String regno=request.getParameter("reg");
String uname=request.getParameter("name");
String pic=request.getParameter("filepic");
String hid=request.getParameter("hidd");
System.out.println(pic);
String query="insert into table1(regno,name,pic) values(?,?,?)";
PreparedStatement s=conn.prepareStatement(query);
s.setString(1,regno);
s.setString(2,uname);
File file = new File(pic);
FileInputStream fis = new FileInputStream(file);
s.setBinaryStream(3,fis,fis.available());

int temp=s.executeUpdate();
if(temp>0)
response.sendRedirect("index.html");
}
catch(Exception e)
{}
}}
</code>



<code>
<!-- jsp to view picture on browser -->
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%! ResultSet rs; %>
<%! Statement s;
String uname; %>
<%
try
{
uname="uday@vinjey.com";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://192.168.1.77:3306/test","root","sudha");
Statement s=con.createStatement();
rs=s.executeQuery("select uid,uname,i_picture from users");
rs.next();
rs.next();
}
catch(Exception e)
{}
%>
<html>
<head>
<title>Problem/Change Management System</title>
</head>
<body>
<form name="picadmin">
<table align="center">
<td rowspan=2 width="2%" height="3%"> <img src="http://localhost:8080/chandu/GetPicture?uname=<%= uname %>" width="100" height="70" name ="mypic"></td>
<td><h1>ADMINISTRATOR</h1></td>
</table>

</body>
</html>
</code>


<code>
//servlet to get picture from the remote database
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class GetPicture extends HttpServlet
{
static Connection conn;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
try
{
String URL="jdbc:mysql://192.168.1.77:3306/test";
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(URL,"root","sudha");
}
catch(Exception e)
{
System.out.println(e.toString());
}
String uname=request.getParameter("uname");
try
{
PreparedStatement pst=conn.prepareStatement("select i_picture from users where uname = ?");
pst.setString(1,uname);
ResultSet rs= pst.executeQuery();
if(rs.next())
{
ServletOutputStream out= response.getOutputStream(); response.setContentType("image/jpg");
out.write(rs.getBytes("i_picture"));
System.out.print("came");
System.out.print(uname);
}
else
{
System.out.println("Ms Access image is not inserted");
}


}
catch(SQLException e)
{
System.out.println(e.toString());
}
}
}

</code>
can anybody help me to solve the issue?
Its executing till FileInputStream in addvalue servlet. Its not compiling furthur.
Thank You,
regards,
Chandra Shekar B.S

Is This A Good Question/Topic? 0
  • +

Page 1 of 1