• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in Inserting data into database

 
Gerald Leonard
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi All, I have tried to insert data into my database with no success;




Please any help am new in JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you need now a newcomer to JSP is that putting Java code into a JSP is a bad practice that has been obsolete and discredited for 14 years. 14 years! You need to find a JSP reference or tutorial that teaches you modern JSP that uses the JSTL and EL in place of Java scriptlets.

I also recommend that newcomers to JSP read:
  • The Secret Life of JSPs
  • The Front Man

  •  
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65530
    108
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Gerald Leonard wrote:hi All, I have tried to insert data into my database with no success;

    Secondly, this doesn't help us to help you. What does "no success" mean. Please read ItDoesntWorkIsUseless.

    Once you have moved the Java code into a servlet, please post back with details of your issue if it still occurs.
     
    Greg Charles
    Sheriff
    Posts: 3010
    12
    Firefox Browser IntelliJ IDE Java Mac Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Gerald,

    First, please use code tags even on the HTML. (The XML formatter works well for that.) I've added the code tags for you above. Also, it looks like you've put a second table opening tag on line 39, instead of the table closing tag </table>.

    OK, the big thing is you shouldn't be embedding Java code into your JSP. It's technically possible, but the reasons for that are historical. In other words, we don't do that any more. If you bring out that code and put it in a Java class, probably a Servlet, then your question becomes a JDBC issue, not a JSP one. That's better, because there are many reasons a JDBC insert can fail. Maybe the connection is failing, maybe the parameters are wrong somehow. Each one of those have several possible causes. It would be easier to diagnose if you weren't trying to force it to execute in a JSP.

    Here's what I'd do. Get the code to run inside a standalone Java app, i.e., make it part of static final void main(String [] args) method. If you can get it working that way, then you can migrate it into a Servlet, and integrate with your web app as appropriate. At the same time, you might migrate from the DriverManager method of getting a database connection, to the DataSource method, which is the newer best practice.
     
    Gerald Leonard
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks,I tried to use Servlet but I cant send a data in my second <tr>.
    I just can send a single<tr>:how can I send the second <tr> without
    duplication of the second <tr> codes in my servlet because am havind 10 rows of products. here are my codes:


    Insert.java( servlet)


    please any help; to fix this...!!
     
    Greg Charles
    Sheriff
    Posts: 3010
    12
    Firefox Browser IntelliJ IDE Java Mac Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Gerald,

    Again, it's easier for us to help you fix things if you tell us what's wrong. I suggested putting the insert code into a standalone Java program before trying to connect it with a web app, and I still suggest that. It's easier to take things one step at a time.

    There are a few things I notice right away though. You don't have a submit button, so there's no way to submit your form. Even if you add that button, I'm not sure the Insert action is going to map correctly to your Servlet. That's going to depend on mappings in your web.xml file if you have one, or on defaults of your Servlet container whatever that is ... Tomcat, JBoss, WebLogic.

    In your servlet code, there's no reason for response.getWriter() if you don't intend to use it. You forward the request no matter what happens. Also, you don't want one request forward in the catch block and another in the finally block. In fact it's almost never a good idea to put a forward in the finally block, just as it's not a good idea to put a return statement there for methods that return values.
     
    Dave Tolls
    Ranch Hand
    Posts: 2284
    25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And one more important thing re: your servlet:


    Don't put state in your servlet. That lot should be variables local to the method.
    A Servlet runs on multiple threads...so more than one request hitting your server for this servlet can run at the same time, so they would be sharing all this information, and overwriting each others data. The result of that will be hard to debug errors.

    Oh, and stick some debugging logging in there so you can see exactly where your code is getting to. Simple System.out.println should end up in your server logs.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!