有时候,这里没有自动创建项目,这时就需要我们手动去创建(已创建请跳过)
手动创建如下:
然后点ok
说明:此处的/test 是项目在 服务器的 路径即:localhost:8080/test
就是我们服务器主页的路径
然后点ok
然后我们开始码代码
mysql jar包
EL jar包
JSTL jar包
然后创建几个包,分别是dao、model、servlet、test
由于太刺眼了 故换成了黑色主题…
创建了一个BaseDao类
package dao;
import java.sql.*;
public class BaseDao {
public Connection getCN8() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila" +
"?useSSL=false&useUnicode=true&characterEncoding=utf-8&server Timezone=UTC", "root", "123456");
}
public void closeAll(Connection cn, Statement st, ResultSet rs) {
if (cn != null) {
try {
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
然后创建了一个test类
package test;
import dao.BaseDao;
import java.sql.Connection;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
BaseDao b = new BaseDao();
Connection cn = null;
try {
cn = b.getCN8();
if (cn != null) {
System.out.println("yes");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
b.closeAll(cn, null, null);
}
}
}
应该是 server ,而不是service
该过来以后就正确了
然后我们把sakila数据库中film表中的数据查出来
然后依次创建构造方法、get、set 、equals、hashcode、toStirng方法
打开这个菜单的快捷键是 Alte + Insert
Film类
package model;
import java.io.Serializable;
import java.util.Objects;
public class Film implements Serializable {
private String filmId,title,description,releaseYear,languageId,originalLanguageId,rentalDuration,rentalRate
,length,replacementCost,rating,specialFeatures,lastUpdate;
public Film() {
}
public Film(String title, String description, String releaseYear, String languageId, String originalLanguageId, String rentalDuration, String rentalRate, String length, String replacementCost, String rating, String specialFeatures, String lastUpdate) {
this.title = title;
this.description = description;
this.releaseYear = releaseYear;
this.languageId = languageId;
this.originalLanguageId = originalLanguageId;
this.rentalDuration = rentalDuration;
this.rentalRate = rentalRate;
this.length = length;
this.replacementCost = replacementCost;
this.rating = rating;
this.specialFeatures = specialFeatures;
this.lastUpdate = lastUpdate;
}
public Film(String filmId, String title, String description, String releaseYear, String languageId, String originalLanguageId, String rentalDuration, String rentalRate, String length, String replacementCost, String rating, String specialFeatures, String lastUpdate) {
this.filmId = filmId;
this.title = title;
this.description = description;
this.releaseYear = releaseYear;
this.languageId = languageId;
this.originalLanguageId = originalLanguageId;
this.rentalDuration = rentalDuration;
this.rentalRate = rentalRate;
this.length = length;
this.replacementCost = replacementCost;
this.rating = rating;
this.specialFeatures = specialFeatures;
this.lastUpdate = lastUpdate;
}
public String getFilmId() {
return filmId;
}
public Film setFilmId(String filmId) {
this.filmId = filmId;
return this;
}
public String getTitle() {
return title;
}
public Film setTitle(String title) {
this.title = title;
return this;
}
public String getDescription() {
return description;
}
public Film setDescription(String description) {
this.description = description;
return this;
}
public String getReleaseYear() {
return releaseYear;
}
public Film setReleaseYear(String releaseYear) {
this.releaseYear = releaseYear;
return this;
}
public String getLanguageId() {
return languageId;
}
public Film setLanguageId(String languageId) {
this.languageId = languageId;
return this;
}
public String getOriginalLanguageId() {
return originalLanguageId;
}
public Film setOriginalLanguageId(String originalLanguageId) {
this.originalLanguageId = originalLanguageId;
return this;
}
public String getRentalDuration() {
return rentalDuration;
}
public Film setRentalDuration(String rentalDuration) {
this.rentalDuration = rentalDuration;
return this;
}
public String getRentalRate() {
return rentalRate;
}
public Film setRentalRate(String rentalRate) {
this.rentalRate = rentalRate;
return this;
}
public String getLength() {
return length;
}
public Film setLength(String length) {
this.length = length;
return this;
}
public String getReplacementCost() {
return replacementCost;
}
public Film setReplacementCost(String replacementCost) {
this.replacementCost = replacementCost;
return this;
}
public String getRating() {
return rating;
}
public Film setRating(String rating) {
this.rating = rating;
return this;
}
public String getSpecialFeatures() {
return specialFeatures;
}
public Film setSpecialFeatures(String specialFeatures) {
this.specialFeatures = specialFeatures;
return this;
}
public String getLastUpdate() {
return lastUpdate;
}
public Film setLastUpdate(String lastUpdate) {
this.lastUpdate = lastUpdate;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Film)) return false;
Film film = (Film) o;
return Objects.equals(getFilmId(), film.getFilmId()) &&
Objects.equals(getTitle(), film.getTitle()) &&
Objects.equals(getDescription(), film.getDescription()) &&
Objects.equals(getReleaseYear(), film.getReleaseYear()) &&
Objects.equals(getLanguageId(), film.getLanguageId()) &&
Objects.equals(getOriginalLanguageId(), film.getOriginalLanguageId()) &&
Objects.equals(getRentalDuration(), film.getRentalDuration()) &&
Objects.equals(getRentalRate(), film.getRentalRate()) &&
Objects.equals(getLength(), film.getLength()) &&
Objects.equals(getReplacementCost(), film.getReplacementCost()) &&
Objects.equals(getRating(), film.getRating()) &&
Objects.equals(getSpecialFeatures(), film.getSpecialFeatures()) &&
Objects.equals(getLastUpdate(), film.getLastUpdate());
}
@Override
public int hashCode() {
return Objects.hash(getFilmId(), getTitle(), getDescription(), getReleaseYear(), getLanguageId(), getOriginalLanguageId(), getRentalDuration(), getRentalRate(), getLength(), getReplacementCost(), getRating(), getSpecialFeatures(), getLastUpdate());
}
@Override
public String toString() {
return "" + filmId +
" " + title +
" " + description +
" " + releaseYear +
" " + languageId +
" " + originalLanguageId +
" " + rentalDuration +
" " + rentalRate +
" " + length +
" " + replacementCost +
" " + rating +
" " + specialFeatures +
" " + lastUpdate +
" ";
}
}
package dao.impl;
import dao.BaseDao;
import dao.IFilmDao;
import model.Film;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class FilmImpl extends BaseDao implements IFilmDao {
@Override
public List selectAllFilm() {
String sql = "select * from film;";
ArrayList arrayList = new ArrayList<>();
Film f = null;
Connection cn = null;
Statement st = null;
ResultSet rs = null;
try {
cn = getCN8();
st = cn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
f = new Film(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)
, rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10)
, rs.getString(11), rs.getString(12), rs.getString(13));
arrayList.add(f);
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
closeAll(cn, st, rs);
}
return arrayList;
}
}
然后我们测试一下
然后我们可以看到,1000条数据已经出来了
接下来就是显示到网页上的事了
package servlet;
import dao.impl.FilmImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/showfilm")
public class ShowFilmServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().setAttribute("list",new FilmImpl().selectAllFilm());
resp.sendRedirect("index.jsp");
}
}
然后把bootstrap也丢进来
网页:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/12/8/008
Time: 下午 2:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
显示
电影编号
电影名称
简介
发布年份
语言
原始语言
租赁期限
租金率
长度
重置成功
评级
特殊功能
最后一次更新
${f.toString()}
在浏览器中输入:http://localhost:8080/test/showfilm
就可以看到jsp页面了