java如何从数据库中检索数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19514881/
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-08-12 17:56:01  来源:igfitidea点击:

java how retrieve data from database

javasql-serverobjectjdbcreturn

提问by babboon

I have code

我有代码

public static String getData(String query) {
        String output = "";
        try {
            String connectionUrl = "jdbc:sqlserver://localhost:1234;databaseName=123;user=123;password=123";
            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                con = DriverManager.getConnection(connectionUrl);
                String SQL = "select smth from tableName where smth";
                stmt = con.createStatement();
                rs = stmt.executeQuery(query);
                while (rs.next()) {
                    output =  (String) rs.getObject(1);
                }
                rs.close();
        }
        catch (Exception e) {

            return "ERROR while retrieving data: " + e.getMessage();
        }
        return output;
    }

It works if value is string. But if it integer? Or boolean? How to modify this method so it would be universal, no matter what type data I get I still return it as string?

如果值是字符串,它会起作用。但如果是整数呢?还是布尔值?如何修改此方法使其通用,无论我得到什么类型的数据,我仍然将它作为字符串返回?

采纳答案by Ashu Phaugat

First retreive the result in ResultSet rs, then you can write the code like below.

首先在ResultSet rs中检索结果,然后您可以编写如下代码。

You can check the instance of the object and than assign the value.

您可以检查对象的实例,然后分配值。



String str;

Object obj = (Object)rs.getObject(1);

if(obj instanceof String){
   //do you work here for string like below
     str=(String)obj;
}
else if (obj instanceof Integer){
   //do your work for Integer
}

// same you can do for other types

// 你可以对其他类型做同样的事情



回答by SpringLearner

in this line

在这一行

output =  (String) rs.getObject(1);

if stringthen use

如果字符串然后使用

output =   rs.getString(1);

if int

如果是整数

output =   rs.getInt(1);

click oraclefor more info

单击oracle了解更多信息

回答by bsmk

what about toString()?

toString() 呢?

while (rs.next()) {
  output = rs.getObject(1).toString();
}

回答by Thihara

You can't accurately do that without using ResultSetMetaDataclass to get the column type.

如果不使用ResultSetMetaData类来获取列类型,就无法准确地做到这一点。

Get the column data according to the type of the column.

根据列的类型获取列数据。

回答by Keerthivasan

You are getting the value from the resultset presuming that it is always a String and trying to typecast the Object instance. You should make use of the retrieve methods based on the type. Most of the cases, we will be knowing the datatype of the column values from which we retried the data. You can write the program based on the column's type. that's why ResultSet API has a method for each datatype.

您从结果集中获取值,假设它始终是一个字符串并尝试对 Object 实例进行类型转换。您应该根据类型使用检索方法。大多数情况下,我们将知道从中重试数据的列值的数据类型。您可以根据列的类型编写程序。这就是 ResultSet API 为每种数据类型都有一个方法的原因。

For String

rs.getString(1);

For Int

rs.getInt(1)

Please read the documentation of ResultSet

请阅读ResultSet的文档

回答by Java Man

 while (rs.next()) 
 {
       String[] data;
       data = new String[100];
       data[i] = rs.getString("smth");
       i = i + 1;

  }

Try this you got your data in array.. use array instead of object.

试试这个你把你的数据放在数组中..使用数组而不是对象。