jsp问题 谁能帮我解决一下谢谢了
<%
try
{
//以下几项请自行修改
String spath= "myprog\\DataBase\\db1.mdb "; //注意由网站根到文件的路径
String dbpath = application.getRealPath(spath); //转化成物理路径
String dbname = " "; //Acess 数据库用户名,没有则为空
String user = " "; //Acess 数据库密码,没有则为空
//数据库连接字符串
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ= "+dbpath;
//加载驱动程序
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
//建立连接
Connection conn= DriverManager.getConnection(url);
//创建语句对象
Statement stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Statement stmt1=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Statement stmt2=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String SelectStr = " ";
String sql1,sql2,sql3;
int i1,i2,i3;
//一级分类开始
sql1 = "select c1id,c1name from class1 ";
ResultSet rs1 = stmt.executeQuery(sql1);
i1 = 0;
while (rs1.next())
{
if (i1 > 0 )
{SelectStr = SelectStr + "|| ";}
else
{SelectStr = SelectStr + rs1.getString( "c1name ") + "{{ ";}
//显示与值不同时,用下面一行
//SelectStr = SelectStr + rs1( "c1name ") + "@ "+ rs1( "c1id ") + "{{ "
//二级分类开始
sql2 = "select c2id,c2name from class2 where c1name = ' "+ rs1.getString( "c1name ") + " ' ";
ResultSet rs2 = stmt1.executeQuery(sql2);
i2 = 0;
while (rs2.next())
{
if (i2 > 0)
{SelectStr = SelectStr + "** ";}
else
{SelectStr = SelectStr + rs2.getString( "c2name ") + "(( ";}
//显示与值不同时,用下面一行
//SelectStr = SelectStr + rs2( "c2name ") + "@ "+ rs2( "c2id ") + "(( "
//三级分类开始
sql3 = "select c3id,c3name from class3 where c2name = ' "+ rs2.getString( "c2name ")+ " ' ";
ResultSet rs3 = stmt2.executeQuery(sql3);
i3 = 0;
while (rs3.next())
{
if (i3 > 0 )
{SelectStr = SelectStr + "^ ";}
else
{SelectStr = SelectStr + rs3.getString( "c3name ");}
//显示与值不同时,用下面一行
//SelectStr = SelectStr + rs3( "c3name ") + "@ " + rs3( "c3id ")
i3 = i3 + 1;
}
rs3.close();
//三级分类结束
i2 = i2 + 1;
}
rs2.close();
//二级分类结束
i1 = i1 + 1;
}
rs1.close();
stmt.close(); //关闭语句对象
conn.close(); //关闭连接对象
out.println(SelectStr);
}
catch(Exception e){
out.print( "数据库连接错误!,错误信息如下: <br> ");
out.print(e.getMessage());
}
//一级分类结束
%>
<SCRIPT LANGUAGE= "JavaScript ">
<!--
var aa= " <% =SelectStr%> "
</SCRIPT>
为什么总是报错呢var aa= " <% =SelectStr%> ";
[解决办法]
代码太乱了。出错的原因是SelectStr定义在try块里面了,程序执行发生异常时,SelectStr就不存在了,导致illegal start of expression错误。
像你这样使用jdbc话,程序都得死掉。在结果集里面嵌套数据库操作是非常糟糕的思想。
应该把数据从结果集中取道List中,然后在List中做循环遍历操作。
另外Connection、Statement、ResultSet等JDBC接口都要在try外定义并初始化null
在finally里关闭或者释放
这样你的程序发生异常时才不会浪费数据库资源
[解决办法]
<% =这里,你%和=之间有一个空格,不要有那个空格,否则会将“=SelectStr”认为是一个变量,当然会报错了
<%=这种表示方法才对,这种地方不可以为了程序美观而加空格的
就像“==”不可以写成“= =”一样,