当前位置: 代码迷 >> Java Web开发 >> 关于JSP+SQL SERVER 2000的编程问题
  详细解决方案

关于JSP+SQL SERVER 2000的编程问题

热度:344   发布时间:2006-04-11 10:10:00.0
关于JSP+SQL SERVER 2000的编程问题

大家帮我看看,这个JAVABEAN和check两个代码,是关于连接数据库和登陆验证的,老是无法正确运行。有人看了说是check中的if(rs.next())有问题,哪位高人能帮我解答一下。

JAVABEAN:

package com.bwm.db;
import java.sql.*;
public class Condb{
String servername="localhost";
String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://servername:1433;DatabaseName=db_Scenince;user=sa;password=123";
Connection con=null;
ResultSet rs=null;
public Condb(){
try{
Class.forName(drivername);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
con=DriverManager.getConnection(url);
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql){
int result=0;
try{
con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return result;
}
public void close(){
try{
if(rs!=null){
rs.close();
}
}catch(Exception e){
System.err.println(e.getMessage());
}
try{
if(con!=null){
con.close();
}
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}

搜索更多相关主题的帖子: SQL  SERVER  JSP  数据库  

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

check代码:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<% request.setCharacterEncoding("gb2312"); %>
<%@ page import="com.bwm.page.Show"%>
<%@ page import="com.bwm.db.Condb"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<%
Condb con=new Condb();
//Connection conn=con.getConnection();
Show show=new Show();
String name=request.getParameter("name");
String password=request.getParameter("password");
String sql="select * from tb_Person where Username='"+name+"' and Password='"+password+"'";
ResultSet rs=con.executeQuery(sql);
if(rs.next()){
String strsql=rs.getString(1);
session.setAttribute("name",name);
session.setAttribute("password",password);
session.setAttribute("groupid",strsql);
response.sendRedirect("index.htm");
}else{
out.print(show.errorBox("你输入的用户名或密码有误","错误信息"));
out.close();
}
//if(conn!=null){
// conn.close();
//}
con.close();
%>

</body>
</html>


----------------解决方案--------------------------------------------------------
说是空指针错误,

type Exception report

message

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

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.NullPointerException
org.apache.jsp.user.check_jsp._jspService(check_jsp.java:67)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs


----------------解决方案--------------------------------------------------------
session.setAttribute("name",name);
session.setAttribute("password",password);
这句中的引号里的name,password应该大写吧

----------------解决方案--------------------------------------------------------
回复
改一下这两个变量试试,(如果你数据字段设置正确的话)

String name=request.getParameter("name")+"";
String password=request.getParameter("password")+"";


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

你首先给你的name password 赋一个初值试试.


----------------解决方案--------------------------------------------------------
check的倒数第13行“response.sendRedirect("index.htm");”
是不是改成 “response.sendRedirect("index");”
!!!!!!!

----------------解决方案--------------------------------------------------------
String url="jdbc:microsoft:sqlserver://servername:1433;DatabaseName=db_Scenince;user=sa;password=123";
该成
String url="jdbc:microsoft:sqlserver://servername:1433;DatabaseName=db_Scenince";
String username="sa";
String password="123";
试试看~~

----------------解决方案--------------------------------------------------------
  相关解决方案