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

jsp连接数据库插入数据,myeclipse报错,求解
报错结果:
2012-7-1 16:09:18 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 34 in the jsp file: /regist.jsp
prst cannot be resolved
31: }finally{
32: try{
33:  
34: prst.close();
35: con.close();
36: }catch(Exception e){}
37: }


An error occurred at line: 35 in the jsp file: /regist.jsp
con cannot be resolved
32: try{
33:  
34: prst.close();
35: con.close();
36: }catch(Exception e){}
37: }
38: %>




jsp代码为:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
  <title>test for register</title>
  </head>
  <body>  
  <%
  try{
   
  Connection con=null;
  PreparedStatement prst=null;
  //Statement st=null;
  //ResultSet rs=null;
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=a_Hss","sa","7116105");
  prst=con.prepareStatement("insert into T1_user(UserID,Password,UserName) value(?,?,?)");
  prst.setString(1,request.getParameter("userid"));
  prst.setString(2,request.getParameter("userpwd"));
  prst.setString(3,request.getParameter("username"));
  prst.executeUpdate();
  }catch(Exception e){
  System.out.println(e); 
  }finally{
  try{
   
  prst.close();
  con.close();
  }catch(Exception e){}
  }
  %>
  <h2>12345</h2>
  </body>
</html>


------解决方案--------------------
楼猪,我怀疑你是不是没有转换类型,你request.getParameter("userid")获取的是字符串,,话说你数据库是什么类型的?是varchar或者nvarchar吗?