• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlets - database

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I just want to know that if I had made one form using servlet and I want to store the values of form fields into a database, then what is the right procedure or method.
Actually firstly I have made the form using html tags in "out.println(" ");" command, but after that i was not able to submit the values into the database.
Then second time, first I made HTML package and import it into the servlet.But after I compiled my servlet and put the class file into Java web server's servlet directory,and when I want to load that servlet it shows me an error "Cannot upload servlet : null".
I am not able to catch the problem,pl. do help I am getting frustrated.Please can u explain me which method is correct and how to go about it.
I will be thankful to u if u just give me an example of making a servlet for submitting the values of form which consist of :
1)Label 'Name'; 2) Text field; 3)Submit button.
I will be very thanful to u. Pl. help me.
Swati
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi swati,

don't worry yaar..!!!
Design u'r form just with HTML tags only and save the page with .html extension.
In action property of the form tag, give the path to .class(servlet) file.
As soon as u submit the form, it will look for the .class i.e., servlet file.
In the servlet write the code for inserting the values in a database. I think u know how to insert values in a database by retrieving the values from the form fields(using getParameter method of HttpServletRequest)...
Try this and get back...
Good luck!!!
regds,
Sandeep.
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend you use a stringbuffer instead of println for garbage collection purposes.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
 
swati maken
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for ur reply,but I have still not found the solution for my problem.
I have followed ur advice, but as soon as the servlet is called it shows me the error 'class not found'.
Actually at the time of compilation it gave me no error but at the run time it displayed the error on the screen.
I am enclosing my code so that u can help me out.Please give ur advice .
---------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class sjvtechfin1 extends HttpServlet
{
String type[];
String typ,per,amount,formven,val;
int uid;
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doPost(request,response);
}

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
type = new String[5];
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html><head></head><body>");
Enumeration parameters = request.getParameterNames();
String param = null;
uid = 012;
out.println("Welcome user "+uid+"<br>");
while (parameters.hasMoreElements())
{
param=(String)parameters.nextElement();
type[0]=param;
type[1]=param;
type[2]=param;
type[3]=param;

try
{
out.println("Swati <br>");

Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/iba");

if (con!=null)
{
out.println("Swati");
Statement statement=con.createStatement();
PreparedStatement ps = con.prepareStatement("insert into sjvtechfin values(?,?,?,?,?)");
ps.setInt(1,uid);
ps.setString(2,type[0]);
ps.setString(3,type[1]);
ps.setString(4,type[3]);
ps.setString(5,type[2]);
ps.executeUpdate();
out.println("Swati");
}
}
catch (Exception ds)
{
out.println("error:"+ds);
}

out.println(param+":"+request.getParameter(param)+"<br>");
}

out.println("</body></html>");
out.close();

}
public String getServletInfo()
{
return "BasicServlet Information";
}
}
----------------------------
Waiting for ur reply.
Swati

Originally posted by Raukutam Sandeep:

Design u'r form just with HTML tags only and save the page with .html extension.
In action property of the form tag, give the path to .class(servlet) file.
As soon as u submit the form, it will look for the .class i.e., servlet file.
In the servlet write the code for inserting the values in a database. I think u know how to insert values in a database by retrieving the values from the form fields(using getParameter method of HttpServletRequest)...
Try this and get back...
Good luck!!!
regds,
Sandeep.


 
Raukutam Sandeep
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi swati,

the 'class not found' error will occur, if u haven't given the correct path in the action property of the form tag...
In u'r case the class name is 'sjvtechfin1'. After compiling the java file, u will get sjvtechfin1.class file.
Copy this file to servlets directory of JavaWebserver2.0.
( Can u tell me which server u are using?)
In the action property of form tag, set the path to this class file..like action="http://<servername>:<portnumber>/servlet/sjvtechfin1.class"
for example..
<form action="http://localhost:8080/servlet/sjvtechfin1.class">
Remember, give servlet in the path, not servlets...
I hope this solves u'r problem,
If u still have the probelm... reply me at rsandeep_1980@yahoo.com
I will see the code and if possible i will rectify that..
Try these steps and get back..
good luck!!
regds,
Sandeep.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi swati
In your code you do not have form tags
I feel you need to have form tag and a textbox and a submit button in the html form so that the user enters a value in the textbox and clicks on submit button the key/value pair present on the html are submitted to the servlet that is expecting these values
in the getParameter method if you pass the name of the testbox you have given in the HTML form then you will be able to access the value
Hope you got it
Regards
 
Those are the largest trousers in the world! Especially when next to this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic