<html>  <head>    <title></title>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  </head>  <body> ...
<%@pagecontentType="text/html" pageEncoding="UTF-8"%><%@page import="java.sql.*, business.User, javax.servlet.*, java.io.*...
tempUser = (User) it.next();    %>    <tr>      <td>First Name:</td>      <td><%= tempUser.getFirstName() %></td>      <td...
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package business;/** * * @...
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package util;importjava.sq...
private void connectDb(){   try {       conn = DriverManager.getConnection(dbUrl, username, password);   } catch (SQLExcep...
}}
Upcoming SlideShare
Loading in …5
×

Week 12 code

232 views
197 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
232
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Week 12 code

  1. 1. <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form action="index.jsp" method="post"> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> Email Address: <input type="text" name="emailAddress"><br> <input type="submit" Value="Submit"> </form> </body></html>
  2. 2. <%@pagecontentType="text/html" pageEncoding="UTF-8"%><%@page import="java.sql.*, business.User, javax.servlet.*, java.io.*, javax.servlet.http.*, java.sql.*,java.util.*, util.SQLDBUtil" %><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>mySQL</h1> <% User user; try { String dbUrl = "jdbc:mysql://localhost:3306/murach"; String username = "root"; String password = "sesame"; String query = "SELECT * FROM murach.user"; String userName = ""; SQLDBUtilutil = new SQLDBUtil(); ArrayList<User> users = new ArrayList<User>(); //insert a new recordutil.insertUser(request.getParameter("firstName"),request.getParameter("lastName"),request.getParameter("emailAddress")); Connection conn = DriverManager.getConnection(dbUrl, username, password); PreparedStatementps = conn.prepareStatement(query); Statement statement = conn.createStatement(); ResultSetrs = statement.executeQuery(query); while(rs.next()) { user = new User(rs.getString(2),rs.getString(3),rs.getString(4)); users.add(user); //userName = user.getFirstName(); } rs.close(); Iterator it = users.iterator(); User tempUser; %> <table cellpadding="5" cellspacing="5" border =" 1"> <% while(it.hasNext()) {
  3. 3. tempUser = (User) it.next(); %> <tr> <td>First Name:</td> <td><%= tempUser.getFirstName() %></td> <td> Last Name: </td> <td><%= tempUser.getLastName() %></td> <td> Email Address: </td> <td><%= tempUser.getEmailAddress() %></td> <br> </tr> <% } %> </table> <% } catch (SQLException e) { for (Throwable t : e) t.printStackTrace(); } finally { } %> </body></html>
  4. 4. /* * To change this template, choose Tools | Templates * and open the template in the editor. */package business;/** * * @author tpersson */public class User { String firstName, lastName, emailAddress; public User () { } public User (String first, String last, String email) { firstName = first; lastName = last; emailAddress = email; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getEmailAddress() { return emailAddress; }}
  5. 5. /* * To change this template, choose Tools | Templates * and open the template in the editor. */package util;importjava.sql.*;import business.User;import javax.servlet.*;import java.io.*;import javax.servlet.http.*;import java.sql.*;import java.util.*;/** * * @author tpersson */public class SQLDBUtil{ String dbUrl = "jdbc:mysql://localhost:3306/murach"; String username = "root"; String password = "sesame"; String query = ""; //connection objects Connection conn; PreparedStatementps; Statement statement; publicSQLDBUtil() { } public void insertUser(String first, String last, String email) { //query = "INSERT INTO murach.user(,first, last, email)"; query = "INSERT into murach.user (FirstName,LastName,EmailAddress) values (" + first + "," +last + "," + email + ")"; runQuery(query); } public void updateUser(String first, String last, String email) { if(doesExist(email)) { query = "UPDATE murach.user SET FirstName ="+ first+",LastName="+last+",EmailAddress="+email2 +" WHERE EmailAddress=" + email +""; runQuery(query); } else { //out.printLine("No record with that email address"); } }
  6. 6. private void connectDb(){ try { conn = DriverManager.getConnection(dbUrl, username, password); } catch (SQLException e) { } finally { }}private void runQuery(String q) { try { connectDb(); PreparedStatementps = conn.prepareStatement(query); Statement statement = conn.createStatement(); ps.executeUpdate(); conn.close(); } catch (SQLException e) { } finally { }}private booleandoesExist(String email){ Boolean exists = false; ResultSetrsCheck; connectDb(); query="SELECT * FROM murach.User WHERE EmailAddress=" + email + ""; try { ps = conn.prepareStatement(query); statement = conn.createStatement(); rsCheck = statement.executeQuery(query); rsCheck.last(); if(rsCheck.getRow()>0) { exists = true; } } catch(SQLException e) { } finally { return exists; }
  7. 7. }}

×