11 Replies - 6253 Views - Last Post: 04 October 2012 - 06:59 AM

#1 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Uplaod image to MySQL using JSP

Posted 28 September 2012 - 06:40 PM

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TryInsert extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		PrintWriter pw = response.getWriter();
		String connectionURL = "jdbc:mysql://localhost:3306/dbfinals";
		Connection con=null;
		try
		{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection(connectionURL, "root", "");
			
			if(!con.isClosed())
			{
				pw.println("Successfully connected to " + "MySQL server using TCP/IP...");
				con.close();
			}

			PreparedStatement ps = con.prepareStatement("INSERT INTO tblPic VALUES(?,?)");
			File file = new File("C:/Users/Blenda/Pictures/1.jpg");
			FileInputStream fs = new FileInputStream(file);
			ps.setInt(1,1);
			ps.setBinaryStream(2,fs,fs.available());
			int i = ps.executeUpdate();
			if(i!=0)
			{
				pw.println("image inserted successfully");
			}
			else
			{
				pw.println("problem in image insertion");
			}
		} 
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}



This is my code. But only a blank white page appears everytime I run TryInsert.java. How to fix this? Thank you.

The image is not inserted to the database. ;(

Is This A Good Question/Topic? 0
  • +

Replies To: Uplaod image to MySQL using JSP

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Uplaod image to MySQL using JSP

Posted 28 September 2012 - 07:11 PM

Why do you close the connection to the database on line 21, before you use it to insert the data?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Uplaod image to MySQL using JSP

Posted 28 September 2012 - 07:14 PM

And don't store your images in the database directly, as it clogs them up. Store them on the server, and store their paths as Strings in the database.
Was This Post Helpful? 0
  • +
  • -

#4 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Uplaod image to MySQL using JSP

Posted 28 September 2012 - 07:23 PM

How to do that?

I already removed con.close.

I made some modifications.

Statement st = con.createStatement();
			    File imgfile = new File("C:/1.jpg");
			    FileInputStream fin = new FileInputStream(imgfile);
			    PreparedStatement pre = con.prepareStatement("insert into tblpic (pic) values(?)");
			    pre.setBinaryStream(2,fin,(int)imgfile.length());
			    pre.executeUpdate();
			    pw.println("Inserting Successfully!");
			    pre.close();
			    con.close();



But still doesn't work.
Was This Post Helpful? 0
  • +
  • -

#5 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Uplaod image to MySQL using JSP

Posted 02 October 2012 - 06:15 PM


queryString = "select * from tbluser where uname=? and upass=? and eadd=?";
				ps = conn.prepareStatement(queryString);
				ps.setString(1, uname);
				ps.setString(2, pass);
				ps.setString(3, eadd);
				rs = ps.executeQuery();
				while (rs.next())
				{
						int UID=rs.getInt(1);
						out.println(UID);
				}
			
				queryString = "INSERT INTO tbluserperinfo VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			    ps = conn.prepareStatement(queryString);
				ps.setInt(1, UID);
				ps.setString(2, fname);
				ps.setString(3, mname);
				ps.setString(4, lname);




This is my code. I wonder why I always get this error:
Unable to compile class for JSP: An error occurred at line: 80 in the jsp file: /SaveRegForm.jsp UID cannot be resolved to a variable 77: 78: queryString = "INSERT INTO tbluserperinfo VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 79: ps = conn.prepareStatement(queryString); 80: ps.setInt(1, UID); 81: ps.setString(2, fname); 82: ps.setString(3, mname); 83: ps.setString(4, lname); Stacktrace:

description The server encountered an internal error (Unable to compile class for JSP: An error occurred at line: 80 in the jsp file: /SaveRegForm.jsp UID cannot be resolved to a variable 77: 78: queryString = "INSERT INTO tbluserperinfo VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 79: ps = conn.prepareStatement(queryString); 80: ps.setInt(1, UID); 81: ps.setString(2, fname); 82: ps.setString(3, mname); 83: ps.setString(4, lname); Stacktrace:) that prevented it from fulfilling this request.


Please help me.... Thank you!

This post has been edited by blackcompe: 02 October 2012 - 06:29 PM
Reason for edit:: [code] tags

Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: Uplaod image to MySQL using JSP

Posted 02 October 2012 - 06:31 PM

 ps.setInt(1, UID);



UID is undefined here, since you've defined it inside the while loop.

This post has been edited by blackcompe: 02 October 2012 - 06:32 PM

Was This Post Helpful? 1
  • +
  • -

#7 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Uplaod image to MySQL using JSP

Posted 02 October 2012 - 06:49 PM

But I stored it in a uid variable. Isn't that possible? How can I solve this? Please help me...

I've modified the code to this:

queryString = "select * from tbluser where uname=? and upass=? and eadd=?";
				ps = conn.prepareStatement(queryString);
				ps.setString(1, uname);
				ps.setString(2, pass);
				ps.setString(3, eadd);
				rs = ps.executeQuery();
				if (rs.next())
				{
						int UID=rs.getInt(1);
						out.println(UID);
				}
				

				queryString = "INSERT INTO tbluserperinfo (fname,mname,lname,gender,mstat,age,bday,pobirth,religion,citizenship,cadd,padd,country,conno,talskills) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			    ps = conn.prepareStatement(queryString);
				ps.setInt(1, UID);
				ps.setString(2, fname);
				ps.setString(3, mname);
				ps.setString(4, lname);




However, the error is still the same. :(
Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: Uplaod image to MySQL using JSP

Posted 02 October 2012 - 06:54 PM

Quote

/SaveRegForm.jsp UID cannot be resolved to a variable


This means you UID hasn't been declared. This of course is based on the code you've posted.

Please review your code. You're overlooking a simple mistake. If I have an if statement:

if(true) {
 int a = 1;
}


I cannot reference variables declared inside the if from outside of the if.

if(true) {
 int a = 1;
}
System.out.println(a);



I can't help you past this. Good luck.

This post has been edited by blackcompe: 02 October 2012 - 06:55 PM

Was This Post Helpful? 1
  • +
  • -

#9 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Uplaod image to MySQL using JSP

Posted 02 October 2012 - 07:06 PM

I already got it. Thank you very much!!! :D
Was This Post Helpful? 0
  • +
  • -

#10 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Uplaod image to MySQL using JSP

Posted 03 October 2012 - 05:37 PM

This is my code.

<%@page import="java.sql.*,java.io.*"%>
<%
try{
    InputStream sImage;
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbfinals","root","");
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery("SELECT pic FROM tblpic");
      if(rs.last()) {
      byte[] bytearray = new byte[1048576];
      int size=0;
      sImage = rs.getBinaryStream(1);
      response.reset();
      response.setContentType("image/jpeg");
      while((size=sImage.read(bytearray))!= -1 ){
      response.getOutputStream().write(bytearray,0,size);
   }
}
 con.close();
}     
catch(Exception ex){
out.println("error :"+ex);
}
%>



However, it gives this error:

HTTP Status 500 - java.lang.IllegalStateException: getOutputStream() has already been called for this response

type Exception report

message java.lang.IllegalStateException: getOutputStream() has already been called for this response

description The server encountered an internal error (java.lang.IllegalStateException: getOutputStream() has already been called for this response) that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:585)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.IllegalStateException: getOutputStream() has already been called for this response
	org.apache.catalina.connector.Response.getWriter(Response.java:636)
	org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
	org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
	org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
	org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
	org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
	org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
	org.apache.jsp.view_jsp._jspService(view_jsp.java:98)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)



Please help me in solving this... :(
Was This Post Helpful? 0
  • +
  • -

#11 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Uplaod image to MySQL using JSP

Posted 04 October 2012 - 04:49 AM

upload_attach.jsp

<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%@ page import ="javax.naming.InitialContext" %>
<%@ page import ="java.sql.PreparedStatement.*" %>
<form name="upload1" method=get>
<%!
	String url = "jdbc:mysql://localhost:3306/";
	String dbName = "dbFinals";
	String driver = "com.mysql.jdbc.Driver";
	String userName = "root";
	String password = "";
%>

<%	
try
{
	String up1=request.getParameter("num");
	String up2=request.getParameter("attach");
	java.io.File f=new java.io.File(up2);
	java.io.FileInputStream fis= new java.io.FileInputStream(f);
	out.println(up2);
	
	Class.forName(driver).newInstance();
	Connection con = DriverManager.getConnection(url+dbName,userName,password);
	PreparedStatement pst=con.prepareStatement("insert into tblpic (blah,pic) values(?,?)");
	pst.setString(1,up1);
	pst.setBinaryStream(2,fis,(int)f.length());
	pst.executeUpdate();
	pst.close();
	con.close();
%>
<center><h3>File Uploaded Successfull</h3></center>
<%
}
catch(Exception e)
{
e.printStackTrace();
%>
<center><h3>File Does not Uploaded</h3></center>

<%
}
%>
</form>
</html> 





attach.jsp

<html>
<head>
<title>Attach</title>
</head>
<body>
<br />
<br />
<br>
<center>
<form name="attach" action="./upload_attach.jsp" method="post" ENCTYPE="multipart/form-data">
<p><b>Upload No:</b><input type="text" name=num size=30> </p>
<p><input type="file" name=attach size=30> </p>
<br />
<br>
<input type="submit" name="upl" value="UPLOAD">
</form>
</center>
</body>
</html> 




Why is it not saving in database? Please help me. Thank you!
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: Uplaod image to MySQL using JSP

Posted 04 October 2012 - 06:59 AM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1