开发者

How to rectify the database connectivity error in jsp?

i wrote a code jsp to store the employee details in the database . i created a database called employee and table empdetails in that employee database. i deployed the code in the tomcat server. but the data that am giving in the concern fields are not getting stored in the database table empdetails.

can anyone help me out.

the code is as follows

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd" >
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<HTML>
    <HEAD>
        <TITLE>welcome to City Solutions </TITLE>
    </HEAD>
    <BODY bgcolor="#ffffcc">
        <font size="+3" color="green"><br>Welcome to City Solutions !</font>
        <br>
        Employee Registration..
        <FORM action="prepared_statement_query.jsp" method="get">
            <TABLE style="background-color: #ECE5B6;" WIDTH="30%">
                <TR>
                    <TH width="50%">
                        Name
                    </TH>
                    <TD width="50%">
                        <INPUT TYPE="text" NAME="name">
                    </TD>
                </tr>
                <TR>
                    <TH width="50%">
                        City
                    </TH>
                    <TD width="50%">
                        <INPUT TYPE="text" NAME="city">
                    </TD>
                </tr>
                <TR>
                    <TH width="50%">
                        Phone
                    </TH>
                    <TD width="50%">
                        <INPUT TYPE="text" NAME="phone">
                    </TD>
                </tr>
                <TR>
                    <TH width="50%">
                        Qualification
                    </TH>
                    <TD width="50%">
                        <INPUT TYPE="text" NAME="Qualification">
                    </TD>
                </tr>
                <TR>
                    <TH width="50%">
                        Year
                    </TH>
                    <TD width="50%">
                        <select name="year">
                            <option value="select">
                                select
                            </option>
                            <option value="2011">
                                2011
                            </option>
                            <option value="2010">
                                2010
                            </option>
                            <option value="2009">
                                2009
                            </option>
                            <option value="2008">
                                2008
                            </option>
                            <option value="2007">
                                2007
                            </option>
                            <option value="2006">
                                2006
                            </option>
                        </select>
                    </TD>
                </tr>
                <TR>
                    <TH width="50%">
                        Experience
                    </TH>
                    <TD width="50%">
                        <INPUT size="4" TYPE="text" NAME="Experience">
                    </TD>
                </tr>
                <TR>
                    <TH width="50%">
                        Position
                    </TH>
                    <TD width="50%">
                        <select name="Position">
                            <option value="select">
                                select
                            </option>
                            <option value="Java">
                                JAVA
                            </option>
                            <option value="Testing">
                                TESTING
                            </option>
                            <option value="ETL">
                                ETL
                            </option>
                            <option value="BA">
                                BA
                            </option>
                        </select>
                    </TD>
                </tr>


                <TR>
                    <TH></TH>
                    <TD width="50%">
                        <INPUT TYPE="submit" VALUE="submit">
                    </TD>
                </tr>

            </TABLE>
            <%
                String name = request.getParameter("name");
                String city = request.getParameter("city");
                String phone = request.getParameter("phone");
                String qualification = request.getParameter("qualification");
                String year = request.getParameter("year");

                System.out.println("year" + year);
                String experience = request.getParameter("experience");
                String position = request.getParameter("position");
                 /* Create string of connection url within specified 
   format with machine name, 
    port number and database name. Here machine name id 
    localhost and database name is student. */
    String connectionURL = "jdbc:mysql://localhost:3306/employee";
          // declare a connection by using Connection interface 
    Connection connection = null;
        // declare object of Statement interface that uses for 
   // executing sql statements.
     PreparedStatement pstatement = null;
         // Load JBBC driver "com.mysql.jdbc.Driver"
     Class.forName("com.mysql.jdbc.Driver").newInstance();
          int updateQuery = 0;

         // check if the text box is empty
         //if(name!=null && city!=null && phone!=null){

                    // check if the text box having only blank spaces
                    if (name != "" && city != "" && phone != ""
                            && qualification != "" && year != ""
                            && experience != "" && position != "") {
                        try {
                            /* Create a connection by using getConnection()
                            method that takes parameters of string type 
                            connection url, user name and password to connect 
                              to database. */
                            connection = DriverManager.getConnection(connectionURL,
                                    "root", "root");
                            // sql query to insert values in the specified table.
                            String queryString = "INSERT INTO empdetails(Name,City,Phone,Qualification,Year,Experience,Position) VALUES (?, ?, ?, ?, ?, ?, ?)";
                            /* createStatement() is used for create statement
                            object that is used for 
                            sending sql statements to the specified database. */
                            pstatement = connection.prepareStatement(queryString);
                            pstatement.setString(1, name);
                            pstatement.setString(2, city);
                            pstatement.setString(3, phone);
                            pstatement.setString(4, qualification);
                            pstatement.setString(5, year);
                            pstatement.setString(6, experience);
                            pstatement.setString(7, position);
                            updateQuery = pstatement.executeUpdate();
                            if (updateQuery != 0) {
            %>
            <br>
            <TABLE style="background-color: #E3E4FA;" WIDTH="50%" border="1">
                <tr>
                    <th>
                        Your Information Has to be Stored
                    </th>
                </tr>
            </table>
            <%
                }
                        } catch (Exception ex) {
                            out.println("Unable to connect to开发者_如何转开发 Database.");

                        } finally {
                            // close all the connections.
                            //pstatement.close();
                            //connection.close();
                        }
                    }

            %>
        </FORM>
    </body>
</html> 


Replace

} catch (Exception ex) {
    out.println("Unable to connect to Database.");
}

by

} catch (Exception ex) {
    throw new ServletException("Unable to connect to Database.", ex);
}

And a wealth of information (the exception details and stacktrace) will appear in server logs (and if you're lucky, also in the JSP response). The exception type, message and stacktrace tells you in depth detail about the cause of the problem. If you're able to interpret the exception, you'll be able to understand the cause of the problem and therefore the solution is nothing more than obvious. If you're unable to understand the exception, just pass the type/message and if necessary the 1st line through Google. If you still stucks, just ask it here (don't forget to provide the exception/trace in your question!).


Unrelated to the concrete problem, putting Java code (for sure database interaction code) inside a JSP file is a bad practice. Fix it accordingly based on How to avoid Java code in JSP files?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜