编译的一个错误怎么改啊。求助,急!在线等!

wwwfox111 2003-09-12 02:57:47
我编写了一个 jsp页面,是用来向Access存储数据,代码如下:
里面有个数据转换方法如下:
<%!
public String getStr(String str)
{ try{
if(str.length()==0)
return null;
String temp=str;
byte[] temp_p=temp.getBytes("ISO-8859-1");
return new String(temp_p,"GB2313");
}catch(Exception e){ }
}
%>
编译出错,错误提示:
Return required at end of java.lang.String getStr(java.lang.String).
String getStr(String str)
^
请问怎么改阿
...全文
41 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liushenling 2003-09-12
  • 打赏
  • 举报
回复
在我们的程序的每个出口都应该有值返回!
wwwfox111 2003-09-12
  • 打赏
  • 举报
回复
问题已经解决了。是转换方法出了错。应该是:
String getStr(String str) throws Exception
{
if(str.length()==0)
return null;
String temp=str;
byte[] temp_p=temp.getBytes("8859_1");
return new String(temp_p);
}
呵呵,谢谢各位的指点了。
wwwfox111 2003-09-12
  • 打赏
  • 举报
回复
谢谢,现在jsp页面可以编译通过了。可是在表单输入数据点击发送后无法插入数据库,也无法转到index.jsp页面。
我把代码帖出来,麻烦帮我诊断一下了。急啊!
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%!
String getStr(String str) throws Exception
{
if(str.length()==0)
return null;
String temp=str;
byte[] temp_p=temp.getBytes("ISO-8859-1");
return new String(temp_p,"GB2313");
}


String cutLine(String str){
int indx=0;
while((indx=str.indexOf("\n"))!=-1){
str=str.substring(0,indx);
str=str+"<br>"+str.substring(indx+1);
}
return(str);
}
%>
<html>
<head>
<title>增加留言</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/style" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#CCCCCC">
<%String action=request.getParameter("action");
if (action==null)
{%>
<form name="form1" method="post" action="add.jsp?action=add">
<table width="41%" height="267" border="0" align="center" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
<tr bgcolor="#FFFFFF">
<td height="27" colspan="2" align="center" background="images/table.gif">书写留言</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="19%" height="27" align="center">姓名:</td>
<td width="81%" height="27"><input name="username" type="text" id="username"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="27" align="center">性别:</td>
<td height="27"><select name="sex" id="sex">
<option value="男" selected>男</option>
<option value="女">女</option>
</select></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="27" align="center">邮件:</td>
<td height="27"><input name="email" type="text" id="email"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="25" align="center">网址:</td>
<td height="25"><input name="url" type="text" id="url"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="31" align="center">QQ:</td>
<td height="31"><input name="qq" type="text" id="qq"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="31" align="center">头象:</td>
<td height="31"><input name="tx" type="radio" value="images/face01.gif" checked>
<img src="images/face01.gif" width="32" height="32">
<input type="radio" name="tx" value="images/face02.gif">
<img src="images/face02.gif" width="32" height="32">
<input type="radio" name="tx" value="images/face03.gif">
<img src="images/face03.gif" width="32" height="32"> <br>
<input type="radio" name="tx" value="images/face04.gif">
<img src="images/face04.gif" width="32" height="32">
<input type="radio" name="tx" value="images/face05.gif">
<img src="images/face05.gif" width="32" height="32">
<input type="radio" name="tx" value="images/face06.gif">
<img src="images/face06.gif" width="32" height="32"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="31" align="center">留言内容:</td>
<td height="31"><textarea name="content" cols="40" rows="10" id="content"></textarea></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="31" colspan="2" align="center"><input type="submit" name="Submit" value="增加">
 
<input type="reset" name="Submit2" value="重写">
 
<input type="button" name="Submit22" value="返回" onClick="window.location.href='index.jsp'"></td>
</tr>
</table>
</form>
<%}else{%><jsp:useBean id="guestbook" class="guestbook.GuestBookJdbc" scope="page"/>
<%
String username=request.getParameter("username");
String tx=request.getParameter("tx");
String email=request.getParameter("email");
String url=request.getParameter("url");
String sex=request.getParameter("sex");
String qq=request.getParameter("qq");
String ip=request.getRemoteAddr();
String content=request.getParameter("content");
boolean corrent=true;
int err=0;
String errmsgs[]=new String[20];
if(username.length()==0){
corrent=false;
errmsgs[err]="无名氏至少也给我个名字吧";
err++;
}
if(email.length()==0){
corrent=false;
errmsgs[err]="电子邮件忘了添了";
err++;
}
if(content.length()==0){
corrent=false;
errmsgs[err]="至少也给我写点什么了";
err++;
}
out.println("<h1><center>");
out.println("<font color=\"blue\">");
out.println("发生错误信息</font>");
out.println("<hr></h1>");
if(corrent=false){
for(int i=0;i<err;i++){
out.println("<font color=red>");
out.println(errmsgs[i]+"</font><br>");
}
out.println("<hr><br><a href=index.jsp>0我要看别人的留言0</a>");
out.println("<br><a href=add.jsp>0回留言看版0</a>");
out.println("</center>");
}else{
int cout=0;
int year=0;
int month=0;
int day=0;
int hour=0;
int minute=0;
int second=0;
int point=0;
String time;
GregorianCalendar calendar;
calendar=new GregorianCalendar();
year=calendar.get(calendar.YEAR);
month=calendar.get(calendar.MONTH)+1;
day=calendar.get(calendar.DAY_OF_MONTH);
hour=calendar.get(calendar.HOUR_OF_DAY);
minute=calendar.get(calendar.MINUTE);
second=calendar.get(calendar.SECOND);
time=year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second;
username=getStr(username);
tx=getStr(tx);
email=getStr(email);
url=getStr(url);
sex=getStr(sex);
qq=getStr(qq);
ip=getStr(ip);
content=cutLine(content);
content=getStr(content);
time=getStr(time);
Connection conn=guestbook.GetConn();
Statement sqlrs= conn.createStatement() ;
String sql="insert into Guestbook(username,tx,email,url,qq,content,ip,sex,date) values('"+username+"','"+tx+"','"+email+"','"+url+"','"+qq+"','"+content+"','"+ip+"','"+sex+"','"+time+"')";
sqlrs.executeUpdate(sql);
response.sendRedirect("index.jsp");
}
}

%>
<table width="75%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="22" align="center">三人行工作室 </td>
</tr>
</table>
</body>
</html>
chanceqw 2003-09-12
  • 打赏
  • 举报
回复
是关于return的问题,如果进入catch子句,将没有返回值
方法一:
//修改catch子句
catch(Exception e){
System.err.println("Encoding error!");
return null;
}
事实上在肯定不会写错"ISO-8859-1"以及"GB2313"的情况下
完全可以让函数抛出这些错误不处理
方法二:
public String getStr(String str) throws Exception
{
if(str.length()==0)
return null;
String temp=str;
byte[] temp_p=temp.getBytes("ISO-8859-1");
return new String(temp_p,"GB2313");
}

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧