Retrieving All Rows from a Database Table : Statement « Database « Java Tutorial






import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver).newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT * FROM  emp");
    while (res.next()) {
      int i = res.getInt("ID");
      String s = res.getString("name");
      System.out.println(i + "\t\t" + s);
    }
    con.close();
  }
}








20.5.Statement
20.5.1.Change the fetch size on the result set
20.5.2.Get the fetch size of a statement
20.5.3.Set the fetch size on the statement
20.5.4.Retrieving All Rows from a Database Table
20.5.5.Getting Column Names from a database table in Java
20.5.6.Retrieve a rowcount from a ResultSet
20.5.7.Setting the Number of Rows to Prefetch When Executing a SQL Query
20.5.8.Create a result set
20.5.9.Creating a Database Table called my_table with one column, col_string, which holds strings.
20.5.10.Deleting a Database Table called my_table from a database.
20.5.11.Inserting a Row into a Database Table
20.5.12.Inserting a Row into a Database Table Using a Prepared Statement
20.5.13.Deleting a Row from a Database Table
20.5.14.Statement Batch Update