package helloworld;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*/
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* Default constructor.
*/
public HelloWorld() {
// TODO Auto-generated constructor stub } /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn = null;
Statement stmt = null;
ResultSet rs = null; response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter(); out.println("<table border=1>");
out.println("<tr><td>查询结果</td></tr>");
try{
Class.forName("com.mysql.jdbc.Driver"); //lib
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gs?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from user");
while(rs.next()){
out.println("<tr>");
out.println("<td>"+rs.getString("name")+"</td>");
out.println("</tr>");
}
out.println("</table>");
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}
finally{
try{
if(rs !=null){
rs.close();
rs=null;
}
if(stmt != null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
catch(SQLException e){
e.printStackTrace(); }
} } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter();
out.println("<html><head><title>helloworld</title><head><body>hello world!!!</body></html>"); } }

servelet 连接mysql的更多相关文章

  1. nodejs进阶(6)—连接MySQL数据库

    1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...

  2. 【初学python】使用python连接mysql数据查询结果并显示

    因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...

  3. Node.js Express连接mysql完整的登陆注册系统(windows)

    windows学习环境: node 版本: v0.10.35 express版本:4.10.0 mysql版本:5.6.21-log 第一部分:安装node .Express(win8系统 需要&qu ...

  4. PDO连接mysql数据库

    1.PDO简介 PDO(PHP Data Object) 是PHP 5 中加入的东西,是PHP 5新加入的一个重大功能,因为在PHP 5以前的php4/php3都是一堆的数据库扩展来跟各个数据库的连接 ...

  5. 使用ABP EntityFramework连接MySQL数据库

    ASP.NET Boilerplate(简称ABP)是在.Net平台下一个很流行的DDD框架,该框架已经为我们提供了大量的函数,非常方便与搭建企业应用. 关于这个框架的介绍我就不多说,有兴趣的可以参见 ...

  6. jmeter之连接mysql和SQL Server配置

    下载jdbc驱动 在使用jmeter做性能或自动化测试的时候,往往需要直接对数据库施加压力,或者某些参数只能从数据库获取,这时候就必须使用jmeter连接数据库. 1.下载对应的驱动包 mysql驱动 ...

  7. Windows操作系统下远程连接MySQL数据库

    用Eclipse做一个后台项目,但是数据库不想放在本地电脑,于是买了一个腾讯云服务器(学生有优惠,挺便宜的),装上MySQL数据库,但是测试连接的时候,发现总是连接不是上,但是本地数据库可以连接,于是 ...

  8. Hibernate连接mysql数据库的配置

    <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  9. C# 3种方法连接MySql

    转   http://wenku.baidu.com/view/d0cf34708e9951e79b8927c7.html C# 连接MYSQL数据库的方法及示例 连接MYSQL数据库的方法及示例 方 ...

随机推荐

  1. Qemu 有用的链接

    Qemu下载和编译 Download https://en.wikibooks.org/wiki/QEMU/Linux https://en.wikibooks.org/wiki/QEMU/Insta ...

  2. vs2013 编译 notepad++ 源代码

    一.官方网站下载源代码,解压后得到scintilla和powereditor文件夹. 二.安装vs2013.直接打开powereditor\visual.net\notepadplus.vcxproj ...

  3. Java汉字md5值不一致问题

    原文:http://blog.csdn.net/earthhour/article/details/51188437 通过main方法测试得到一个加密值,通过servlet request调用得到一个 ...

  4. 【mybatis】mybatis中insert 主键自增和不自增的插入情况【mysql】

    主键不自增:返回值是插入的条数 <insert id="add" parameterType="EStudent"> insert into TSt ...

  5. oracle解决连接池不足

       select count(*) from v$process;----系统有多少连接数  select value from v$parameter where name = 'processe ...

  6. linux基础-第二十单元_计划任务crond服务

    第二十单元 计划任务crond服务 什么是计划任务:后台运行,到了预定的时间就会自动执行的任务,前提是:事先手动将计划任务设定好.这就用到了crond服务 crond服务相关的软件包[root@MiW ...

  7. install libiconv libraries

    #下载地址: https://ftp.gnu.org/pub/gnu/libiconv/ #编译安装 ./configure --prefix=/usr/local make make install ...

  8. javascript快速入门2--变量,小学生数学与简单的交互

    变量 对于变量的理解:变量是数据的代号.如同人的名字一样. var num;//在JavaScript中使用关键字var声明一个变量 在JavaScript中,使用上面的语法,就可以声明一个变量,以便 ...

  9. 一起來玩鳥 Starling Framework(2)效能測試以及Image與Texture

    上一篇我們放了一個Quad與TextField在舞台上慢慢轉.眼尖的可能會發現轉起來邊緣有點鋸齒,這可以透過設定Starling的反鋸齒來解決,在Main.as裡,新增了_starling之後,可以加 ...

  10. shell报错:未预期的符号***附近有语法错误

    1.安装dos2unix(我的centos命令: yum install dos2unix)2.执行命令:dos2unix  文件名.sh 3.执行命令:bash  -n  文件名.sh (检查语法错 ...