Aim:
Develop a Web Application Using With Handling Resultset and Statements.
Procedure:
- Step 1: Open a New Java Web Application on Netbeans Editor.
- Step 2: Open Netbeans Ide, Choose File -> new Project.
- Step 3: New Open a New Project and Choose a Web Application.
- Step 4: Create a Database in Mysql.
- Step 5: the Give Proper Database Connections Through Web Services.
- Step 6: Run the Project and Give the Data Through the Application to Database.
- Step 7: Save the Project (Ctrl+s).
- Step 8: Run the Project (F5).
Program:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ResultSetExample { public static void main(String[] args) { String username = "myusername"; String password = "mypassword"; String databaseName = "albums"; Connection connect = null; Statement statement = null; try { Class.forName("com.mysql.jdbc.Driver"); connect = DriverManager.getConnection("jdbc:mysql://localhost/" + databaseName + "?" + "user=" + username + "&password=" + password); statement = connect.createStatement(); String query = "SELECT * FROM the_classics ORDER BY year"; ResultSet resultSet = statement.executeQuery(query); while (resultSet.next()) { System.out.println("Printing result..."); String albumName = resultSet.getString("name"); String artist = resultSet.getString("artist"); int year = resultSet.getInt("year"); System.out.println("\tAlbum: " + albumName + ", by Artist: " + artist + ", released in: " + year); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } try { connect.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
Output:
