Click to See Complete Forum and Search --> : problem with the JDBC and servlet connection


theo1985
11-21-2010, 05:18 PM
hi!!!i have a problem with my database and i would like your help.
i have created a database in mysql with the name mybooking and the table booking. This table has 13 fields. I want to insert in this table the values which i give in my form...
The problem is that when i press the submit button then the program gives me error...Something goes wrong with my connection in servlet class.Could you please help me?


<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>
<body>
<form name="NameDetails" action="/sqlStatServlet/ServletUserEnquiryForm2" method="post" onsubmit="return onClick()"
onsubmit="return form1_onsubmit()">
<fieldset>
<legend>Address</legend>
<ol>
<li>
<label for="bookingID">BookingID*:</label>
<input id="bookingID" name="bookingID" class="text"
type="text" />
</li>
<li>
<label for="address">Address*:</label>
<input id="address" name="address" class="text"
type="text" />
</li>
<li>
<label for="suburb">Suburb/Town*:</label>
<input id="suburb" name="suburb" class="text"
type="text" />
</li>
<li>
<label for="county">County:</label>
<input id="county" name="county" class="text"
type="text" />
</li>
<li>
<label for="postcode">Postcode*:</label>
<input id="postcode" name="postcode"
class="text textSmall" type="text" />
<input type="button" name="Button" value="Check Code" onclick="postit()">
</li>
<li>
<label for="country">Country*:</label>
<input id="country" name="country" class="text"
type="text" />
</li>
</ol>
</fieldset>
<fieldset>
<legend>Debit Card Details</legend>
<ol>
<li>
<label for="name">Name*:</label>
<input id="name" name="name" class="text" type="text" />
<input type="button" name="Button" value="Check Code" onclick="namecheck()">
</li>
<li>
<label for="surname">Surname*:</label>
<input id="surname" name="surname" class="text" type="text" />
</li>
<li>
<label for="email">Email address*:</label>
<input id="email" name="email" class="text" type="text" />
<input type="button" name="Button" value="Check Code" onclick="ValidateEmail()">
</li>
<li>
<label for="phone">Telephone*:</label>
<input id="phone" name="phone" class="text" type="text" />
</li>
<li>
<label for="Card number">Card number*:</label>
<input id="Card Number" name="Card Number" class="text" type="text" />
</li>
<li>
<label for="Card Security Number">Card Security Number*:</label>
<input id="Card Security Number" name="Card Security Number" class="text"
type="text" />
</li>
<li>
<label for="Card Issue Number:">Card Issue Number:</label>
<input id="Card Issue Number:" name="Card Issue Number:" class="text"
type="text" />
</li>
<li>
<label for="Card Start Date">Card Start Date:</label>
<select id="credit date" name="credit date">
<%
for (int i=1;i<=31;i++) {
%>
<option value="<%=i%>"><%=i%></option>
<%
}
%>
<select id="Origin" name="Origin">
<option>Please Choose</option>
<option>2000</option>
</select>
</li>
<li>
<label for="Card Expiry Date">Card Expiry Date:</label>
<select id="expiry date" name="expiry date">
<%
for (int i=1;i<=31;i++) {
%>
<option value="<%=i%>"><%=i%></option>
<%
}
%>
<select id="Origin" name="Origin">
<option>Please Choose</option>
<option>2000</option>
</select>
</li>
<li>
</ol>
</fieldset>
<fieldset class="submit">
<input class="submit" type="submit"
value="Submit" />
</fieldset>
</body>
</html>






import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletUserEnquiryForm2 extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
/**Process the HTTP Get request*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String connectionURL = "jdbc:mysql://localhost/mybooking";
Connection connection=null;
ResultSet rs;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//get the variables entered in the form
String bookingID = req.getParameter("bookingID");
String address = req.getParameter("address");
String suburb = req.getParameter("suburb");
String county = req.getParameter("county");
String postcode = req.getParameter("postcode");
String country = req.getParameter("country");
String name = req.getParameter("name");
String surname = req.getParameter("surname");
String email = req.getParameter("email");
String telephone = req.getParameter("phone");
String cardnumber = req.getParameter("Card Number");
String cardsecnum = req.getParameter("Card Security Number");
String cardisnumber = req.getParameter("Card Issue Number");
try {
// Load the database driver
Class.forName("org.gjt.mm.mysql.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "admin");
//Add the data into the database
String sql = "insert into emp_details values (?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, bookingID);
pst.setString(2, address);
pst.setString(3, suburb);
pst.setString(4, county);
pst.setString(5, postcode);
pst.setString(6, country);
pst.setString(7, name);
pst.setString(8, surname);
pst.setString(9, email);
pst.setString(10, telephone);
pst.setString(11, cardnumber);
pst.setString(12, cardsecnum);
pst.setString(13, cardisnumber);
int numRowsChanged = pst.executeUpdate();
// show that the new account has been created
out.println(" Hello : ");
out.println(" '"+name+"'");
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
}
}

sohguanh
11-22-2010, 03:06 AM
String connectionURL = "jdbc:mysql://localhost/mybooking";


First and foremost, do you have the mysql JDBC driver in your classpath ? Something like ojdbc14.jar (think this is Oracle JDBC driver) but for mysql version instead.

theo1985
11-22-2010, 03:56 AM
i give you more information.
The error which i have when i push the submit button is

HTTP Status 404 -

type Status report

message

descriptionThe requested resource () is not available.
GlassFish Server Open Source Edition 3.0.1

Yes,maybe in this line is my error

String connectionURL = "jdbc:mysql://localhost/mybooking";

in mysql when i write this code then the table opens with no problems:


mysql>show databases;
mysql>usebooking;
mysql>showtables;
describe>booking;


Furthermore i have added in my web application the library mysql JDBC driver my-sql-connector-java-5.1.6.bin.jar

zimonyi
11-23-2010, 12:25 AM
Since your error is a HTTP error it does not provide us with much information.

Does your row get saved in the database?

What happens if you instead of having a Servlet create a normal application (with a main() method) and just hardcode some values and run your program?

We need an stacktrace or something to be able to help you more.

Archie

theo1985
11-23-2010, 07:40 AM
I don't understand something.
In my form i have this

<form name="NameDetails" action="/sqlStatServlet/ServletUserEnquiryForm2" method="post">


when i click on submit button why the next form is not the ServletUserEnquiryForm2

but i have this error?

HTTP Status 404 -

type Status report

message

descriptionThe requested resource () is not available.
GlassFish Server Open Source Edition 3.0.1
[/QUOTE]
Because i think

zimonyi
11-25-2010, 03:18 AM
Do you have you servlet defined in web.xml?

Archie