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必须是纯数字才能转换,不是数字怎么转换成整型?

    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?