dcsimg
www.webdeveloper.com
Results 1 to 10 of 10

Thread: [RESOLVED] upload image to mysql database jsp coding

  1. #1
    bhai Guest

    resolved [RESOLVED] upload image to mysql database jsp coding

    Hello everyone

    I am making my collage project in web development so in student database records there will be photo to upload,I want to upload image with data in mysql database and retrieve in another page .please give me javascript code if any.

    thank u in advance

  2. #2
    Join Date
    Jul 2012
    Posts
    57
    You are seriously a college student asking this question? Oh no...

    Well first, using javascript to access a database is similar to giving every stranger in the world the key to your safe with infinite money, and you then will be confused about how everyone stole all that infinite money you had.

    Second, you need to learn a server side scripting language like perl or python. These can access a database securley (if done correctly) and save the binary file (image) into the database.

  3. #3
    bhai Guest
    my project is in jsp and I have done all my pages,dfd and er diagram I have submitted.I have no knowledge about perl or python.
    So please if you can help me here is my code it saves the images to a given folder but it does not save to database help me waiting for your relpy

    <%@ page import="java.io.*" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.util.zip.*"%>
    <%
    String saveFile="";
    String contentType = request.getContentType();
    if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
    DataInputStream in = new DataInputStream(request.getInputStream());
    int formDataLength = request.getContentLength();
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    while(totalBytesRead < formDataLength){
    byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
    totalBytesRead += byteRead;
    }
    String file = new String(dataBytes);
    saveFile = file.substring(file.indexOf("filename=\"") + 10);
    saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
    saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
    int lastIndex = contentType.lastIndexOf("=");
    String boundary = contentType.substring(lastIndex + 1,contentType.length());
    int pos;
    pos = file.indexOf("filename=\"");
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    int boundaryLocation = file.indexOf(boundary, pos) - 4;
    int startPos = ((file.substring(0, pos)).getBytes()).length;
    int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
    File ff = new File("C:/Tomcat 7.0/webapps/am/notes/"+saveFile);
    FileOutputStream fileOut = new FileOutputStream(ff);
    fileOut.write(dataBytes, startPos, (endPos - startPos));
    fileOut.flush();
    fileOut.close();
    %><table border="0" width="100%" id="table1">
    <tr>
    <td background="untitled.bmp">&nbsp;<p>&nbsp;</p>
    <p>&nbsp;</td>
    </tr>
    </table>
    <center><table width=100% bgcolor="#FFFFFF" id="table2">
    <tr>
    <td width=15% align="center" bgcolor="#3366FF"><b><font color="white" size=4>
    <a href="index.html"><font color="#FFFFFF">Home</font></a></font></b><td width=5% align="center" bgcolor="#3366FF">
    <u><b><font color="white" size=4>
    |</font></b></u><td width=15% align="center" bgcolor="#3366FF"><u><b><font color="white" size=4>About Us</font></b></u><td width=5% align="center" bgcolor="#3366FF">
    <u><b><font color="white" size=4>
    |</font></b></u><td width=15% align="center" bgcolor="#3366FF">
    <font color="#FFFFFF" size="4">
    <span style="font-family: Times New Roman; font-weight: 700">
    <a href="informn.jsp"><font color="#FFFFFF">Latest
    announcements </font></a> </span></font><td width=5% align="center" bgcolor="#3366FF">
    <u><b><font color="white" size=4>|
    </font></b></u><td width=15% align="center" bgcolor="#3366FF"><b><font color="white" size=4>
    <a href="logincheck2.jsp"><font color="#FFFFFF">
    Student Zone</font></a></font></b><td width=5% align="center" bgcolor="#3366FF">
    <u><b><font color="white" size=4>|
    </font></b></u><td width=15% align="center" bgcolor="#3366FF">
    <a href="logincheck1.jsp">
    <b><font color="#FFFFFF" size=4>Admin Login</font></b></a><u><font color="#FFFFFF">
    </font>
    </u>
    </table>
    <p>&nbsp;</p>
    </center>
    <table border="0" width="100%" id="table3">
    <tr>
    <td>&nbsp;</td>
    <td width="630"><table border="2" id="table4"><tr><td><b>You have successfully upload the file:</b>
    <%out.println(saveFile);%></td></tr></table>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td width="630">&nbsp;</td>
    </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p><Br></p>
    <%
    Connection connection = null;
    String connectionURL = "jdbc:mysql://localhost:3306/university";
    ResultSet rs = null;
    PreparedStatement psmnt = null;
    FileInputStream fis;
    try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root", "root");
    File f = new File(saveFile);
    psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
    fis = new FileInputStream(f);
    psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
    int s = psmnt.executeUpdate();
    if(s>0){
    System.out.println("Uploaded successfully !");
    }
    else{
    System.out.println("Error!");
    }
    }
    catch(Exception e){e.printStackTrace();}
    }
    %>

  4. #4
    bhai Guest
    Thank you everybody I have made it myself got no help from any of the sites.
    got the answer by myself.

    thank you .

  5. #5
    Join Date
    Mar 2009
    Posts
    452
    bhai kis jga se ho ap ?

  6. #6
    bhai Guest
    Quote Originally Posted by ZABI View Post
    bhai kis jga se ho ap ?
    Assam se hu or ap kah se ?

  7. #7
    Join Date
    Mar 2009
    Posts
    452
    Sialkot, Punjab, Pakistan se

  8. #8
    bhai Guest
    Quote Originally Posted by ZABI View Post
    Sialkot, Punjab, Pakistan se
    Here I am getting difficulty in many subjects as Apl(assembly programming) projects.

  9. #9
    Join Date
    Mar 2009
    Posts
    452
    :P to be honnest i m too feeling it hard, anyways best of luck for both of us

  10. #10
    Join Date
    Mar 2014
    Posts
    1
    hey I'm doing a similar project and could you please tell whether this code is working..awaiting for your reply!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles