怎样把int类型转换到String类型?

ameny 2004-10-30 05:54:06
这就好象
int page=1;
String sql = "select * from images where partake='"+page+"'";
会出错
我也尝试用
int page=1;
String sql = "select * from images where partake='"+page.toString()+"'";
也出错啊!
求助各位资深大哥
http://community.csdn.net/Expert/topic/3506/3506213.xml?temp=.8217737
同时给分
...全文
484 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
ameny 2004-11-01
  • 打赏
  • 举报
回复
jamesfancy()边城狂人(James Fancy)你的分我在其它页面给你!!
ameny 2004-11-01
  • 打赏
  • 举报
回复
我这就结帖,谢谢各位的帮助,再次感谢!!!
ameny 2004-11-01
  • 打赏
  • 举报
回复
我知道问题的症结了,感谢treeroot(根根)。(真是一语道破玄机啊!!)
frhwxx 2004-11-01
  • 打赏
  • 举报
回复
mark
cangwu_lee 2004-11-01
  • 打赏
  • 举报
回复
((Integer)page.toString()) 估计不行吧

应该是 String.valueOf(page) 呀
dongxiaoy 2004-11-01
  • 打赏
  • 举报
回复
int page=1;
String sql = "select * from images where partake='"+page+"'";
变为
String sql = "select * from images where partake='"+((Integer)page.toString())+"'";
treeroot 2004-11-01
  • 打赏
  • 举报
回复
字符串为null的时候你怎么可以转化成int呢?
fly3484139 2004-11-01
  • 打赏
  • 举报
回复
String sql = "select * from images where partake="+page; 去掉单引号看看
jinannanhai79 2004-11-01
  • 打赏
  • 举报
回复
你把PageRecordInt和PageIDInt两个变量的值打出来看看,可能有空值
jinannanhai79 2004-11-01
  • 打赏
  • 举报
回复
java.lang.NumberFormatException: null
错误原因为程序中出现了空值,看看你自己的程序中变量的赋值可能有问题
eidolon_warrior 2004-11-01
  • 打赏
  • 举报
回复
String.valueOf(page)
边城狂人 2004-11-01
  • 打赏
  • 举报
回复
String sql = "select * from images where partake='"+page+"'";

你的数据库里,如果 partake 字段是整型或者其它数值型的,就不要加单引号,直接加 page 就可以了
边城狂人 2004-11-01
  • 打赏
  • 举报
回复
可能是这里有问题

int PageRecordInt=Integer.parseInt(PageRecordString);
int PageIDInt=Integer.parseInt(PageIDString);

其中 PageRecordString 或者 PageIDString 为 null
你可以改成 下面的代码 试试


int PageRecordInt;
try {
PageRecordInt=Integer.parseInt(PageRecordString);
} catch (NumberFormatException e) {
PageRecordInt = 0;
}
int PageIDInt=Int;
try {
PageIDInt=Integer.parseInt(PageIDString);
} catch (NumberFormatException e) {
PageIDInt = 0;
}

ameny 2004-10-30
  • 打赏
  • 举报
回复
你意思是说该成这样

String sql = "select * from images where partake=1 limit '"+ScreenOut+"','"+PageRecord+"'";

可这样一样出错啊,错误信息和上面一样

我现在重启一下,等会回来
java悠悠 2004-10-30
  • 打赏
  • 举报
回复
要看你的partake类型了,不是数字用
String p=new Integer(page).toString();
String sql="select * from images where partake='"+p+"'";
ameny 2004-10-30
  • 打赏
  • 举报
回复
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>

<%
String PageRecordString=request.getParameter("PageRecord");
String PageIDString=request.getParameter("PageID");

int PageRecordInt=Integer.parseInt(PageRecordString);
int PageIDInt=Integer.parseInt(PageIDString);

if(PageRecordInt<=0)PageRecordInt=1;
if(PageIDInt<=0)PageIDInt=1;

int ScreenOutInt=(PageIDInt-1)*PageRecordInt;

String PageRecord=new Integer(PageRecordInt).toString();
String PageID=new Integer(PageIDInt).toString();
String ScreenOut=new Integer(ScreenOutInt).toString();
%>

<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://127.0.0.1:3306/photo?user=root&password=123456";

//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

String sql = "select * from images where partake=1 limit "+ScreenOut+","+PageRecord;

ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第1个字段内容为:<%=rs.getString(1)%><br>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
还是出错,错误信息为
500 Servlet Exception

java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:436)
at java.lang.Integer.parseInt(Integer.java:518)
at _jsp._index__jsp._jspService(/index.jsp:8)
at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
at com.caucho.jsp.Page.pageservice(Page.java:571)
at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:155)
at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:190)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:177)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:221)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:263)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:331)
at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:464)
at com.caucho.util.ThreadPool.run(ThreadPool.java:408)
at java.lang.Thread.run(Thread.java:534)
Resin-3.0.8 (built Tue, 08 Jun 2004 02:25:40 PDT)
vgvg 2004-10-30
  • 打赏
  • 举报
回复
String p=new Integer(page).toString();
String sql="select * from images where partake="+p;
ameny 2004-10-30
  • 打赏
  • 举报
回复
这和我上面的第一句不是一样嘛!
fruitfull 2004-10-30
  • 打赏
  • 举报
回复
<%
int page=1;
String sql="select * from images where partake='"+page+"'";
%>

这样写你试试看.可不可以.
ameny 2004-10-30
  • 打赏
  • 举报
回复
还有这里的分
http://community.csdn.net/Expert/topic/3320/3320866.xml?temp=.5787165

81,092

社区成员

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

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