有关jsp嵌套查询,请求帮助!

彭哥 发布于 2012/01/30 07:08
阅读 944
收藏 0

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
Connection con;
Statement sql;
Statement sql1;
ResultSet rs,rs1;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch(ClassNotFoundException e)
{
 out.print("<h3>加载数据库驱动出错<h3>");
}
try
{
con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/pyl_jsp?user=root&password=123456&useUnicode=true&characterEncoding=utf-8");//创建语句对象
 sql=con.createStatement();
sql1=con.createStatement();
}
catch(SQLException e)
{
 out.print("<h3>连接数据库出错<h3>");
}
   

%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>企业网站通用模版1</title>

<%
 rs=sql.executeQuery ( "select id,Pid,Pname from pyl_main ");
while(rs.next()){
rs1=sql1.executeQuery( "select id,Pid,Pname from pyl_main where Pid=1");
while(rs1.next()){
out.println(rs1.getString( "Pname"));
out.println( " </br> ");
}
}
%>

</body>
</html>

程序精简上面的如此简单,结果却依然一样出错!

加载中
0
designer
designer

大哥,为什么jsp写成和asp一样,在里面嵌套java代码,看看你的那个获取连接的,

con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost /pyl_jsp?user=root&password=123456&useUnicode=true& characterEncoding=utf-8");//创建语句对象

jdbc:mysql://localhost:端口号..看看是不是这样出问题了,另外你好歹把错误信息贴出来...

0
彭哥
彭哥

引用来自“designer”的答案

大哥,为什么jsp写成和asp一样,在里面嵌套java代码,看看你的那个获取连接的,

con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost /pyl_jsp?user=root&password=123456&useUnicode=true& characterEncoding=utf-8");//创建语句对象

jdbc:mysql://localhost:端口号..看看是不是这样出问题了,另外你好歹把错误信息贴出来...

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 36 in the jsp file: /Connections/connexe.jsp
Generated servlet error:
D:\WATMServer\Tomcat5\work\Catalina\localhost\_\org\apache\jsp\Connections\connexe_jsp.java:81: variable sql might not have been initialized
rs=sql.executeQuery ( "select id,Pid,Pname from pyl_main ");
    ^


An error occurred at line: 36 in the jsp file: /Connections/connexe.jsp
Generated servlet error:
D:\WATMServer\Tomcat5\work\Catalina\localhost\_\org\apache\jsp\Connections\connexe_jsp.java:83: variable sql1 might not have been initialized
rs1=sql1.executeQuery( "select id,Pid,Pname from pyl_main where Pid=1");
    ^
2 errors
0
虫虫
虫虫

引用来自“彭哥”的答案

引用来自“designer”的答案

大哥,为什么jsp写成和asp一样,在里面嵌套java代码,看看你的那个获取连接的,

con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost /pyl_jsp?user=root&password=123456&useUnicode=true& characterEncoding=utf-8");//创建语句对象

jdbc:mysql://localhost:端口号..看看是不是这样出问题了,另外你好歹把错误信息贴出来...

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 36 in the jsp file: /Connections/connexe.jsp
Generated servlet error:
D:\WATMServer\Tomcat5\work\Catalina\localhost\_\org\apache\jsp\Connections\connexe_jsp.java:81: variable sql might not have been initialized
rs=sql.executeQuery ( "select id,Pid,Pname from pyl_main ");
    ^


An error occurred at line: 36 in the jsp file: /Connections/connexe.jsp
Generated servlet error:
D:\WATMServer\Tomcat5\work\Catalina\localhost\_\org\apache\jsp\Connections\connexe_jsp.java:83: variable sql1 might not have been initialized
rs1=sql1.executeQuery( "select id,Pid,Pname from pyl_main where Pid=1");
    ^
2 errors
错误说的很清楚。。。。sql这个变量没有初始化 =.=
0
彭哥
彭哥

引用来自“虫虫”的答案

引用来自“彭哥”的答案

引用来自“designer”的答案

大哥,为什么jsp写成和asp一样,在里面嵌套java代码,看看你的那个获取连接的,

con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost /pyl_jsp?user=root&password=123456&useUnicode=true& characterEncoding=utf-8");//创建语句对象

jdbc:mysql://localhost:端口号..看看是不是这样出问题了,另外你好歹把错误信息贴出来...

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 36 in the jsp file: /Connections/connexe.jsp
Generated servlet error:
D:\WATMServer\Tomcat5\work\Catalina\localhost\_\org\apache\jsp\Connections\connexe_jsp.java:81: variable sql might not have been initialized
rs=sql.executeQuery ( "select id,Pid,Pname from pyl_main ");
    ^


An error occurred at line: 36 in the jsp file: /Connections/connexe.jsp
Generated servlet error:
D:\WATMServer\Tomcat5\work\Catalina\localhost\_\org\apache\jsp\Connections\connexe_jsp.java:83: variable sql1 might not have been initialized
rs1=sql1.executeQuery( "select id,Pid,Pname from pyl_main where Pid=1");
    ^
2 errors
错误说的很清楚。。。。sql这个变量没有初始化 =.=

有的,你看看前面的代码就知道了!

0
Jeky
Jeky

确实sql变量没有初始化,而且sql1也没有初始化

因为你初始化这两个变量的过程是在一个try块里,无法保证他们确实被初始化了,Java语言要求你使用变量的时候确定这个变量一定是初始化过的

你可以这样:sql = null;sql1 = null;

还有 那个body标签没开头,head标签没闭合

修改如下:

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
    Connection con;
    Statement sql = null;
    Statement sql1 = null;
    ResultSet rs, rs1;
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    } catch (ClassNotFoundException e) {
        out.print("<h3>加载数据库驱动出错<h3>");
    }
    try {
        con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/pyl_jsp?user=root&password=123456&useUnicode=true&characterEncoding=utf-8");//创建语句对象
        sql = con.createStatement();
        sql1 = con.createStatement();
    } catch (SQLException e) {
        out.print("<h3>连接数据库出错<h3>");
    }
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>企业网站通用模版1</title>
    </head>
    <body>
        <%
            rs = sql.executeQuery("select id,Pid,Pname from pyl_main ");
            while (rs.next()) {
                rs1 = sql1.executeQuery("select id,Pid,Pname from pyl_main where Pid=1");
                while (rs1.next()) {
                    out.println(rs1.getString("Pname"));
                    out.println(" </br> ");
                }
            }
        %>

    </body>
</html>

0
彭哥
彭哥
java游戏规则也太挑了吧,刚才我试了改了,但错误码率依然如旧!
Z
ZYud
sql=null,sql1=null;试试在try之前加
0
Z
ZYud
java 静态检查不通过
返回顶部
顶部