4

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

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();
    }
}

我的数据库表架构是:

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
4

6 回答 6

3

您检索每一列作为int键。我假设其中一些列代表Strings 或Dates。

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 */
}

然后构建一个包含对象作为值和id作为键的 Map:

   //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);

        }
于 2013-07-09T08:27:58.600 回答
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!

于 2013-07-09T08:33:31.970 回答
2

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

于 2018-05-10T11:10:54.653 回答
1

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

U 应将name列的数据类型更改为 STRING,此问题将自动修复。

于 2018-10-31T06:23:05.787 回答
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"));
            }

基本上,您需要根据保存在 DB 中的数据类型来获取您的值。通过 Ctrl+单击 ResultSet 类检查检索值的可用方法

于 2018-10-31T06:38:41.970 回答
-1

请检查语法错误,以了解在 select 语句之前添加的 comma(,) 的小错误

例子:

select id, name, mobile, address from table_name;

但你正在使用喜欢

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

请检查并更正

于 2020-04-07T18:06:19.107 回答