Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. <%@page import="java.sql.*"%>
  2. <% Class.forName("com.mysql.jdbc.Driver"); %>
  3. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Selecting Data from a db</title>
  9. </head>
  10. <body>
  11. <h1>selecting data from a database</h1>
  12. <%!
  13. public class Actor
  14. {
  15. String URL ="jdbc:mysql://localhost:3306/sakila";
  16. String USERNAME = "root";
  17. String PASSWORD = "root";
  18.  
  19. Connection connection = null;
  20. PreparedStatement selectActors = null;
  21. ResultSet resultSet = null;
  22.  
  23. public Actor()
  24. {
  25. try
  26. {
  27. connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
  28.  
  29. selectActors = connection.prepareStatement("SELECT actor_id, first_name, last_name from actor WHERE first_name ='PENELOPE'");
  30. }
  31. catch(SQLException e)
  32. {
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. public ResultSet getActors()
  38. {
  39. try
  40. {
  41. resultSet = selectActors.executeQuery();
  42. }
  43. catch(SQLException e)
  44. {
  45. e.printStackTrace();
  46. }
  47. return resultSet;
  48. }
  49. }
  50.  
  51. %>
  52. <%
  53. Actor actor = new Actor();
  54. ResultSet actors = actor.getActors();
  55. %>
  56.  
  57. <table border="1" width="50" cellspacing="5" cellpadding="5" style="border-collapse: collapse;">
  58.  
  59. <tbody>
  60. <tr>
  61. <td>Actor Id</td>
  62. <td>First Name</td>
  63. <td>Last Name</td>
  64. </tr>
  65. <%while(actors.next()){%>
  66. <tr>
  67. <td><% actors.getInt("actor_id"); %></td>
  68. <td><% actors.getString("first_name"); %></td>
  69. <td><% actors.getString("last_name"); %></td>
  70. </tr>
  71. <% } %>
  72. </tbody>
  73. </table>
  74. </body>
  75. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement