当前位置: 代码迷 >> Java Web开发 >> jsp连数据库遇到的有关问题,请高手帮忙
  详细解决方案

jsp连数据库遇到的有关问题,请高手帮忙

热度:232   发布时间:2016-04-17 12:27:02.0
jsp连数据库遇到的问题,请高手帮忙
数据库连接类
package DBConn;
import java.sql.*;
public class DataBaseConn{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
String user="sa";
String psw="";

String drivername="com.microsof.jdbc.sqlserver.SQLServerDriver";

ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
public DataBaseConn(){}

public void openconn()throws Exception{
try{
  Class.forName(drivername);
conn=DriverManager.getConnection(url,user,psw); 
}
catch(Exception e){
System.out.println(e.getMessage());
}

  }

  public ResultSet executeQuery(String sql){
  try{
  stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  rs=stmt.executeQuery(sql);
  }
  catch(SQLException e){
  System.err.println("executeQuery:"+e.getMessage());
  }
  return rs;
  }
   
   
  public void executeUpdate(String sql){
  try{
  stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  stmt.executeUpdate(sql);
  }
  catch(Exception e){
  System.err.println("executeUpdate:"+e.getMessage());
  }
  }
   
  public void closeconn(){
  try{
  conn.close();
  }
  catch(Exception e){
  System.out.println(e);
  }
  }
   
  public void closestmt(){
  try{
  stmt.close();
  }
  catch(Exception e){
  System.out.println(e);
  }
  }
}



jsp



<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="db" scope="session" class="DBConn.DataBaseConn">
</jsp:useBean>
<html>
<body>
<table>
  <tr> 
  <%!String sql="select sid from stu"; 
   
  String s="";
  %>
  <% db.openconn();
  ResultSet rs=db.executeQuery(sql);
  while(rs.next()){
  s=rs.getString(1);
  }  
  %>
  <td><%=s %></td>
  </tr>
  </table>
  </body>
  </html>



运行时出现的问题

HTTP Status 500 - 

--------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: An exception occurred processing JSP page /test.jsp at line 14

11: String s="";
12: %>
13: <% db.openconn();
14: ResultSet rs=db.executeQuery(sql);
15: while(rs.next()){
16: s=rs.getString(1);
17: }  


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
  相关解决方案