在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. 微信小程序(二十)-UI组件(Vant Weapp)-01按装配置

    1.官网 https://vant-contrib.gitee.io/vant-weapp/#/intro https://gitee.com/vant-contrib/vant-weapp 2.按装 ...

  2. JVisualVM监控JVM-外网服务器

    环境说明: a:阿里云服务器Centos8.2 b:JDK1.8 1:增加JMV运行参数 java -jar 启动时添加一下参数: -Djava.rmi.server.hostname=外网IP地址 ...

  3. IntelliJ Idea tomcat 控制台输出乱码

    reference: https://blog.csdn.net/dandandeshangni/article/details/485442211. 在运行/调试 配置对话框的Startup/Con ...

  4. JDK的下载、安装与配置

    一.JDK的下载 1.JDK下载地址:https://www.oracle.com/cn/java/technologies/javase-downloads.html 2.登录Oralce官网:ht ...

  5. 215. 数组中的第K个最大元素 + 快速排序 + 大根堆

    215. 数组中的第K个最大元素 LeetCode-215 另一道类似的第k大元素问题:https://www.cnblogs.com/GarrettWale/p/14386862.html 题目详情 ...

  6. 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

    剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 Offer 21 这题的解法其实是考察快慢指针和头尾指针. package com.walegarrett.offer; /** * @Aut ...

  7. Nginx常见的错误配置

    Blog:博客园 个人 翻译自Common Nginx misconfigurations that leave your web server open to attack Nginx是当前主流的W ...

  8. rest framework Views

    基于类的意见 Django的基于类的意见是从旧式的观点颇受欢迎. - Reinout面包车里斯 REST框架提供了一个APIView类,它的子类Django的View类. APIView类是从正规不同 ...

  9. springboot整合mybatis。mapper.xml资源文件放置到resources文件夹下的配置&别名使用配置

  10. BZOJ_2844 albus就是要第一个出场 【线性基】

    一.题目 albus就是要第一个出场 二.分析 非常有助于理解线性基的一题. 构造线性基$B$后,如果$|A| > |B|$,那么就意味着有些数可以由$B$中的数异或出来,而多的数可以取或者不取 ...