java.sql.SQLException:getInt()的值无效 - 'Glomindz支持'

时间:2013-07-09 08:26:30

标签: java

我无法通过以下编码从我的数据库表中检索列值,控制台中已显示一条消息:

java.sql.SQLException: Invalid value for getInt() - 'Glomindz Support'

我的代码是:

package com.glomindz.mercuri.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.glomindz.mercuri.util.MySingleTon;

public class UserServicesDAO {

    private Connection connection;

    public UserServicesDAO() {
        // connection = new MySingleTon().getConnection();
        connection = MySingleTon.getInstance().getConnection();

    }

    public void get_all_data() {
    }

    public Map<Integer, String> get_all_data1() {
        HashMap<Integer, String> result = new HashMap<Integer, String>();
        String query = "SELECT * FROM spl_user_master";
        try {
            PreparedStatement stmt = connection.prepareStatement(query);
            boolean execute = stmt.execute();
            System.out.println(execute);
            ResultSet resultSet = stmt.getResultSet();
            System.out.println(resultSet.getMetaData());
            while (resultSet.next()) {
                result.put(resultSet.getInt(1), resultSet.getString("id"));
                result.put(resultSet.getInt(2), resultSet.getString("name"));
                result.put(resultSet.getInt(3), resultSet.getString("email"));
                result.put(resultSet.getInt(4), resultSet.getString("mobile"));
                result.put(resultSet.getInt(5), resultSet.getString("password"));
                result.put(resultSet.getInt(6), resultSet.getString("role"));
                result.put(resultSet.getInt(7), resultSet.getString("status"));
                result.put(resultSet.getInt(8),
                        resultSet.getString("last_update"));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        new UserServicesDAO().get_all_data1();
    }
}

我的db表架构是:

id  name    email   mobile  password    role    status  last_update
1   Glomindz Support    support@glomindz.com    9854087006  cbf91a71c11d5ec348b0c7e9b2f0055e    admin   1   2013-05-02 22:05:14
2   Amarjyoti Das   amarjyotidas@splcare.com    9864092598  88f2dccb02b2a20615211e5492f85204    admin   1   2013-04-26 05:44:41

6 个答案:

答案 0 :(得分:3)

您将每个列检索为密钥的int。我假设其中一些列代表String s或Date s。

while(resultSet.next()){
    result.put(resultSet.getInt(1),resultSet.getString("id"));
    result.put(resultSet.getInt(2),resultSet.getString("name")); //Most likely a String
    result.put(resultSet.getInt(3),resultSet.getString("email"));
    result.put(resultSet.getInt(4),resultSet.getString("mobile"));
    result.put(resultSet.getInt(5),resultSet.getString("password"));
    result.put(resultSet.getInt(6),resultSet.getString("role"));
    result.put(resultSet.getInt(7),resultSet.getString("status"));
    result.put(resultSet.getInt(8),resultSet.getString("last_update"));  //Most likely a date
}

数据类型与getInt()方法返回的对象/值之间的不一致会导致错误。我建议在您的域中构建/建模一个存储表中行的对象。类似的东西:

public class User{
    private Integer id;
    private String name;
    private String email;
    private String mobile;
    private String password;
    private String role;
    private String status;
    private Date lastUpdate;

    /*  Get and set methods for each field */
}

然后构建一个Map,其中包含对象作为值,id作为键:

   //Use Map interface here, also notice generic arguments <Integer,User>
   Map<Integer, User> result = new HashMap<Integer, User>();
   try {
        PreparedStatement stmt = connection.prepareStatement(query);
        boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());
        while(resultSet.next()){
            User user = new User();
            user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString("name");
            user.setEmail(resultSet.getString("email");
            //do this for each field, using appropriate method for type...

            //then add to map
            result.put(user.getId(), user);

        }

答案 1 :(得分:2)

我认为您需要将表格中的所有数据作为地图。但是您的数据库中可能有多行,因此您基本上需要一个地图列表!将您的方法修改为以下内容: -

public List<Map<Integer, String>> get_all_data1() {
    List<Map<Integer, String>> allRows = new ArrayList<Map<Integer, String>>();
    String query = "SELECT * FROM spl_user_master";
    try {
        PreparedStatement stmt = connection.prepareStatement(query);
        boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());
        while (resultSet.next()) {
            Map<Integer, String> result = new HashMap<Integer, String>();
            result.put(1, resultSet.getString("id"));
            result.put(2, resultSet.getString("name"));
            result.put(3, resultSet.getString("email"));
            result.put(4, resultSet.getString("mobile"));
            result.put(5, resultSet.getString("password"));
            result.put(6, resultSet.getString("role"));
            result.put(7, resultSet.getString("status"));
            result.put(8, resultSet.getString("last_update"));
            allRows.add(result);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return allRows;
}

此处,对于来自数据库的每个提取的记录,列都放入Map,每个地图代表一行,添加到List

答案 2 :(得分:1)

此错误是由于在创建表时您可能将name列提到为INTEGER所致。因此,在检索时将使用getInt()方法获得该列。但实际上,该列的类型为STRING。

U应将name列的数据类型更改为STRING,此问题将自动得到解决。

答案 3 :(得分:0)

如果您使用JPA注释将实体字段映射到db表,则需要在ENUM类型实体字段上使用@Enumerated(EnumType.STRING)注释。

答案 4 :(得分:0)

根据文档,getInt()getString()要求columnIndex或columnLabel才能获取特定条目中该列的值。要查找列索引,您需要使用findColumn(name)方法来获取其columnIndex。因此,您的代码应如下所示:

while (resultSet.next()) {
                result.put(resultSet.findColumn("id"), resultSet.getInt("id"));
                result.put(resultSet.findColumn("name"), resultSet.getString("name"));
                result.put(resultSet.findColumn("email"), resultSet.getString("email"));
                result.put(resultSet.findColumn("mobile"), resultSet.getInt("mobile"));
                result.put(resultSet.findColumn("password"), resultSet.getString("password"));
                result.put(resultSet.findColumn("role"), resultSet.getString("role"));
                result.put(resultSet.findColumn("status"), resultSet.getInt("status"));
                result.put(resultSet.findColumn("last_update"),
                        resultSet.getString("last_update"));
            }

基本上,您需要根据将其保存在数据库中的数据类型来获取值。按住Ctrl键并单击ResultSet类,检查可用的方法来检索值

答案 5 :(得分:-1)

请检查语法错误,以防在选择语句之前添加了逗号(,)。

示例:

select id, name, mobile, address from table_name;

但您正在使用类似

select id, name, mobile, address, from table_name;

请检查并更正