在service()方法中连接数据库获取表信息

  • 代码:
     package com.shige.controller;

     import javax.servlet.*;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.*; public class ListEmpServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException { } @Override
public ServletConfig getServletConfig() {
return null;
} @Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //防止乱码
//response.setCharacterEncoding("text/html;charset=UTF-8");
response.setContentType("text/html;charset=utf-8"); //创建字符输出流
PrintWriter printWriter=response.getWriter(); //HTML代码
printWriter.print(" <!DOCTYPE html>");
printWriter.print("<meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/> ");
printWriter.print(" <head>");
printWriter.print(" <meta charset='UTF-8'>");
printWriter.print(" <title>员工信息</title>");
printWriter.print(" </head>");
printWriter.print(" <body>");
printWriter.print(" <h3 align='center'>员工信息表</h3>");
printWriter.print(" <hr width='60%'>");
printWriter.print(" <table border='1' align='center' width='50%'>");
printWriter.print(" <tr align='center'>");
printWriter.print(" <th>序号</th>");
printWriter.print(" <th>员工编号</th>");
printWriter.print(" <th>员工姓名</th>");
printWriter.print(" <th>员工薪酬</th>");
printWriter.print(" <th>员工岗位</th>");
printWriter.print(" </tr>"); //JDBC连接数据库
//创建连接对象
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null; try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver"); //获取连接
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/employ?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",
"root","123456"); //获取预编译对象
String sql="select empno,ename,sal,job from emp";
preparedStatement=connection.prepareStatement(sql); //执行SQL语句
resultSet=preparedStatement.executeQuery(); //处理查询结果集
int i=1;
while(resultSet.next()){
String empno=resultSet.getString("empno");
String name=resultSet.getString("ename");
String sal=resultSet.getString("sal");
String job=resultSet.getString("job"); printWriter.print(" <tr align='center'>");
printWriter.print(" <th>"+(i++)+"</th>");
printWriter.print(" <th>"+empno+"</th>");
printWriter.print(" <th>"+name+"</th>");
printWriter.print(" <th>"+sal+"</th>");
printWriter.print(" <th>"+job+"</th>");
printWriter.print(" </tr>"); } } catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
printWriter.print(" </table>");
printWriter.print(" </body>");
printWriter.print(" </html>"); } @Override
public String getServletInfo() {
return null;
} @Override
public void destroy() { }
}

JAVAEE_Servlet_04_在service()方法中连接数据库获取表信息的更多相关文章

  1. Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?

    在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...

  2. ORACLE获取表信息方法

    获取表: select table_name from user_tables; //当前用户的表 select table_name from all_tables; //所有用户的表 select ...

  3. SpringBoot项目中,获取配置文件信息

    1.在配置文件中设置信息,格式如下 wechat: mpAppId: wxdf2b09f280e6e6e2 mpAppSecret: f924b2e9f140ac98f9cb5317a8951c71 ...

  4. mysql中 show table status 获取表信息

    用法 mysql>show table status; mysql>show table status like 'esf_seller_history'\G; mysql>show ...

  5. SSH整合中为获取表单对象Action类实现的接口及拦截器配置

    保存员工或者用户信息时,以员工为例,是通过表单收集信息的,需要把这些信息赋给一个对象,然后保存到数据库中.对应的Action类须实现Preparable接口及ModelDriven接口,且在Actio ...

  6. Android在onCreate()方法中动态获取TextView控件的高度

    正好朋友项目里遇到了给写了个小Demo: 这个监听器看名字也知道了.就是在绘画完毕之前调用的,在这里面能够获取到行数.当然也能够获取到宽高等信息 package com.example.textvie ...

  7. 在html中如何获取表单提交的数据

    a.html: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...

  8. 获取表信息(MSSQL)

    涉及到的系统表汇总 sys.databases sys.objects sys.indexes sys.tables sys.columns sys.data_spaces sys.partition ...

  9. mybatis获取表信息,以及遍历ResultSet

    @RunWith(SpringRunner.class) @SpringBootTest public class BravolinksCrmServerApplicationTests { @Aut ...

随机推荐

  1. vscode 配置表

    { "git.ignoreMissingGitWarning": true, "editor.multiCursorModifier": "ctrlC ...

  2. MongoDB 在Node中的应用

    转: MongoDB 在Node中的应用 文章目录 一 .什么是 MongoDB? 二.小Demo 三.Demo 增删改查 3.1 新增 3.2 查询 3.2.1 查询所有 [{},{}] 找不到返回 ...

  3. RichTextBox FlowDocument类型操作

      最近研究微信项目,套着web版微信协议做了一个客户端,整体WPF项目MVVM架构及基本代码参考于:http://www.cnblogs.com/ZXdeveloper/archive/2016/1 ...

  4. Java 集合框架 03

    集合框架·HashSet 和 TreeSet HashSet存储字符串并遍历 * A:Set集合概述及特点 * 通过API查看即可 * 无索引,不可以重复,无序 * B:案例演示 * HashSet存 ...

  5. SVHN数据集 Format1 剪裁版

    SVHN数据集官网:http://ufldl.stanford.edu/housenumbers/ SVHN数据集官方提供的有两种格式 Format1是那种在街上拍的照片,每张照片的尺寸都不同,然后l ...

  6. 实话实说:只会.NET,会让我们一直处于鄙视链、食物链的下游

    金三银四,是个躁动的季节. 结合最近的面试,谈一谈一个老牌开发人员的面试感悟. 大家都知道我的主力技术栈是 .NET + Devops + 弱前端 (当前技术认知,不排除以后变化). 面试了大小厂,有 ...

  7. Python编程中可能经常用到的函数

    1.os.walk() 一般用法为 import os ph=r'D:\temp\build' for root,dirs,files in os.walk(ph): print(root,dirs, ...

  8. POJ_2387 Til the Cows Come Hom 【最短路】

    一.题目 POJ2387 二.分析 Bellman-Ford算法 该算法是求单源最短路的,核心思想就是不断去更新到起点的最短距离,更新的前提是没有负边.如果有负边需要手动控制循环次数. Dijkstr ...

  9. SQL注入与参数化查询

    SQL注入的本质 SQL注入的实质就是通过SQL拼接字符串追加命令,导致SQL的语义发生了变化.为什么发生了改变呢? 因为没有重用以前的执行计划,而是对注入后的SQL语句重新编译,然后重新执行了语法解 ...

  10. MySQL在线DDL工具 gh-ost

    一.简介 gh-ost基于 golang 语言,是 github 开源的一个 DDL 工具,是 GitHub's Online Schema Transmogrifier/Transfigurator ...