StringBuffer的append方法的问题!求助!
本人初学JAVA,JSP!
=======
问题:
源代码:
<%@page contentType= "text/html;charset=gb2312 "%>
<%@page pageEncoding= "UTF-8 "%>
<%@ page import= "java.sql.* " %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri= "http://java.sun.com/jsp/jstl/core " prefix= "c "%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN "
"http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=UTF-8 ">
<title> JSP Page </title>
</head>
<body>
<%
StringBuffer nameList=new StringBuffer();
Connection con=null;
Statement sql=null;
ResultSet rs=null;
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch(ClassNotFoundException e)
{
out.println( "驱动程序加载错误! ");
}
try
{
con=DriverManager.getConnection( "jdbc:obdc:grade ", " ", " ");
sql=con.createStatement();
rs=sql.executeQuery( "SELECT * FROM candidate ");
nameList.append( " <form action= 'vote.jsp ' method= 'POST '> ");
nameList.append( " <table border> ");
nameList.append( " <tr> ");
nameList.append( " <th width= '100 '> "+ "姓名 "+ " </th> ");
nameList.append( " <th width= '50 '> "+ "投票选择 "+ " </th> ");
nameList.append( " </tr> ");
while(rs.next())
{
nameList.append( " <tr> ");
String name=rs.getString(1);
nameList.append( " <td> "+name+ " </td> ");
String s= " <input type= 'radio ' name= 'name ' value= ' "+name+ " '> ";
nameList.append( " <td> "+s+ " </td> ");
nameList.append( " </tr> ");
}
nameList.append( " </table> ");
nameList.append( " <input type= 'submit ' value= '投票 '> ");
nameList.append( " </form> ");
con.close();
out.println(nameList);
}
catch(SQLException e1)
{}
%>
<br>
<form name= "formname1 " action= "showvote.jsp " method= "POST ">
<input type= "submit " value= "查看投票情况 " name= "submit " />
</form>
</body>
</html>
========
在页面输出的结果只有一个按钮“查看投票情况”
而投票的表单没有显示,不知道什么原因,请高手帮帮,谢谢啊!
=========
append这个问题我搞了半天都没搞好,我又建立个JSP文件
源代码如下:
<%@page contentType= "text/html "%>
<%@page pageEncoding= "UTF-8 "%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri= "http://java.sun.com/jsp/jstl/core " prefix= "c "%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN "
"http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=UTF-8 ">
<title> JSP Page </title>
</head>
<body>
<h1> JSP Page </h1>
<%
StringBuffer nameList=new StringBuffer();
nameList.append( " <table border> ");//在表单中包含一个表格
nameList.append( " <tr> ");
nameList.append( " <th width= '100 '> "+ "姓名 "+ " </th> ");//构成表头的字符串
nameList.append( " <th width= '50 '> "+ "投票选择 "+ " </th> ");//构成表头的字符串
nameList.append( " </tr> ");
nameList.append( " </table> ");
%>
</body>
</html>
===
还是不能显示表单信息啊,什么原因呢?是不是程序有问题咯??
[解决办法]
catch(SQLException e1)
{
out.print( "看看这里是否程序异常了 ");
}
[解决办法]
<%@ page contentType= "text/html;charset=gb2312 "%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri= "http://java.sun.com/jsp/jstl/core " prefix= "c "%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN "
"http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=UTF-8 ">
<title> JSP Page </title>
</head>
<body>
<h1> JSP Page </h1>
<%
StringBuffer nameList=new StringBuffer();
nameList.append( " <table border> ");//在表单中包含一个表格
nameList.append( " <tr> ");
nameList.append( " <th width= '100 '> "+ "姓名 "+ " </th> ");//构成表头的字符串
nameList.append( " <th width= '50 '> "+ "投票选择 "+ " </th> ");//构成表头的字符串
nameList.append( " </tr> ");
nameList.append( " </table> ");
out.print(nameList.toString());
%>
</body>
</html>
---------------------------
上面是可正常运行的代码
1.字符集错误
2.没有out.print
[解决办法]
没有把你的namelist print出来,当然不会显示表单了阿
[解决办法]
没有out.print(nameList.toString());
这个所以出不来
加上试试
[解决办法]
catch(SQLException e1)
{
e1.printStackTrace();
}
把这个异常打出来,再看看,应该是这个的问题
[解决办法]
看一下是不是没有数据呀
[解决办法]
先用System.out.println(nameList);看看数据对上不
也把异常打印出来,应该是有异常的问题
[解决办法]
con=DriverManager.getConnection( "jdbc:obdc:grade ", " ", " ");
根据我使用mysql的经验,感觉应该是这个连接建立的问题
mysql下是con=DriverManager.getConnection( "jdbc:mysql://localhost/databasename ", "username ", "password ");
你根据这个改下看看