[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

I have problem with data entry to database

Asked by LunarStar76 in Miscellaneous Web Development

I have jsp class and the servlet it's seem like it's working ok exceptton for the combo box in jsp i don't know how call it to enter database. I am using access database. help me please
 this is my jsp

<!-- Initialize the bean for use in page processing. -->



<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body onLoad="document.shiftForm.shiftName.onchange()">
<form name = "send" action ="InsertServlet " method ="post">
    <form name="shiftForm">
<table width="42%" border="0">
 <tr>
    <td width="19%">&nbsp;</td>
   <td width="30%">

<p>&nbsp; </p></td>
   <td width="8%">&nbsp;</td>
   <td width="43%">&nbsp;</td>
 </tr>
 <tr>
    <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>Days</td>
   <td>&nbsp;</td>
 </tr>
 <tr>
    <td>&nbsp;</td>
   <td>Shift name
    <p><select name="shiftName" onChange="redirect(this)">
    <option value="Morning">Morning</option>
    <option value "Afternoon">Afternoon</option>
    <option value ="Night">Night</option>
    </select>    
    <p>    StartTime
         <select name="shiftStart" onChange="this.form.shiftLength.onchange()">
          </select>
        <script>
<!--

var shiftTimes = [
["7:00AM","7:30AM","8:00AM","8:30AM","9:00AM","9:30AM","10:00AM","10:30AM","11:00AM","11:30AM","12:00AM"],
["12:30PM","1:00PM","1:30PM","2:00PM","2:30PM","3:00PM","3:30PM","4:00PM","4:30PM","5:00PM","5:30PM","6:00PM"],
["6:30PM","7:00PM","7:30PM","8:00PM","8:30PM","9:00PM","9:30PM","10:00PM","10:30PM","11:00PM","11:30PM","12:00PM"]];





function redirect(theSel){
  opt = theSel.form.shiftStart.options;
  opt.length=0;
  shiftOpt = shiftTimes[theSel.selectedIndex];
  for (i=0;i<shiftOpt.length;i++){
    opt[i] = new Option(shiftOpt[i], shiftOpt[i])
  }
  opt[0].selected=true;
  theSel.form.shiftStart.onchange();
}


function setShiftEnd(theField){
  var sTime = new Date("1/1/2000 "+theField.form.shiftStart.value)
  var aDur = theField.value.split(":");
  sTime.setHours(sTime.getHours()+aDur[0]*1);
  if(aDur[1]) sTime.setMinutes(sTime.getMinutes()+aDur[1]*1);
  HH = sTime.getHours();
  MM = sTime.getMinutes();
  if(MM<10) MM = "0"+MM;
  if(HH<13) {
    AP = " AM";
  } else {
    HH = HH - 12;
    AP = " PM";
  }
  theField.form.shiftEnd.value = HH+":"+MM+AP;
}

function go(){
  var temp=document.shiftForm.shiftStart;
  location=temp.options[temp.selectedIndex].value
}
//-->
     </script>
 

   <td>Mon
   <td>
     

       <input name="mondayCheck" type="checkbox" id="mondayCheck" value="Monday"  checked />
 <tr>
    <td>&nbsp;</td>
   <td><p>Shift Length</p>
     
       <input type="text" name="shiftLength" onChange="setShiftEnd(this)"> hours
   <p>&nbsp; </p></td>
   <td>Tues</td>
   <td>  

       <input name="tuesdayCheck" type="checkbox" id="tuesdayCheck" value="Tuesday" checked >
     </td>
 </tr>
 <tr>
    <td>&nbsp;</td>
   <td><p>EndTime</p>
   <p>
     <input type="text" name="shiftEnd" value="">
   </p></td>
   <td>Wed</td>
   <td>
       <input name="wednesdayCheck" type="checkbox" id="wednesdayCheck"  checked >
   </td>
 </tr>
 <tr>
    <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>Thurs</td>
   <td>  
       <input name="thursdayCheck" type="checkbox" id="thursdayCheck" value="Thursday" checked >
</td>
 </tr>
 <tr>
    <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>Frid</td>
   <td>
       <input name="fridayCheck" type="checkbox" id="fridayCheck" value="Friday" checked >
</td>
 </tr>
 <tr>
    <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>Sat</td>
   <td>
       <input name="saturdayCheck" type="checkbox" id="saturdayCheck" value="Saturday" checked >
   </td>
 </tr>
 <tr>
    <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>Sun</td>
   <td>
       <input name="sundayCheck" type="checkbox" id="sundayCheck" value="Sunday" checked >
   </td>
 </tr>
 <tr>
   <td>&nbsp;</td>
   <td>
       <input type="submit" name="Submit" value="Send">
     </td>
   <td>&nbsp;</td>
   <td> <input type="submit" name="Submit2" value="Back"></td>
 </tr>
</table>
</form>
</form>
<p>&nbsp; </p>
</body>
</html>

this is my servlet

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

public class InsertServlet extends HttpServlet
{


      static final int MS_ACCESS_DUP_KEY = 0;
    private Statement statement = null;
    private Connection connection = null;
    private String URL = "jdbc:odbc:roster";  // The ODBC DSN is hard-coded.

    private boolean jdbcDriverError = false, sqlError = false;

    private PrintWriter output;

    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection(URL, "", "");
        }
        catch(ClassNotFoundException cfne)
        {
            System.err.println("Failed to load JDBC/ODBC driver.");
            jdbcDriverError = true;
        }
        catch(SQLException sqle)
        {
            System.err.println("Unable to connect! - " +
                                "Check the connection string");

            sqlError = true;
        }
        return;
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
                                        throws ServletException, IOException
    {
        String shiftName,shiftStart,shiftEnd,mon,tue,wed,thurs,frid,sat,sun;

        output = res.getWriter();

        if(sqlError)
            output.println("<h2>Fatal error using DSN to connect to database. " +
                            "Please advise your leader.</h2>");
        else
        {
            if(jdbcDriverError)
                output.println("<h2>Fatal error loading Java database driver. " +
                                "Please advise your leader.</h2>");
            else
            {
                shiftName = req.getParameter("shiftName");
                shiftStart = req.getParameter("shiftStart");
                shiftEnd = req.getParameter("shiftEnd");
                mon = req.getParameter("mondayCheck");
                tue = req.getParameter("tuesdayCheck");
                wed = req.getParameter("wednesdayCheck");
                thurs = req.getParameter("thursdayCheck");
                frid = req.getParameter("fridayCheck");
                sat = req.getParameter("saturdayCheck");
                sun = req.getParameter("sundayCheck");


                res.setContentType("text/html");


                /* Note: The GuestBook database also contains fields Street,
                 * Suburb, State and Post Code that are not used in this example.
                 * However, the insert into the database must still account for
                 * these fields.
                 */

                boolean success = insertIntoDB("'" +
                                    shiftName + "','" +
                                    shiftStart + "','" +

                                   // Next one includes the 4 unused (by this servlet) fields.
                                    shiftEnd + "','" +

                                   (mon != null ? "yes" : "no") + "','" +
                                   (tue != null ? "yes" : "no") + "','" +
                                   (wed != null ? "yes" : "no") + "','" +
                                   (thurs != null ? "yes" : "no") + "','" +
                                   (frid != null ? "yes" : "no") + "','" +
                                   (sat != null ? "yes" : "no") + "','" +
                                   (sun != null ? "yes" : "no") + "'");
                if(success)
                    output.print("<h2><font face = \"Arial\">Thank you, " + shiftName +
                                    ", for registering.</font></h2>");
            }
        }
        output.close();

        return;
    }

    private boolean insertIntoDB(String stringToInsert)
    {
        try
        {
            statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO EmpAvai values (" +
                                stringToInsert + ");");
            statement.close();
        }
        catch(SQLException sqle)
        {
            int dbErrorCode = sqle.getErrorCode();

            if(dbErrorCode == MS_ACCESS_DUP_KEY)
                output.print("<h2>ERROR: Your email address already exists" +
                                " - please choose another!</h2>");
            else
                output.print("<h2>ERROR: Problems with adding new entry.</h2>");

            return false;
        }
        return true;
    }

    public void destroy()
    {
        try
        {
            if(connection != null)
                connection.close();
        }
        catch(SQLException sqle)
        {
            // Messages to System.out and System.err appear on the server's
            // console window.
            System.err.println("Problem closing the database");
        }
    }
}

 
[+][-]05/21/04 10:50 AM, ID: 11128790Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/22/04 01:11 PM, ID: 11134803Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Miscellaneous Web Development
Sign Up Now!
Solution Provided By: vikram_1982
Participating Experts: 1
Solution Grade: A
 
[+][-]06/01/04 04:31 AM, ID: 11201729Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/16/04 09:13 PM, ID: 12081319Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]09/21/04 07:41 PM, ID: 12119107Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81