java - 使用 Java 打印 MySQL 表值

标签 java mysql eclipse

我正在尝试用 Java 打印数据库中的表值。我已成功创建连接并且也能够创建表。但我在打印表值时遇到问题。

尝试

        {

            Statement stmt = conn.createStatement();

            ResultSet rs;
            String getValues =
                    "SELECT * " + 
                    "FROM EMPLOYEE" /*+ this.tableName*/ + " ; ";
                    //this.executeUpdate(conn, getValues);

            rs = stmt.executeQuery(getValues);

            String printValues = rs.getString(???Want to print all the table values);


            System.out.println(printValues);


            System.out.println("Values Retrived");
        }
        catch (SQLException e)
        {
            System.out.println("ERROR: Could not get values from table");
            e.printStackTrace();
            return;
        }
    }

最佳答案

您将需要使用 while 循环来循环结果集。下面的例子:

    public static ObservableList<Customer> search_ForDropDown(String searchQuery){
    MysqlDataSource dataSource = CurrentServer.getDataSource();
    ObservableList<Customer> data = FXCollections.observableArrayList();
    data.clear();

    try {
        String query = "SELECT * FROM CUSTOMER LIMIT 5";

        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);     

        while (rs.next()) {
            Customer customer = new Customer();
            customer.setID(rs.getInt("id"));
            customer.setPhoneNumber(rs.getString("phoneNumber"));
            customer.setEmailAddress(rs.getString("emailAddress"));

            data.add(customer);
        }

        rs.close();
        stmt.close();
        conn.close();

    } catch (SQLException e) {
        e.printStackTrace();
    }       
    return data;
}

关于java - 使用 Java 打印 MySQL 表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062879/

相关文章:

java - 首先获取值,然后在 onCreate() 方法中检查 null

java - 为什么 IntentReceiverLeaked?

MySQL 问题 - 警告 : mysql_num_rows() expects parameter 1 to be resource, bool 值给出

python - 将 .csv 文件从 linux 导入到 windows MYSQl

java - 在 Eclipse 中查看 Java 应用程序的日期

java - 不读取两个字输入

java - 双向bean同步

php - 浏览器游戏PHP+MySQL每秒增加资源

java - 如何设置 Eclipse JUnit 插件测试环境

java - 安卓和Java : what is the odd object called `R` ?