Java JDBC Statement run database query
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void queryDatabase(Connection conn) { String qry = "select recipe_num, name, description from recipes"; Statement stmt = null;/*from w w w. j a v a2 s . c o m*/ try { stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(qry); while (rs.next()) { String recipe = rs.getString("RECIPE_NUM"); String name = rs.getString("NAME"); String desc = rs.getString("DESCRIPTION"); System.out.println(recipe + "\t" + name + "\t" + desc); } } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { ex.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } } } }