Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes servlets - database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets - database" Watch "servlets - database" New topic
Author

servlets - database

swati maken
Greenhorn

Joined: Jan 12, 2001
Posts: 14
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
Raukutam Sandeep
Ranch Hand

Joined: Nov 15, 2000
Posts: 61
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.
Brett Knapik
Ranch Hand

Joined: Oct 15, 2000
Posts: 255
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


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
swati maken
Greenhorn

Joined: Jan 12, 2001
Posts: 14
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

Joined: Nov 15, 2000
Posts: 61
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.
narayan kulkarni
Greenhorn

Joined: Jan 31, 2001
Posts: 17
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


Take up computer programming as fun and you will just enjoy it. Always keep in mind that in some way your program is going to make human life easier.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: servlets - database