我有这堂课:
public class Interets {
static Statement St ;
public ResultSet rs;
public Interets(Integer IdUser) throws SQLException, ServletException, IOException{
String res=" ";
try{
ResultSet result = St.executeQuery("SELECT description FROM interets, avoir, consomateur WHERE avoir.id_interet=interets.id_interets AND avoir.id_user=consomateur.code_bar AND consomateur.code_bar="+IdUser+"");
ResultSetMetaData resultMeta = (ResultSetMetaData) result.getMetaData();
while(result.next()){
String Newligne=System.getProperty("line.separator");
for(int i = 1; i <= resultMeta.getColumnCount(); i++)
res=res+Newligne+result.getObject(i).toString();
System.out.println(res);
}
}
catch (Exception e) {
System.out.println("Erreur dans la requete d'affichage ");
}
}
}
我想在这样的JSP中调用它的构造函数:
<div id="corps"><h1>
<%Interets inte=new Interets(${IdUser})%>
</h1>
</div>
但是我遇到了像
这样的语法错误“令牌上的语法错误”)“,; expected”
和
“令牌上的语法错误,错误的构造”
所以我该怎么办呢。 谢谢
答案 0 :(得分:0)
由于您使用的是<% ... %>
,因此您应该编写有效的 Java代码,因此您在语句的末尾会遗漏分号;
:
<%Interets inte=new Interets(${IdUser})%>
^here you're missing a semicolon...
此外,您不能将EL与scriptlet混合使用,因此代码应为:
<%
Integer idUser = (Integer)request.getAttribute("IdUser");
//if the attribute is stored in HttpSession (probably)
//or ServletContext (oddly), use the right method to get it
Interets inte =new Interets(idUser);
%>
请注意,这可能会解决您当前的编译问题,但您当前的设计存在一些问题,例如:您没有在Statement
类中的任何位置初始化Interets
,但仍在构造函数中使用它...
在尝试打开与数据库的连接并在Web环境中打印结果之前,您应该首先学习基本的Java和基本JDBC。我不会深入研究这些主题,而是为您当前的代码编写一个可能的解决方案,我会评论错误的代码,以便您注意到错误或不应该做的事情:
public class Interets {
//static Statement St ;
//public ResultSet rs;
public List<String> getDescriptionOfInterets(Integer idUser) {
String res=" ";
//these three: Connection, Statement, ResultSet MUST ALWAYS be local
//variables of a method, NEVER assigned to class field AND NEVER to
//static variables to avoid synchronization problems in a
//multithreaded environment such as a web application
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<String> descriptionList = new ArrayList<String>();
try{
//this is one way to open a database connection
//you should change it to a database connection pool
//which is a different topic you should search about
//since you didn't say which database you're working with
//I'll assume it's mysql
Class.forName("com.mysql.jdbc.Driver");
//similar with the database credentials
String user = "root";
String password = "root";
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/dbname", user, password);
/*
ResultSet result = St.executeQuery("SELECT description FROM interets, avoir, consomateur WHERE avoir.id_interet=interets.id_interets AND avoir.id_user=consomateur.code_bar AND consomateur.code_bar="+IdUser+"");
ResultSetMetaData resultMeta = (ResultSetMetaData) result.getMetaData();
*/
ps = con.prepareStatement("SELECT description "
+ "FROM interets, avoir, consomateur "
+ "WHERE avoir.id = interets.id AND "
+ "avoir.id?user = consomateur.code_bar AND "
+ "contomateur.code_bar = ?");
ps.setInt(1, idUser);
rs = ps.executeQuery();
//while(result.next()){
while (rs.next) {
//String Newligne=System.getProperty("line.separator");
//for(int i = 1; i <= resultMeta.getColumnCount(); i++)
//res=res+Newligne+result.getObject(i).toString();
//System.out.println(res);
descriptionList.add(rs.getString(1));
}
} catch (Exception e) {
//Handle your error! This is very important to check the causes of the problem
//you should at least print the stacktrace
//printing a single line saying "oh no! I got an error" doesn't help
System.out.println("Erreur dans la requete d'affichage.");
e.printStacktrace();
} finally {
//remember to ALWAYS close the opened resources
close(rs);
close(ps);
close(con);
}
}
public void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
//silent...
}
}
}
public void close(Statement st) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
//silent...
}
}
}
public void close(Connection con) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
//silent...
}
}
}
}
然后在你的JSP中:
Description of Interets
<table>
<%
Integer idUser = (Integer)request.getAttribute("IdUser");
//if the attribute is stored in HttpSession (probably)
//or ServletContext (oddly), use the right method to get it
Interets interets = new Interets();
List<String> descriptionList = interets.getDescriptionOfInterets(idUser);
for (String description : descriptionList) {
%>
<tr>
<td><%= description %></td>
</tr>
<%
}
%>
</table>
甚至更好,使用Servlet,Expression Language和JSTL(假设您的JSP名为“interets.jsp”并位于根程序包中):
@WebServlet("/interets")
public class InteretsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Integer idUser = (Integer)session.getAttribute("IdUser");
Interets interets = new Interets();
List<String> descriptionList = interets.getDescriptionOfInterets(idUser);
request.setAttribute("descriptionList", descriptionList);
request.getRequestDispatcher("/interets.jsp").forward(request, response);
}
}
JSP可能看起来像
Description of Interets
<table>
<c:forEach items="${descriptionList}" value="description">
<tr>
<td>${description}</td>
</tr>
</c:forEach>
</table>