如何纠正jsp中的数据库连接错误?
Posted
技术标签:
【中文标题】如何纠正jsp中的数据库连接错误?【英文标题】:How to rectify the database connectivity error in jsp? 【发布时间】:2011-09-13 11:48:27 【问题描述】:我编写了一个代码 jsp 来将员工详细信息存储在数据库中。我在该员工数据库中创建了一个名为员工和表 empdetails 的数据库。我在tomcat服务器中部署了代码。但在关注字段中给出的数据并未存储在数据库表 empdetails 中。
谁能帮帮我。
代码如下
<%@ 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 >
Name
</TH>
<TD >
<INPUT TYPE="text" NAME="name">
</TD>
</tr>
<TR>
<TH >
City
</TH>
<TD >
<INPUT TYPE="text" NAME="city">
</TD>
</tr>
<TR>
<TH >
Phone
</TH>
<TD >
<INPUT TYPE="text" NAME="phone">
</TD>
</tr>
<TR>
<TH >
Qualification
</TH>
<TD >
<INPUT TYPE="text" NAME="Qualification">
</TD>
</tr>
<TR>
<TH >
Year
</TH>
<TD >
<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 >
Experience
</TH>
<TD >
<INPUT size="4" TYPE="text" NAME="Experience">
</TD>
</tr>
<TR>
<TH >
Position
</TH>
<TD >
<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 >
<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>
【问题讨论】:
【参考方案1】:替换
catch (Exception ex)
out.println("Unable to connect to Database.");
通过
catch (Exception ex)
throw new ServletException("Unable to connect to Database.", ex);
大量信息(异常详细信息和堆栈跟踪)将出现在服务器日志中(如果幸运的话,还会出现在 JSP 响应中)。异常类型、消息和堆栈跟踪详细地告诉您问题的原因。如果您能够解释异常,您将能够了解问题的原因,因此解决方案是显而易见的。如果您无法理解异常,只需通过 Google 传递类型/消息和第一行(如有必要)。如果您仍然卡住,请在此处询问(不要忘记在您的问题中提供异常/跟踪!)。
与具体问题无关,将 Java 代码(当然是数据库交互代码)放在 JSP 文件中是一种不好的做法。根据How to avoid Java code in JSP files?进行相应修复
【讨论】:
以上是关于如何纠正jsp中的数据库连接错误?的主要内容,如果未能解决你的问题,请参考以下文章
SQLDBX连接MySQL数据库,数据表中的汉字显示成乱码,应该如何设置才能看到正常的汉字
急!我用Java连接了数据库并读取了一个表中的数据,如何在jsp页面中以表格的形式输出查询的数据??谢谢