在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. Guava - LoadingCache实现Java本地缓存

    前言 Guava是Google开源出来的一套工具库.其中提供的cache模块非常方便,是一种与ConcurrentMap相似的缓存Map. 官方地址:https://github.com/google ...

  2. Docker的镜像操作

    Docker镜像操作 一.查看镜像 docker images # 查看本地镜像 二.搜索镜像 docker search 镜像名字 docker search centos 三.下载(拉取)镜像 自 ...

  3. Linux系列 -- XShell破解版安装教程

    目录 一.xshell6商业版安装教程 1. 为什么要用xshell 2. 打开Keygen软件获取注册码 3.安装Xmanager_PowerSuite软件 4.打开康康. 二.XShell远程连接 ...

  4. go 动态数组 二维动态数组

    go使用动态数组还有点麻烦,比python麻烦一点,需要先定义. 动态数组申明 var dynaArr []string 动态数组添加成员 dynaArr = append(dynaArr, &quo ...

  5. PAT-1144(The Missing Number)set的使用,简单题

    The Missing Number PAT-1144 #include<iostream> #include<cstring> #include<string> ...

  6. HDR(高动态范围)

    一: 简介 一般来说,当存储在帧缓冲(Framebuffer)中时,亮度和颜色的值是默认被限制在0.0到1.0之间的. 但是如果我们遇上了一个特定的区域,其中有多个亮光源使这些数值总和超过了1.0,又 ...

  7. C#中委托、匿名函数、Lambda表达式的一些个人理解

    0x01定义一个委托,相当于定义一个可以存储方法的特殊变量类型 下面我们看具体的代码,通过代码更好理解 delegate void IntMethodInvoker(int x); 这行代码就是声明一 ...

  8. go中sync.Once源码解读

    sync.Once 前言 sync.Once的作用 实现原理 总结 sync.Once 前言 本次的代码是基于go version go1.13.15 darwin/amd64 sync.Once的作 ...

  9. VUE中的子父组件、兄弟组件之间相互传值,相互调用彼此的方法

    vue--组件传值 父组件传值给子组件--"props" 一.父组件--示例 <template> <child :choose-data="choos ...

  10. iOS 面试秘籍全套

    栏目将持续更新--请iOS的小伙伴关注!   (答案不唯一,仅供参考,文章最后有福利) iOS面试题大全(上) iOS面试题大全(下) 目录: iOS面试题:Run Loop iOS面试题:性能优化 ...