2011-06-10 72 views
0
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1" import="java.sql.*"%> 
<!DOCTYPE HTML> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Inline Editing: Form Controls</title> 
<script type="text/javascript" src="../../lib.js"></script> 
<script type="text/javascript" src="inline-editing.js"></script> 
<link type="text/css" rel="stylesheet" href="Presidents.css"> 
</head> 
<body> 
<% 
    Connection conn = null; 
    PreparedStatement stmt = null; 
    ResultSet rs = null; 
    try 
    { 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     conn = DriverManager.getConnection("jdbc:odbc:Presidents"); 

     String sql = "SELECT PresidentID, FirstName, LastName, Bio FROM Presidents"; 
     stmt = conn.prepareStatement(sql); 

     rs = stmt.executeQuery(); 
     out.write("<h1>Presidents</h1>"); 
     out.write("<p>Double click on any cell to edit the field. Click off the field to save your change.</p>"); 
     out.write("<table>"); 
     while (rs.next()) 
     { 
      out.write("<tr id='" + rs.getString("PresidentID") + "'>"); 
/*line 31*/   out.write("<td class="editable" title='FirstName'>" + rs.getString("FirstName") + "</td>"); 
/*line 32*/   out.write("<td class="editable" title='LastName'>" + rs.getString("LastName") + "</td>"); 
/*line 33*/  out.write("<td class="editable" title='Bio'>" + rs.getString("Bio") + "</td>"); 
      out.write("</tr>"); 
     } 
     out.write("</table>"); 
    } 
    catch(Exception e) 
    { 
     out.write("failed: " + e.toString()); 
    } 
    finally 
    { 
     if (rs != null) rs.close(); 
     if (stmt != null) stmt.close(); 
     if (conn != null) conn.close(); 
    } 
%> 
</body> 
</html> 

我收到以下错误的线31,32,33数据库连接使用JSP

The method write(String, int, int) in the type Writer is not applicable for the arguments (String, String) 
Syntax error on token "editable", , expected 

我应该怎么做才能解决这个问题?

+2

首先不要在jsp中编写数据库连接逻辑。 – 2011-06-10 09:19:18

+0

Harry Joy是对的,你应该使用servlets或其他后端逻辑来处理数据库查询。 JSP通常只是显示来自后端数据的哑模块。当你有时间的时候看看一些MVC教程。 – deltaforce2 2011-06-10 09:22:23

回答

0

退出您的引号(或使用单引号)。

更改此:

out.write("<td class="editable" title='FirstName'>" + rs.getString("FirstName") + "</td>"); 

这样:

out.write("<td class=\"editable\" title='FirstName'>" + rs.getString("FirstName") + "</td>"); 

或本:

out.write("<td class='editable' title='FirstName'>" + rs.getString("FirstName") + "</td>"); 
1

试试这个:

while (rs.next()) 
     { 
      out.write("<tr id='" + rs.getString("PresidentID") + "'>"); 
/*line 31*/   out.write("<td class=\"editable\" title='FirstName'>" + rs.getString("FirstName") + "</td>"); 
/*line 32*/   out.write("<td class=\"editable\" title='LastName'>" + rs.getString("LastName") + "</td>"); 
/*line 33*/  out.write("<td class=\"editable\" title='Bio'>" + rs.getString("Bio") + "</td>"); 
      out.write("</tr>"); 
     } 

您不是在标签的类名称中转义“。另请考虑我的评论从不在jsp中编写业务逻辑。在servlet中执行数据库连接。 Jsp只能用于演示。

+0

它的工作... thnx ... – Krishan 2011-06-10 09:26:34

+0

@Krishan:不要忘了标记最有用的答案然后接受。另见http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – BalusC 2011-06-10 11:47:44

0

看起来像一个错字:

out.write("<td class="editable" ... 

应该

out.write("<td class='editable' ... 

代替。