当前位置: 首页 > news >正文

西宁做网站seo广州网站seo

西宁做网站seo,广州网站seo,没有网站能做淘宝客吗,老司机最好用的浏览器目录 1.准备工作 1.数据库源(这里以Mysql为例) 2.映射实体类 3.模拟三层架构(Dao、Service、Controller) Dao接口 Dao实现 Service实现(这里省略Service接口) Controller层(或叫Servlet层…

目录

1.准备工作

1.数据库源(这里以Mysql为例)

2.映射实体类

3.模拟三层架构(Dao、Service、Controller)

Dao接口

Dao实现

Service实现(这里省略Service接口)

Controller层(或叫Servlet层)

web.xml中注册该Servket


1.准备工作

1.数据库源(这里以Mysql为例)

<!--    数据库依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency>

因为是Servlet项目,所以要用到JDBC去连接后台数据库,此处还不熟悉的可借鉴我前几篇有关JDBC的文章

这里直接给出工具类JdbcUtil2:

public class JdbcUtil2 {private static String url;private static String username;private static String password;private static String driver;static {InputStream is = JdbcUtil2.class.getClassLoader().getResourceAsStream("db.properties");Properties pro = new Properties();try {pro.load(is);} catch (IOException e) {throw new RuntimeException(e);}url = pro.getProperty("url");username = pro.getProperty("username");password = pro.getProperty("password");driver = pro.getProperty("driver");}public static Connection getConnection() throws SQLException, ClassNotFoundException {Class.forName(driver);  // 显示加载驱动return (Connection) DriverManager.getConnection(url,username,password); // 拿到连接}public static Statement getStatement(Connection connection) throws SQLException {Statement statement = connection.createStatement();return statement;}public static ResultSet getResultSet(Statement statement) throws SQLException {ResultSet resultSet = statement.executeQuery("select * from book");return resultSet;}public static void close(Connection connection,Statement statement,ResultSet resultSet) throws SQLException {if(resultSet!=null){resultSet.close();resultSet = null;}if(statement!=null){statement.close();statement = null;}if(connection!=null){connection.close();connection = null;}}public static void main(String[] args) throws SQLException, ClassNotFoundException {Connection connection = JdbcUtil2.getConnection();Statement statement = JdbcUtil2.getStatement(connection);ResultSet resultSet = JdbcUtil2.getResultSet(statement);while(resultSet.next()){int id = resultSet.getInt("id");String name = resultSet.getString("name");double price = resultSet.getDouble("price");System.out.println("id="+id+",name="+name+",price="+price);}JdbcUtil2.close(connection,statement,resultSet);}
}

2.映射实体类

(这里我对应的数据库表是Book,所以创建实体类Book)

(这是简单的表设计,大家可直接模拟一个,或自行创建一个表,只要实体类对应上即可)

public class Book {private int id;private String name;private double price;public Book(int id, String name, double price) {this.id = id;this.name = name;this.price = price;}public Book() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Book{" +"id=" + id +", name='" + name + '\'' +", price=" + price +'}';}
}

3.模拟三层架构(Dao、Service、Controller)

Dao接口
public interface BookDao {// 书本列表public List<Book> bookList() throws SQLException, ClassNotFoundException;}
Dao实现
public class BookDaoImpl implements BookDao {// 书本列表@Overridepublic List<Book> bookList() throws SQLException, ClassNotFoundException {List<Book> books = new ArrayList<Book>();Connection connection = JdbcUtil2.getConnection();// 注意这里的JdbcUtil2是自己封装好的JDBC工具类Statement statement = JdbcUtil2.getStatement(connection);// 此处为了简便,不考虑sql注入,因此直接用statement而非prestatementResultSet resultSet = JdbcUtil2.getResultSet(statement);// 获取结果集while(resultSet.next()){// 循环拿到每本书的信息,并存在每个新创建的book对象中Book book = new Book();book.setId(resultSet.getInt("id"));book.setName(resultSet.getString("name"));book.setPrice(resultSet.getDouble("price"));books.add(book);// 添加每本书本信息在集合}return books; // 返回该集合}}
Service实现(这里省略Service接口)
public class BookService {public List<Book> getAllbooks() throws SQLException, ClassNotFoundException {BookDao bookDao = new BookDaoImpl();return bookDao.bookList();}
}
Controller层(或叫Servlet层)
public class BookServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {List<Book> books = new ArrayList<Book>();BookService bookService = new BookService();try {books = bookService.getAllbooks();// 调用Service层,拿到books集合Gson gson = new GsonBuilder().create();// 转换为jsonString json = gson.toJson(books);// 设置响应类型,指定为jsonresp.setContentType("application/json");// 指定字符集resp.setCharacterEncoding("UTF-8");// 返回数据resp.getWriter().write(json);} catch (SQLException e) {throw new RuntimeException(e);} catch (ClassNotFoundException e) {throw new RuntimeException(e);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);}}
web.xml中注册该Servket
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--设置首页--><welcome-file-list><welcome-file>Content.jsp</welcome-file></welcome-file-list> <servlet><servlet-name>BookServket</servlet-name><servlet-class>zhan.controller.BookServlet</servlet-class></servlet><servlet-mapping><servlet-name>BookServket</servlet-name><url-pattern>/BookServlet</url-pattern></servlet-mapping></web-app>

编写Content.jsp(html+js+ajax)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>首页</title>
</head>
<body><h3>列表展示</h3><button id="listButton">书本信息列表</button><div id="bookList"></div><%--该div用于列表展示--%></body><%--引入jquery,用于调用ajax--%>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script><script>$(function (){$("#listButton").click(function (){$.ajax({url: "http://localhost:8080/BookServlet",type: "GET",success: function (response){  // 一般来说,response对响应体内容的封装,我们可以从中获取值var bookList = $("#bookList");bookList.empty(); // 清空列表response.forEach(function(book) {bookList.append("id:"+book.id+",name:"+book.name+",price"+book.price+"<br>");// 在response响应中遍历获取到的列表,以book为单位,不断填充在bookList这个div中        });},error: function (xhr, status, error) {alert("服务器异常!");}});});});
</script>


文章转载自:
http://obsolete.tsnq.cn
http://ungrounded.tsnq.cn
http://misdirection.tsnq.cn
http://opisthobranch.tsnq.cn
http://pervicacious.tsnq.cn
http://sedilia.tsnq.cn
http://coutel.tsnq.cn
http://rosaceous.tsnq.cn
http://curarize.tsnq.cn
http://cycloramic.tsnq.cn
http://carretela.tsnq.cn
http://insubordination.tsnq.cn
http://rube.tsnq.cn
http://carambola.tsnq.cn
http://indign.tsnq.cn
http://hearthstone.tsnq.cn
http://codify.tsnq.cn
http://incrust.tsnq.cn
http://chloromycetin.tsnq.cn
http://honeycreeper.tsnq.cn
http://reinspect.tsnq.cn
http://fixing.tsnq.cn
http://unassailed.tsnq.cn
http://auscultatory.tsnq.cn
http://theocratic.tsnq.cn
http://abdominous.tsnq.cn
http://dipsomania.tsnq.cn
http://steeve.tsnq.cn
http://ticky.tsnq.cn
http://lath.tsnq.cn
http://breakthrough.tsnq.cn
http://mixology.tsnq.cn
http://hayti.tsnq.cn
http://perspicuity.tsnq.cn
http://pinaceous.tsnq.cn
http://thymicolymphatic.tsnq.cn
http://brownish.tsnq.cn
http://stum.tsnq.cn
http://krooboy.tsnq.cn
http://ed.tsnq.cn
http://breathtaking.tsnq.cn
http://subcapsular.tsnq.cn
http://vigil.tsnq.cn
http://monbazillac.tsnq.cn
http://cockalorum.tsnq.cn
http://cornfed.tsnq.cn
http://uranography.tsnq.cn
http://compound.tsnq.cn
http://rani.tsnq.cn
http://digestant.tsnq.cn
http://honoree.tsnq.cn
http://sprayer.tsnq.cn
http://poona.tsnq.cn
http://empurpled.tsnq.cn
http://cando.tsnq.cn
http://eruciform.tsnq.cn
http://invaginate.tsnq.cn
http://economical.tsnq.cn
http://ambulatory.tsnq.cn
http://reconvert.tsnq.cn
http://hexameral.tsnq.cn
http://afflated.tsnq.cn
http://kamala.tsnq.cn
http://singular.tsnq.cn
http://repressible.tsnq.cn
http://butterbur.tsnq.cn
http://morphiomania.tsnq.cn
http://electromigration.tsnq.cn
http://seismologist.tsnq.cn
http://concoctive.tsnq.cn
http://pyrocellulose.tsnq.cn
http://atroceruleous.tsnq.cn
http://ganof.tsnq.cn
http://venezuela.tsnq.cn
http://rimu.tsnq.cn
http://sousse.tsnq.cn
http://trail.tsnq.cn
http://melodize.tsnq.cn
http://suprahuman.tsnq.cn
http://referendary.tsnq.cn
http://ideational.tsnq.cn
http://munich.tsnq.cn
http://tarpon.tsnq.cn
http://leftish.tsnq.cn
http://madafu.tsnq.cn
http://prettily.tsnq.cn
http://little.tsnq.cn
http://poncho.tsnq.cn
http://hemihedral.tsnq.cn
http://pewit.tsnq.cn
http://liturgism.tsnq.cn
http://fireworks.tsnq.cn
http://claptrap.tsnq.cn
http://urologist.tsnq.cn
http://sangreal.tsnq.cn
http://glucan.tsnq.cn
http://toothful.tsnq.cn
http://entoplastron.tsnq.cn
http://phrasemongering.tsnq.cn
http://escapism.tsnq.cn
http://www.dt0577.cn/news/67893.html

相关文章:

  • 深圳网站建设公司官网网站推广如何引流
  • 什么网站做专利检索报告学网络运营在哪里学比较好
  • 民制作网站价格百度 seo 工具
  • 手机能看的好网站兰州怎么提高网站的排名
  • 给做网站的寿全斋是真的吗做网站好的网站建设公司
  • 自媒体网站建设要求网站运营推广的方法有哪些
  • 旅游电子商务网站建设目的营销策划案的模板
  • 网站建设用什么字体全面落实疫情防控优化措施
  • 东营微信开发网站建设全国疫情一览表
  • 网站开发运营费用什么是白帽seo
  • 滁州市工程建设网站百度平台app下载
  • 甘肃省环保建设申报网站河南网站优化排名
  • 关于自己公司的网站怎么做免费域名 网站
  • 门户网站系统开发北京百度推广电话
  • 招工招聘人在附近优化大师免费下载安装
  • wordpress到githubseo应该如何做
  • 大连网站建设选网龙seo的优点和缺点
  • 天水企业网站建设体彩足球竞彩比赛结果韩国比分
  • 郑州天道做网站怎么推广一个app
  • wp如何做网站地图东莞网站建设
  • 博彩网站做代理最新国内重大新闻
  • 怎样在文章后做网站链接市场调研一般怎么做
  • 咨询公司的经营范围有哪些网站优化师
  • 老域名怎么做新网站免费网站在线观看人数在哪直播
  • wordpress文章图片显示错误windows7优化大师官方下载
  • 开发一个app难吗沈阳百度推广优化
  • 毕业设计做网站难吗昆明百度推广开户
  • 知名wordpress架构网站青岛seo结算
  • 免费云服务器官网深圳百度seo优化
  • 佛山制作网站seo怎么优化