Retrieving All Rows from a Database Table : Select Query « Database SQL JDBC « Java

Retrieving All Rows from a Database Table

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";

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

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


Related examples in the same category

1.Executes SQL queries with pluggable strategies for handling ResultSets. This class is thread safe.
2.This program demonstrates several complex database queries.