java - 如何只使用一种方法返回两个不同的值?

标签 java mysql

<分区>

如何在 MySQL 表中分别返回两种不同类型的列(int、string)?我试过使用这段代码,但我想不通。好心的,你能告诉我哪里出了问题吗?

请注意:::我想返回 keyid_col 和 key_col

这是我的代码:::

 public  void retrieve_keyword () throws SQLException{

    try {
        ResultSet results = statement.executeQuery
                ("SELECT keyword_id,key_words  FROM keywordlist");  
        try {

            while ( results.next() ) {
                int numColumns =
                        results.getMetaData().getColumnCount();
                int keyid_col = results.getInt("keyword_id");
                String key_col = results.getString("key_words");

                if (numColumns == 2) {
                    System.out.println(results.getObject(numColumns));
                }

                for ( int i = 1 ; i <= numColumns ; i++ ) {
                    System.out.println( "COLUMN " + i + " = " + results.getObject(i) );
                }        
            }
        } 

        catch(Exception ew){
            System.out.println( ew );
        }
    }catch(Exception ew){
        System.out.println( ew );
    }
}

最佳答案

Java 不支持开箱即用。您需要创建一个数据结构并使用它。例如:

 public class retType {
     public retType( int val1, string val2 ) {
           this.val1 = val1; 
           this.val2 = val2;
     }
     public int val1;
     public string val2;
 }

 public retType someFunc()
 {
     int i = 1;
     string j = "example";
     return new retType(i, j);
 }

关于java - 如何只使用一种方法返回两个不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16617158/

相关文章:

java - 从类名创建列表或按列表转换

java - 使用 Datastax 驱动程序 3.6 的 Cassandra 分页 : Null paging state and fetch size not honoured

php - Laravel 5.2 如何在同一行显示成员列表和他/她的组(一对多(或多对多)数据显示)

mysql - 当有两个 max (XXX) 时,SQL 只选择一行

php - php中csv文件中的SQL Select语句

sql - 我怎样才能使用mysql查询删除表中的第n行

java - 应用程序或 Activity 有时需要时间来加载

java - 错误 : Program type already present: com. rom4ek.arcnavigationview.R

c# - 尝试从非常大的数据中从mysql中的多个相关表检索数据

java - 在 (*) 中可以访问多少个不同版本的 'x'?