各位大侠,帮帮忙!只要成功,我愿出200分!--在线等待 (200分)

  • 主题发起人 主题发起人 zlc_168
  • 开始时间 开始时间
Z

zlc_168

Unregistered / Unconfirmed
GUEST, unregistred user!
这个问题已经有4、5天未解决了。那就是从JSP中连接sql server数据库,详细资料如下:
jdk:jdk1.40;
apache:apache_2.0.43;
tomcat:tomcat-4.1.24;
sql server 2000 driver for jdbc;
安装好以上组件后,输入http://localhost和http://localhost:8080均可见到正确的
运行画面。并且已经将sql server 2000 driver for jdbc中三个jar文件:
mabase.jar、msutil.jar、mssqlserver.jar拷贝到jdk/bin目录下,相应的path和
classpath均已设置,但是一运行以下代码就出错,请哪位大侠给看看,如解决,
本人将给200分,源码:

<html>
<head>
<title>New Page 1</title>
</head>
<body>
<%@ page language="java" import="java.sql.*" contentType="text/html;charSet=gb2312"%>
<%String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://zhy:1433;DatabaseName=pubs";
String user="sa";
String password="";

String sqlstr="select * from jobs";
try{
Class.forName(driver).newInstance();
Connection conn=DriverManager.getConnection(url,user,password);
}
catch(Exception e){
out.println(e.toString());
}
try{
Statement lstmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=lstmt.executeQuery(sqlstr);
}
catch(Exception e){
out.println(e.toString());
}
while (rs.next()){
out.println("<tr><td>"+rs.getString("job_desc")+"</tr></td>");
}
conn.close();
%>
%>
</body>
</html>

出错信息:
Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Since fork is true, ignoring compiler setting.
[javac] Compiling 1 source file
[javac] Since fork is true, ignoring compiler setting.
[javac] D:/Tomcat/work/Standalone/localhost/_/query_jsp.java:62: cannot resolve symbol
[javac] symbol : variable conn
[javac] location: class org.apache.jsp.query_jsp
[javac] Statement lstmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
[javac] ^
[javac] D:/Tomcat/work/Standalone/localhost/_/query_jsp.java:68: cannot resolve symbol
[javac] symbol : variable rs
[javac] location: class org.apache.jsp.query_jsp
[javac] while (rs.next()){
[javac] ^
[javac] D:/Tomcat/work/Standalone/localhost/_/query_jsp.java:69: cannot resolve symbol
[javac] symbol : variable rs
[javac] location: class org.apache.jsp.query_jsp
[javac] out.println(""+rs.getString("job_desc")+"");
[javac] ^
[javac] D:/Tomcat/work/Standalone/localhost/_/query_jsp.java:71: cannot resolve symbol
[javac] symbol : variable conn
[javac] location: class org.apache.jsp.query_jsp
[javac] conn.close();
[javac] ^
[javac] 4 errors


 
难道这么简单的问题,都没有人会回答吗?
 
哈哈,我来蹭分了,这是一个典型的try ... catch结合变量定义的问题。
不过我没有测试过,你自己试吧
<html>
<head>
<title>New Page 1</title>
</head>
<body>
<%@ page language="java" import="java.sql.*" contentType="text/html;charSet=gb2312"%>
<%String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://zhy:1433;DatabaseName=pubs";
String user="sa";
String password="";

String sqlstr="select * from jobs";
Connection conn = null;
Statement lstmt = null;
ResultSet rs = null;
try
{
Class.forName(driver).newInstance();
conn=DriverManager.getConnection(url,user,password);
lstmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=lstmt.executeQuery(sqlstr);
while (rs.next())
{
out.println("<tr><td>"+rs.getString("job_desc")+"</tr></td>");
}
}
catch(Exception e)
{
out.println(e.toString());
}
finally
{
if(conn!=null)
{
conn.close();
}
}
%>
%>
</body>
</html>
 
呵呵,我也来蹭分,
首先,报错是因为conn定义的范围太小,你在try{}中定义,他的作用范围不够,
应该先定义:
Connection conn = null;
Statement lstmt = null;
ResultSet rs = null;
然后才能用try{};
2.你说把mabase.jar、msutil.jar、mssqlserver.jar拷贝到jdk/bin目录下
我想是不是该拷贝到jdk/lib或者jdk/jre/lib呢?
 
后退
顶部