Yake_Can 2017-05-17 03:59 采纳率: 33.3%
浏览 1690

resultSet.getString( )搜索数据库方法问题

package com.buy.bean.coin;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.buy.bean.comm.database.DBConnect;

public class UsercoinSelectBean {
//查询用户信息的方法
public String selectUsercoin(String ID, int i) {
//声明查询语句的字符串类型变量
String condition = null;
if(i == 0) {
//如果参数i为0表示查询用户电子货币余额
condition = "select coin from user where id = " + ID;
} else if(i == 1) {
//如果参数i为1表示查询用户已花费的电子货币
condition = "select consume from user where id = " + ID;
} else if(i == 2) {
//如果参数i为2表示查询用户累计的电子货币
condition = "select coin + consume from user where id = " + ID;
} else if(i == 3) {
//如果参数i为3表示查询用户的姓名
condition = "select name from user where id = " + ID;
} else if(i == 4) {
//如果参数i为4表示查询用户的类型
condition = "select type from user where id = " + ID;
}
ResultSet rs = null;
Statement sql = null;
String str = new String();
try {
int coin;
//获得数据库连接
Connection conn = DBConnect.getConnection();
//建立Statement
sql = conn.createStatement();
//执行数据库查询
rs = sql.executeQuery(condition);
//取结果集记录中的第一个字符
while(rs.next()) {
str = rs.getString(1);
}
//将取得的字符串转化成整型
coin = Integer.parseInt(str);
//关闭结果集
rs.close();
//关闭Statement
sql.close();
//关闭连接
conn.close();
} catch (Exception e) {
//如果查询错误
return new String("can not select");
}
//返回结果
return str;
}
}


用resultSet.getString( )方法
name和type都是varchar类型的 前面几个整型都能打印出来
varchar类型的打印不出来
查询语句正常 放入数据库里能检索到
求教什么问题。

  • 写回答

1条回答 默认 最新

  • sunshine_0518 2017-05-19 02:25
    关注

    你转化为整型,Integer.parseInt(str)其中str必须是纯数字才能转换,不是数字怎么转换成整型?

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况