6. Exercise
✤ Create a db bkbiet
✤ Create a table bkbiet
✤ id
✤ name
✤ age
✤ email
✤ Use java to connect to db
✤ insert your own data in table
CREATE TABLE `bkbiet`.`bkbiet` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`stream` VARCHAR(45) NOT NULL ,
`email` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) );
String insertTableSQL = "INSERT INTO DBUSER"
! ! + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
! ! + "(?,?,?,?)";
PreparedStatement preparedStatement =
dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setInt(1, 11);
preparedStatement.setString(2, "mkyong");
preparedStatement.setString(3, "system");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
// execute insert SQL stetement
preparedStatement .executeUpdate();
Thursday, 13 June 13
7. Templates
✤ Difficult to write long html pages
✤ requires templates
✤ Enter JSP
✤ Servlets feed data(model) to jsp
✤ converts to html pages
✤ JSP get converted to servlets at runtime
Thursday, 13 June 13
8. MVC Recall
✤ Model-View-Controller
✤ separate the data from the view
✤ Servlet: Controller
✤ DAO: Data Access Object
✤ View: JSP
Thursday, 13 June 13
9. Exercise
✤ Create a web site for users
✤ enter their details into the db
✤ create a page to view all details
✤ search for a user
<HTML>
<HEAD>
<TITLE>A Sample Form Using GET</TITLE>
</HEAD>
<BODY>
<FORM ACTION="http://localhost:8080/bkbiet/
BKBIETServlet" METHOD=”POST”>
<CENTER>
Name:
<INPUT TYPE="TEXT" NAME="name"
VALUE="Joe"><BR>
Last name:
<INPUT TYPE="TEXT" NAME="age" VALUE="26"><BR>
<INPUT TYPE="email" NAME="email"
VALUE="abc@example.com"><P>
<INPUT TYPE="SUBMIT">
<!-- Press this button to submit form -->
</CENTER>
</FORM>
</BODY>
</HTML>
Thursday, 13 June 13