【问题标题】:Joining database values and add to an array using Java使用 Java 连接数据库值并添加到数组
【发布时间】:2017-11-08 07:36:58
【问题描述】:

这是我在 netbeans 中的 web 服务 java 项目的一部分。 我不知道如何从我的数据库中检索 两个值(描述 + 文化 - 这些是),将它们添加数组,然后检索它们强>稍后。 当只检索 一个 值时,我知道如何做到这一点。

   public String countries(@WebParam(name = "name") String name) {
            //TODO write your implementation code here
            try {
                String url = "jdbc:odbc:" + "worldcup"; 
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                Connection con = DriverManager.getConnection(url,"","");

                // Gets a statement
                Statement state1 = con.createStatement();
                //Statement state2 = con.createStatement();

                String query1 = "SELECT description,culture FROM Countries WHERE name = '" + name + "'";
                // selects the description for the selected group ( group will be referenced to the chosen group)
                ResultSet results = state1.executeQuery(query1);
                int i = 0;

                Arrays.fill(names, "");
                while (results.next()) {
                    String nam = results.getString("description"); // <---- this is the part i need help from
                    names[i++] = nam;
                }   

                return Arrays.toString(names);

            } catch (SQLException e){

                e.printStackTrace();
            } catch (ClassNotFoundException e) {

            }
            return null;

【问题讨论】:

  • String nam = results.getString("description") + " "+results,getString("culture");
  • 谢谢!!有道理,我不知道如何组合它们。
  • 我会创建 Country 类的对象。对于 ResultSet 中的每一行。

标签: java arrays database resultset


【解决方案1】:

您可以通过列名或列索引访问该值。

按列名访问。

names[i++] = results.getString("description");

names[i++] = results.getString("culture");

按列索引访问。

names[i++] = results.getString(1);

names[i++] = results.getString(2);

【讨论】:

    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多