JAVAEE_Servlet_04_在service()方法中连接数据库获取表信息
在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()方法中连接数据库获取表信息的更多相关文章
- Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?
在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...
- ORACLE获取表信息方法
获取表: select table_name from user_tables; //当前用户的表 select table_name from all_tables; //所有用户的表 select ...
- SpringBoot项目中,获取配置文件信息
1.在配置文件中设置信息,格式如下 wechat: mpAppId: wxdf2b09f280e6e6e2 mpAppSecret: f924b2e9f140ac98f9cb5317a8951c71 ...
- mysql中 show table status 获取表信息
用法 mysql>show table status; mysql>show table status like 'esf_seller_history'\G; mysql>show ...
- SSH整合中为获取表单对象Action类实现的接口及拦截器配置
保存员工或者用户信息时,以员工为例,是通过表单收集信息的,需要把这些信息赋给一个对象,然后保存到数据库中.对应的Action类须实现Preparable接口及ModelDriven接口,且在Actio ...
- Android在onCreate()方法中动态获取TextView控件的高度
正好朋友项目里遇到了给写了个小Demo: 这个监听器看名字也知道了.就是在绘画完毕之前调用的,在这里面能够获取到行数.当然也能够获取到宽高等信息 package com.example.textvie ...
- 在html中如何获取表单提交的数据
a.html: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...
- 获取表信息(MSSQL)
涉及到的系统表汇总 sys.databases sys.objects sys.indexes sys.tables sys.columns sys.data_spaces sys.partition ...
- mybatis获取表信息,以及遍历ResultSet
@RunWith(SpringRunner.class) @SpringBootTest public class BravolinksCrmServerApplicationTests { @Aut ...
随机推荐
- Django Admin后台管理功能使用+二次开发
一 使用环境 开发系统: windows IDE: pycharm 数据库: msyql,navicat 编程语言: python3.7 (Windows x86-64 executable in ...
- vue核心---虚拟dom的实现
生成dom的过程 由vue模板生成虚拟dom 虚拟dom转换成真实dom渲染到html页面 代码实现 要实现的真实dom <div id="box"> <p cl ...
- 剑指 Offer 65. 不用加减乘除做加法 + 位运算
剑指 Offer 65. 不用加减乘除做加法 Offer_65 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author WaleGa ...
- 【转载】UML类图中箭头和线条的含义和用法
文章转载自 http://blog.csdn.net/hewei0241/article/details/7674450 https://blog.csdn.net/iamherego/article ...
- go中errgroup源码解读
errgroup 前言 如何使用 实现原理 WithContext Go Wait 错误的使用 总结 errgroup 前言 来看下errgroup的实现 如何使用 func main() { var ...
- ijkplayer接入使用
1.ijkplayer简介 ijkplayer是一个基于FFmpeg的轻量级Android/iOS视频播放器.FFmpeg的是全球领先的多媒体框架,能够解码,编码, 转码,复用,解复用,流,过滤器和播 ...
- TensorFlow学习(1)-初识
初识TensorFlow 一.术语潜知 深度学习:深度学习(deep learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法. 深度学 ...
- HashMap源码阅读(小白的java进阶)
OverView 构造方法 //构造方法 public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < ...
- android分析之Condition
Condition的含义是条件变量,其实现依赖于系统,一般都要配合Mutex使用,使用步骤为:给mutex上锁(Lock),调用wait等待"条件"发生,如果没有发生则re-wai ...
- MySQL优化从执行计划开始(explain超详细)
前言 小伙伴一定遇到过这样反馈:这页面加载数据太慢啦,甚至有的超时了,用户体验极差,需要赶紧优化: 反馈等同于投诉啊,多有几次,估计领导要找你谈话啦. 于是不得不停下手里头的活,赶紧进行排查,最终可能 ...