Selecting all rows from a table and creates a result set: : Query ResultSet « Database « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. JSP
22. JSTL
23. Servlet
24. Web Services SOA
25. Email
26. J2ME
27. J2EE Application
28. XML
29. Design Pattern
30. Log
31. Security
32. Apache Common
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Database » Query ResultSet 
20. 5. 7. Selecting all rows from a table and creates a result set:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  public static void main(String[] argsthrows Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
        ResultSet.CONCUR_UPDATABLE);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    while (rs.next()) {
      String name = rs.getString("name");
      System.out.println(name);
    }

    rs.close();
    st.close();
    conn.close();

  }

  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";

    return DriverManager.getConnection(url, "sa""");
  }
}
nameValue
null
Tom
20. 5. Query ResultSet
20. 5. 1. ResultSet's Methods to Access Columns by Index
20. 5. 2. ResultSet's Methods to Access Columns by Name
20. 5. 3. ResultSet: A SQL query returns a ResultSet containing the requested data
20. 5. 4. Get Data from a ResultSet
20. 5. 5. Retrieving the Value of a Column Using the Index Number
20. 5. 6. Retrieving the Value of a Column Using the Column Name
20. 5. 7. Selecting all rows from a table and creates a result set:
20. 5. 8. If you Do Not Know the Name, Position, and Type of Each Column, how to get value from ResultSet
20. 5. 9. Determine If a Fetched Value Is NULL
20. 5. 10. Get int value from ResultSet
20. 5. 11. Get the Number of Rows in a Database Table
20. 5. 12. Limit the Number of Rows Returned from a SQL Query
20. 5. 13. Checking for a Warning Using a ResultSet Object
20. 5. 14. Converting types: DATE to String
w_ww_._j__a_v__a___2s._c___o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.