<span style="font-size:18px;"><%@ page contentType="text/html;charset=utf-8" %>
<%@ page language="java" import="java.util.*" import="java.sql.*" import="com.mysql.jdbc.Driver" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<html>
<head><title>the first jsp page</title></head>
<body>
<sql:setDataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
user="root"
password="wkd22775"
var="test"/>
<sql:query sql="select question from qt where qid=1" var="question1"/>
<sql:query sql="select question from qt where qid=2" var="question2" />
<sql:query sql="select selection from st where qid=1 and sid=1" var="selection11"/>
<sql:query sql="select selection from st where qid=1 and sid=2" var="selection12"/>
<sql:query sql="select selection from st where qid=2 and sid=1" var="selection21"/>
<sql:query sql="select selection from st where qid=2 and sid=2" var="selecton22"/>
<form >
the first question:<c:out value="${question1}"/>
the second question:<c:out value="${question2}"/>
</form>
</body>
</html></span>
这是我原来的JSP页面代码,起初没有将mysql-connector-java-bin.jar导入到buidlpath中,报错No suitable driver,导入之后报空指针错误,上网查资料发现,原来是<sql:query>中没有指定使用的数据库,应该将代码改为如下所示
<span style="font-size:18px;"><sql:query dataSource="${test}" sql="select question from qt where qid=1" var="question1"/></span>
这样就不报错了,但是我有一个疑惑,<sql:setDataSource>的默认范围不是page么,为什么同一个页面还是需要指定datasource