2011-06-10 65 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' ... 

代替。