Solved

conflict between upload file code and insert query in DB

Posted on 2007-11-21
6
1,089 Views
Last Modified: 2013-11-24
What i tried to do is to creae jsp page that enable the user to fill a form and upload file
THEN : the filled field will be insert to the sql database, and the upload file will be store in a folder(out side of DB) but the name of the uploaded file will be inserted to one of the DB column

What happen ?? the insertion code is (WROK GOOD ALONE) and the upload file code is (WROK GOOD ALONE)
WHEN I JOINED THE TWO CODES --> ONLY the upload file is happen and no insertion to DB happen ????

This is the code (addsoftware.jsp)  : where is the problem
<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@ page import="java.sql.*,java.io.*,java.util.*,org.apache.*,java.io.File,java.lang.*"%>
 
 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

   "http://www.w3.org/TR/html4/loose.dtd">
 

<html>

    <%@ page language="java" import="javazoom.upload.*,java.util.*" %>

<%@ page errorPage="ExceptionHandler.jsp" %>
 

<jsp:useBean id="upBean" scope="page" class="javazoom.upload.UploadBean" >

  <jsp:setProperty name="upBean" property="folderstore" value="c:/uploads" />

</jsp:useBean>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Page</title>

    </head>

    <body>
 

          <%

        // database quiere to insert fields in the database  

           try

          {

          

          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

          

           Connection conn=DriverManager.getConnection("jdbc:odbc:NN_project","sa","nnproject");

          String str=request.getParameter("uploadfile");

          // out.print(str);

          

          File f= new File(str);

          str= f.getName();

          String sname=request.getParameter("softname");

          //File f=new File(str);

          

         String nn_model=request.getParameter("nn_model");

         

          String descr=request.getParameter("descr");

          String nn_type=request.getParameter("nn_type_1");

          String new_net_type=request.getParameter("new_net_type");

          String input=request.getParameter("input");

          String output=request.getParameter("output");

          String hidden=request.getParameter("hidden");

          String Func=request.getParameter("func");

          

          

          PreparedStatement stmt = conn.prepareStatement(

                  "INSERT INTO soft_temp (software_name,software_file_name,descr,input_neuron,output_neuron,hidden_neuron,new_type,nn_model,nn_type,activiation_function) VALUES (?,?,?,?,?,?,?,?,?,?)");

          

         stmt.setString(1,sname);

         stmt.setString(2,str);

         // stmt.setString(3,nn_model);

         // stmt.setString(4,nn_type);

          stmt.setString(3,descr);

          stmt.setString(4,input);

          stmt.setString(5,output);

          stmt.setString(6,hidden);

          stmt.setString(7,new_net_type);

          stmt.setString(8,nn_model);

          stmt.setString(9,nn_type);

          stmt.setString(10,Func);

          

          

          

          stmt.executeUpdate();

          

          

          stmt.close();

          

          

          } catch (Exception ex) {

      ex.printStackTrace();

    }

           

           //// the code used to save the uploadfile  in a folder

         if (MultipartFormDataRequest.isMultipartFormData(request))

          {

          // Uses MultipartFormDataRequest to parse the HTTP request.

          MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);

          String todo = null;

          if (mrequest != null) todo = mrequest.getParameter("todo");

          if ( (todo != null) && (todo.equalsIgnoreCase("upload")) )

          {

          Hashtable files = mrequest.getFiles();

          if ( (files != null) && (!files.isEmpty()) )

          {

          UploadFile file = (UploadFile) files.get("uploadfile");

          if (file != null) out.println("<li>Form field : uploadfile"+"<BR> Uploaded file : "+file.getFileName()+" ("+file.getFileSize()+" bytes)"+"<BR> Content Type : "+file.getContentType());

          // Uses the bean now to store specified by jsp:setProperty at the top.

          upBean.store(mrequest, "uploadfile");

          }

          else

          {

          out.println("<li>No uploaded files");

          }

          }

          else out.println("<BR> todo="+todo);

          }

          

         

          

         

          out.println("You will receive the acceptance within three days");

            

          

         %>

         ///////////// The Form //////////////////

    <h1>Add Software</h1>

   

        <form method="post" action="addsoftware.jsp" name="upform" enctype="multipart/form-data">

            <h2> Software name :</h2> 

        <input type="text" name="softname" value="" />

        

        <h2> Describtion:</h2>

        <input type="text" name="descr" value="" />

        <h2>Net Model:</h2>

        <input type="radio" name="nn_model" value="FeedForward" />FeedForward

        <input type="radio" name="nn_model" value="Recurrent" />Recurrent

     <h2>Net Type:</h2>   

<select name="nn_type_1">  

  <% 
 

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection conn = DriverManager.getConnection(

"jdbc:odbc:NN_project","sa","nnproject");
 
 

int i=0;

String[] ar;

String ff="ff";

String ffdb=null;

Statement stmt = conn.createStatement();

String sql = "SELECT NN_type_name FROM NN_type  ";

ResultSet rs = stmt.executeQuery(sql);

 //ar=rs.getArray("NN_type_name");

 

 

while(rs.next())

{

    

    %>

    <option> 

    

    <%= rs.getString("NN_type_name")%>

  

    </option>

  <%           

   } 

    stmt.close();

    } catch (Exception ex) {

      ex.printStackTrace();}

%>     

        
 

            

        </select>

         

  

        <br>If the net type you wants does not exist please enter the new net type: <br>

        

       <h2> New net type :</h2>

        <input type="text" name="new_net_type" value="" />

            <h2>

                <br>Input Neuron: <input type="text" name="input" value="" />

            <br>Hidden Neuron: <input type="text" name="hidden" value="" /></h2> if there is more than layer, please seperate the number of neuron in each layer by ";" 

            <h2>

            <br>Output Neuron:  <input type="text" name="output" value="" />

        </h2>

        <br>

        <h2> Activation Function:  </h2> 

        <select name="func">

              <option>Identity Function</option>

              <option>Binary Function</option>

              <option>Sigmoid Function</option>

          </select>

          

          <h2> Upload software:</h2>

       <input type="file" name="uploadfile" size="50">

       <input type="hidden" name="todo" value="upload">

        

        <input type="submit" value="submit" name="submit" />

        <input type="reset" value="reset" name="reset" />

    </form>

    <%--

    This example uses JSTL, uncomment the taglib directive above.

    To test, display the page like this: index.jsp?sayHello=true&name=Murphy

    --%>

    <%--

    <c:if test="${param.sayHello}">

        <!-- Let's welcome the user ${param.name} -->

        Hello ${param.name}!

    </c:if>

    --%>

   

        
 

    </body>

</html>

Open in new window

0
Comment
Question by:master_programmer
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Expert Comment

by:AustinSeven
Comment Utility
What I would do to debug this...
See what the value of 'str' is (exactly).   Write some test code and copy the value of 'str' as a hardcoded value and try and insert it into the table along with some dummy values for the other fields if they are not NULL.   Do the test insert using T-SQL in order to test outside of java.    

AustinSeven
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
  Hi!

What I can see from your code is that both of the code-fragments you say working independently
should also work as joined however I don't see how you can make 100% sure that the
request.getParameter("softname") matches the filename which you store in the variable str in line 34.
It may be that the record is in the database while there is no matching link between softname and the actual
file name.

Regards,
  Tomas Helgi
0
 

Author Comment

by:master_programmer
Comment Utility
Actually there is no relation between filename (str) and softname field
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:master_programmer
Comment Utility
I did a very simple "insert query operation" that is independent from the upload operation and it is also not work with the upload code, I doubted from the header of the form :
<form method="post" action="addsoftware.jsp" name="upform" enctype="multipart/form-data">
when i delete the statment (enctype="multipart/form-data") the insertion work, but upload not??

I am also doubted from  the statment:
<jsp:useBean id="upBean" scope="page" class="javazoom.upload.UploadBean" >
  <jsp:setProperty name="upBean" property="folderstore" value="c:/uploads" />
</jsp:useBean>
specially from (scope="page") what it did mean ??
0
 
LVL 10

Expert Comment

by:AustinSeven
Comment Utility
The point is, if the insert into the db table is failing, extract the exact string value that is being returned in the code and then create a T-SQL INSERT statement to replicate the insert statement in your code sample.   Run the code in Query Analyser/Management Studio.    Does it fail?  

I must admit, I'm not 100% sure I understand the problem but I have to interpret it as 'the java code handles the file upload ok but when it tries to insert into a db table, it fails'.   Is that right?  If so, what I have suggested is a good place to start.

AustinSeven
0
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
Comment Utility
Have you checked out the log files of the server you are running the jsp page on ?
I am curious what the ex.printStackTrace() is showing.

You can also add the followin lines
out.println(ex.getMessage());
out.println(ex.printStackTrace());
in stead of just ex.printStackTrace() where you catch Exceptions.
That way any exeptions are printed directly to the webpage to debug.

Regards,
   Tomas Helgi
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sharepoint 3.0 migration 4 37
SQL Script to find duplicates 16 17
while loop in html mail format 5 31
Sql query 34 14
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now