我编写了一个代码jsp来将员工详细信息存储在数据库中。我在该员工数据库中创建了一个名为employee和table empdetails的数据库。我在tomcat服务器中部署了代码。但是在关注字段中给出的数据不会存储在数据库表格中。
任何人都可以帮助我。
代码如下
<%@ 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>
答案 0 :(得分:3)
替换
} catch (Exception ex) {
out.println("Unable to connect to Database.");
}
通过
} catch (Exception ex) {
throw new ServletException("Unable to connect to Database.", ex);
}
大量信息(异常细节和堆栈跟踪)将出现在服务器日志中(如果幸运的话,也会出现在JSP响应中)。异常类型,消息和堆栈跟踪会告诉您有关问题原因的详细信息。如果您能够解释异常,您将能够理解问题的原因,因此解决方案只是显而易见的。如果您无法了解例外,只需传递类型/消息,并在必要时通过Google传递第1行。如果您仍然坚持,请在此处询问(不要忘记在您的问题中提供异常/跟踪!)。
无关,将Java代码(确保数据库交互代码)放在JSP文件中是一种不良做法。根据{{3}}
进行相应修改