我的JSP环境已经配置好了(已经测试通过,且已经装了JDBC驱动),可是连接数据库时却死活连不上,郁闷
下面我的JSP代码:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.lang.*,java.sql.*,java.io.*,java.util.*"%><html>
<body>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs","sa","123456");Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("select job_id,job_desc from jobs");rs.close(); stmt.close(); conn.close();%><%out.println("<h1>Hello World! </h1>");%></html>
</body>
下面是报错信息,提示好像socket不能建立,不知道该怎么解决?
type Exception reportmessage 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.index_jsp._jspService(index_jsp.java:100)
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:512)
java.sql.DriverManager.getConnection(DriverManager.java:171)
org.apache.jsp.index_jsp._jspService(index_jsp.java:61)
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
下面我的JSP代码:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.lang.*,java.sql.*,java.io.*,java.util.*"%><html>
<body>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs","sa","123456");Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("select job_id,job_desc from jobs");rs.close(); stmt.close(); conn.close();%><%out.println("<h1>Hello World! </h1>");%></html>
</body>
下面是报错信息,提示好像socket不能建立,不知道该怎么解决?
type Exception reportmessage 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.index_jsp._jspService(index_jsp.java:100)
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:512)
java.sql.DriverManager.getConnection(DriverManager.java:171)
org.apache.jsp.index_jsp._jspService(index_jsp.java:61)
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
建意你最好写个连接数据库的javaBean,哪样使用起来更方便。
package Logistics.CustomOrder;import java.sql.*;
public class conn
{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xk;user=sa;password=jiaojian";
Connection conn=null;
ResultSet rs=null;
public conn()
{
try {
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e)
{
System.out.println("无法建立数据库连接!:"+e.getMessage());
}
}
public Connection setConnect()throws Exception{
try{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sConnStr);
}
catch(ClassNotFoundException e)
{
System.out.println("无法建立数据库连接!:"+e.getMessage());
}
return conn;
}
public void executeUpdate(String sql) throws Exception
{
//sql= new String(sql.getBytes("GBK"),"ISO8859_1");
try
{
conn= DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
PreparedStatement stmt1=conn.prepareStatement(sql);
stmt.executeUpdate(sql);
stmt1.executeUpdate(sql);
//conn.close();
//stmt.close();
}
catch(SQLException ex)
{
System.out.println("更新数据操作失败!"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql) throws Exception
{
rs = null;
try
{
//sql= new String(sql.getBytes("GBK"),"ISO8859_1");
conn= DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
//conn.close();
//stmt.close();
}
catch(SQLException ex)
{
System.out.println("执行查询出错!"+ex.getMessage());
}
return rs;
}
public String convert(String str){
byte newstr[]=new byte[str.length()];
for(int i=0;i<str.length();i++)
newstr[i]=(byte)str.charAt(i);
return new String(newstr);
}
}
你的这个JSP文件是放在哪个目录下的?
public class conn
{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xk;user=sa;password=jiaojian";
Connection conn=null;
ResultSet rs=null;
public conn()
{
try {
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e)
{
System.out.println("无法建立数据库连接!:"+e.getMessage());
}
}
public Connection setConnect()throws Exception{
try{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sConnStr);
}
catch(ClassNotFoundException e)
{
System.out.println("无法建立数据库连接!:"+e.getMessage());
}
return conn;
}
public void executeUpdate(String sql) throws Exception
{
//sql= new String(sql.getBytes("GBK"),"ISO8859_1");
try
{
conn= DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
PreparedStatement stmt1=conn.prepareStatement(sql);
stmt.executeUpdate(sql);
stmt1.executeUpdate(sql);
//conn.close();
//stmt.close();
}
catch(SQLException ex)
{
System.out.println("更新数据操作失败!"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql) throws Exception
{
rs = null;
try
{
//sql= new String(sql.getBytes("GBK"),"ISO8859_1");
conn= DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
//conn.close();
//stmt.close();
}
catch(SQLException ex)
{
System.out.println("执行查询出错!"+ex.getMessage());
}
return rs;
}
public String convert(String str){
byte newstr[]=new byte[str.length()];
for(int i=0;i<str.length();i++)
newstr[i]=(byte)str.charAt(i);
return new String(newstr);
}
}
我编写的程序能成功运行,把数据库关了,再运行
就是[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
异常!