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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17543440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:16:28  来源:igfitidea点击:

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

java

提问by atikul hussain

I am not able to retrieve column values from my database table with the following coding a message has been displayed in the console:

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

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

My code is:

我的代码是:

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

My db table schema is:

我的数据库表架构是:

id  name    email   mobile  password    role    status  last_update
1   Glomindz Support    [email protected]    9854087006  cbf91a71c11d5ec348b0c7e9b2f0055e    admin   1   2013-05-02 22:05:14
2   Amarjyoti Das   [email protected]    9864092598  88f2dccb02b2a20615211e5492f85204    admin   1   2013-04-26 05:44:41

回答by Kevin Bowersox

You retrieve every column as an intfor the key. I assume that some of these columns represent Strings or Dates.

您检索每一列作为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
}

The inconsistencies between the data types and the object/value returned by the getInt()method causes the error. I would suggest building/modeling an object in your domain that stores rows from the table. Something like:

数据类型和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 */
}

Then build a Map containing the object as the value and the idas the key:

然后构建一个包含对象作为值和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);

        }

回答by SudoRahul

I think you need all your data from the table as a Map. But you may have multiple rows in your DB, therefore you basically want a list of maps! Modify your method to something like this:-

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

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

Here, for every fetched record from the DB, the columns are put into the Map, and each map, represents a row, which is added to a List!

在这里,对于从数据库中提取的每条记录,列都被放入Map,每个映射代表一行,该行被添加到List!

回答by Preethiga

This error is due to, while creating the table you may mentioned namecolumn as INTEGER. So while retrieving it will get that column using getInt()method. But actually the column is of type STRING.

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

U should change the datatype of namecolumn to STRING and this issue will be fixed automatically.

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

回答by nitinkumarp

According to documentation, getInt()or getString()requires columnIndex or columnLabel to get value for that column in particular entry. To find the column index you need to use findColumn(name)method to get its columnIndex. So your code should be as follows:

根据文档,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"));
            }

Basically you need to get your values according to the datatype with which it had been saved in DB. Check available methods to retrieve values by Ctrl+Click on ResultSet class

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

回答by Hitesh

If you are using JPA annotations to map entity fields with db table, you need to use @Enumerated(EnumType.STRING)annotation on ENUM type entity fields.

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

回答by Kalaivani

Please check the syntax error for a small mistake for comma(,) you are added before the select statement

请检查语法错误你在select语句之前添加的逗号(,)的一个小错误

Example:

例子:

select id, name, mobile, address from table_name;

but you are using like

但你正在使用像

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

please check and correct it

请检查并更正