VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1117|回复: 4

请教jdbc数据库连接问题?

[复制链接]

1

主题

1

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2020-11-26 18:00:01 | 显示全部楼层 |阅读模式
执行以下程序代码:
mybag.inc文件代码如下:
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.microsoft.jdbc.sqlserver.SQLServerDriver"%>
<%@ page import="java.lang.*"%>
<%@ page contentType="text/html; charset=gb2312"%>
<%
   request.setCharacterEncoding("gb2312");
   String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
   String SERVANDDB="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=school";
   String USER="sa";
   String PWD="sa";   
%>

chaxun.jsp文件代码如下:

<%@ include file="mybag.inc"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<%
//获得请求的参数。
String id=request.getParameter("tuser");
String psw=request.getParameter("pwd");
//连接数据库
Class.forName(CLASSFORNAME);
Connection con = DriverManager.getConnection(SERVANDDB,USER,PWD);
Statement statement=con.createStatement();
String isCorrect="select * from users where userid='" + id + "' and password='" + psw + "'";
ResultSet result=statement.executeQuery(isCorrect);
out.print(result.getString("username"));
%>
</body>
</html>

运行chaxun.jsp文件后出现以下错误,小弟没有办法解决,请教各位大哥
type Exception report

message

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

exception

javax.servlet.ServletException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
        org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
        org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
        org.apache.jsp.tep3_jsp._jspService(tep3_jsp.java:94)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
        com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
        com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
        com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
        com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
        com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
        com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
        com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
        com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
        java.sql.DriverManager.getConnection(DriverManager.java:525)
        java.sql.DriverManager.getConnection(DriverManager.java:171)
        org.apache.jsp.tep3_jsp._jspService(tep3_jsp.java:80)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.

说明:jdbc三个驱动文件已复制到F:\Tomcat 5.0\common\lib下
回复

使用道具 举报

0

主题

14

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-11-27 00:30:01 | 显示全部楼层
修改:
out.print(result.getString("username"));

if(result.next()){
out.print(result.getString("username"));
}else{
out.print("-------no data");
}
回复

使用道具 举报

0

主题

14

帖子

9.00

积分

新手上路

Rank: 1

积分
9.00
发表于 2020-11-27 01:30:01 | 显示全部楼层
还要安装 jdbc 的sp3
回复

使用道具 举报

0

主题

16

帖子

11.00

积分

新手上路

Rank: 1

积分
11.00
发表于 2020-11-27 19:30:01 | 显示全部楼层
String isCorrect="select * from users where userid='" + id + "' and password='" + psw + "'";
改为
String isCorrect="select * from users where userid=" + id + " and password='" + psw + "'";
还有就是
out.print(result.getString("username"));
改为
if(result.next())
{
out.print(result.getString("username"));
}
else
{
out.print("没有此用户和密码");
}
回复

使用道具 举报

0

主题

2

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
发表于 2021-5-9 15:25:56 | 显示全部楼层

安装sp3
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表