| |

VerySource

 Forgot password?
 Register
Search
View: 1480|Reply: 4

Ask jdbc database connection problem?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-11-26 18:00:01
| Show all posts |Read mode
Execute the following program code:
The code of mybag.inc file is as follows:
<%@ 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";
%>

The chaxun.jsp file code is as follows:

<%@ include file="mybag.inc"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Untitled document</title>
</head>
<body>
<%
//Get the requested parameters.
String id=request.getParameter("tuser");
String psw=request.getParameter("pwd");
//Connect to the database
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>

After running the chaxun.jsp file, the following error occurred. There is no way to solve the problem. Please ask everyone.
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.

Note: The three jdbc driver files have been copied to F:\Tomcat 5.0\common\lib
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-11-27 00:30:01
| Show all posts
modify:
out.print(result.getString("username"));
for
if(result.next()){
out.print(result.getString("username"));
}else{
out.print("-------no data");
}
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-11-27 01:30:01
| Show all posts
Also install jdbc sp3
Reply

Use magic Report

0

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-11-27 19:30:01
| Show all posts
String isCorrect="select * from users where userid='" + id + "'and password='" + psw + "'";
To
String isCorrect="select * from users where userid=" + id + "and password='" + psw + "'";
There is
out.print(result.getString("username"));
To
if(result.next())
{
out.print(result.getString("username"));
}
else
{
out.print("There is no such user and password");
}
Reply

Use magic Report

0

Threads

2

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2021-5-9 15:25:56
| Show all posts
Install SP3
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list