使用jsp进行数据库连接

时间:2011-06-10 09:15:44

标签: jsp ms-access

<%@ 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行收到以下错误

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

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

3 个答案:

答案 0 :(得分: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应该只用于演示。

答案 1 :(得分: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>");

答案 2 :(得分:0)

看起来像是一个错字:

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

应该是

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

代替。

相关问题