日期:2014-05-17  浏览次数:20741 次

为什么这段代码总是不行?求帮助!

服务器tomcat配置好了,想用jsp文件把数据库(mysql)当中的表输出,却总是出现这种错误:




<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>www.baidu.com 百度,众里寻他千百度</title>
</head>
<body>
<%!
public static final String DBDRIVER="org.git.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/whut";
public static final String DBUSER="root";
public static final String DBPASS="root";
%>
<%
Connection conn=null; //声明数据库连接对象
PrepareStatement pstmt=null; //声明数据库操作
ResultSet rs=null; //声明数据库结果集
%>
<%
try{
Class.forName(DBDRIVER); //数据库驱动程序加载
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS); //取得数据库连接
String sql="select empno,ename,job,sal,hiredate from emp";
pstmt=conn.PrepareStatement(sql);
rs=pstmt.executeQuery();

%>
<center>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员工资</td>
<td>雇用日期</td>
</tr>
<%
while(rs.next()){
int empno=getInt(1);
String ename=rs.getString(2);
String job=rs.getString(3);
float sal=rs.getFloat(4);
java.util.Date date=rs.getDate(5);
%>

<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td<%=sal%></td>
<td><%=date%></td>
</tr>
<%
}
%>
</table>
</center>
<%
}catch (Exception e)
{System.out.println(e);
}finally{

rs.close();
pstmt.close();
conn.close();
}
%>


</body>
</html>

------解决方案--------------------
拼写错误:PreparedStatement,不是PrepareStatement 
------解决方案--------------------
pstmt=conn.PrepareStatement(sql);
方法名不对,写成大写了

------解决方案--------------------

PrepareStatement pstmt=null; //声明数据库操作

改为:

PreparedStatement pstmt=null; //声明数据库操作


pstmt=conn.PrepareStatement(sql);

改为:
pstmt=conn.prepareStatement(sql);