Java代码

public class DBHelper {

private String driverName;

private String url;

private String user;

private String password;

private Connection connection;

private String createTableSql;

private String dropTableSql;

public void getConnection() {

if (null == connection) {

try {

Class.forName(driverName);

connection = DriverManager.getConnection(url, user, password);

} catch (ClassNotFoundException | SQLException e) {

e.printStackTrace();

}

}

}

public void closeConnection() {

if (null != connection) {

try {

connection.close();

connection = null; //connection.close won't set the connection to be null

System.out.println("closed db connection.");

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public void createTable() {

getConnection();

Statement stmt = null;

try {

stmt = connection.createStatement();

stmt.execute(createTableSql);

System.out.println("Executed sql [" + createTableSql + "] success.");

} catch (SQLException e) {

e.printStackTrace();

}

closeConnection();

}

public void dropTable() {

getConnection();

Statement stmt = null;

try {

stmt = connection.createStatement();

stmt.execute(dropTableSql);

System.out.println("Executed sql [" + dropTableSql + "] success.");

} catch (SQLException e) {

e.printStackTrace();

}

closeConnection();

}

public String getDriverName() {

return driverName;

}

public void setDriverName(String driverName) {

this.driverName = driverName;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getUser() {

return user;

}

public void setUser(String user) {

this.user = user;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getCreateTableSql() {

return createTableSql;

}

public void setCreateTableSql(String createTableSql) {

this.createTableSql = createTableSql;

}

public String getDropTableSql() {

return dropTableSql;

}

public void setDropTableSql(String dropTableSql) {

this.dropTableSql = dropTableSql;

}

}

  Java代码

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Test connecting mysql db</title>

</head>

<body>

<jsp:useBean id="dbHelper" class="com.jesse.jsp.bean.DBHelper" />

<jsp:setProperty property="driverName" name="dbHelper" value="com.mysql.jdbc.Driver"/>

<jsp:setProperty property="url" name="dbHelper" value="jdbc:mysql://192.168.1.104:3306/db_jesse?useUnicode=true&characterEncoding=GBK"/>

<jsp:setProperty property="user" name="dbHelper" value="jesse"/>

<jsp:setProperty property="password" name="dbHelper" value="jesse"/>

<jsp:setProperty property="createTableSql" name="dbHelper" value="create table test_tbl(id int)"/>

<jsp:setProperty property="dropTableSql" name="dbHelper" value="drop table test_tbl"/>

<%!

private int id = 0;

%>

<%

System.out.println(this); //to check if the servlet is singleton

id++;

synchronized(com.jesse.jsp.bean.DBHelper.class) {

dbHelper.createTable();

System.out.println(id + " created the table.");

Thread.sleep(5000);

dbHelper.dropTable();

System.out.println(id + " dropped the table.");

}

%>

</body>

</html>

  Java代码

org.apache.jsp.pages.mysql_jsp@567e0fb8

Executed sql [create table test_tbl(id int)] success.

closed db connection.

1 created the table.

org.apache.jsp.pages.mysql_jsp@567e0fb8

Executed sql [drop table test_tbl] success.

closed db connection.

2 dropped the table.

Executed sql [create table test_tbl(id int)] success.

closed db connection.

2 created the table.

Executed sql [drop table test_tbl] success.

closed db connection.

2 dropped the table.

  Note:本意是要让log打印出来是哪个session在执行,看到结果...又长知识了 <%! %> 查了下,觉得这种说法有道理,暂不深究: <%%>是代码段,在由jsp转换成Servlet后 <%%>中的代码是放在serive方法中,相当于doGet()和doPost()方法 <%!%>是

jsp声明,用来定义属性和方法的,在由jsp转换成Servlet后 <%!%>中的代码是放serive方法之外的.

技术分享:www.kaige123.com

JSP简单访问数据库的更多相关文章

  1. JSP中访问数据库

    在JSP中访问数据库使用的是JSTL标签,本文不按照http://wiki.jikexueyuan.com/project/jsp/database-access.html此方法进行实践,而是采用之前 ...

  2. javaweb jdbc实现简单的数据库基本操作和servlet的作用域以及jsp标签的使用

    一,工具类,分页类和连接数据库jdbc package com.direct.util; import java.sql.Connection; import java.sql.DriverManag ...

  3. JSP简单实现统计网页访问次数

    JSP简单实现统计网页访问次数 需求:统计网页的访问次数 核心思想:利用application对象,将访问次数的信息放入application对象中,每次访问就+1.这里利用了application对 ...

  4. PHP与JSP简单比较

    比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较php和Java的Web开发.以下就几个主要方面进行的比较: 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上手 ...

  5. 2017.11.12 web中JDBC 方式访问数据库的技术

    JavaWeb------ 第四章 JDBC数据库访问技术 在JavaWeb应用程序中数据库访问是通过Java数据库连接(JavaDateBase Connectivity简称JDBC)数据库的链接一 ...

  6. 使用Spring.net中对Ado.net的抽象封装来访问数据库

    使用Spring.net中对Ado.net的抽象封装来访问数据库     Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入 ...

  7. Spring实战6:利用Spring和JDBC访问数据库

    主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...

  8. 基于JQuery+JSP的无数据库无刷新多人在线聊天室

    JQuery是一款非常强大的javascript插件,本文就针对Ajax前台和JSP后台来实现一个无刷新的多人在线聊天室,该实现的数据全部存储在服务端内存里,没有用到数据库,本文会提供所有源程序,需要 ...

  9. SQL ser 跨实例访问数据库

    SqlServer如何跨实例访问数据库 Exec sp_droplinkedsrvlogin LinkName,NullExec sp_dropserver LinkName go EXEC sp_a ...

随机推荐

  1. Light OJ 1199 - Partitioning Game (博弈sg函数)

    D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  2. Powershell 批量替换文件

    Powershell 批量替换文件 ##作者:Xiongpq ##时间:2015-06-10 18:50 ##版本:2.0 ##源文件目录 ##源文件目录的所有文件都会覆盖目标目录的同名文件,源文件目 ...

  3. ASP.Net和新对象之context.Server

    描述 Server是一个HttpServerUtility类型的对象,不是一个类名 1.获取服务器上的绝对路径文件名tring ss = context.Server.MapPath("~/ ...

  4. python基础——递归函数

    python基础——递归函数 递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用 ...

  5. 【读书笔记】读《JavaScript设计模式》之代理模式

    一.定义 代理是一个对象,它可以用来控制对另一个对象的访问.它与另外那个对象实现了同样的接口,并且会把任何方法调用传递给那个对象.另外那个对象通常称为本体.代理可以代替其实体被实例化,并使其可被远程访 ...

  6. Android Bander设计与实现 - 设计篇

    转自:http://blog.csdn.net/universus/article/details/6211589#t7 Binder Android IPC Linux 内核 驱动 摘要 Binde ...

  7. hdu 2044:一只小蜜蜂...(水题,斐波那契数列)

    一只小蜜蜂... Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepte ...

  8. MATLAB信号与系统分析(二)——离散时间信号与系统的时域分析

    一.离散信号的表示 1.一个离散信号需要用两个向量来表示: (1)离散信号的幅值 (2)离散信号的位置信息 2.用MATLAB实现离散信号的可视化 (1)不能利用符号运算来表示 (2)绘制离散信号一般 ...

  9. SQL Server SA 密码丢失无法连接数据库怎么办?

    如果Windows账户无法连接并且SA密码也丢失了,那么如何可以连接到数据库呢? 答案是: 在单用户模式下启动SQL Server然后用本地管理员权限连接.登陆之后就可以修改SA密码了. 步骤: 1. ...

  10. MySQL 安装 启动命令总结

    MySQL 安装 启动 基本语法概述 MySQL安装和配置 我是直接使用安装包:mysql-installer-community-5.6.10.1.msi 安装的时候其中有几点要注意: 1.记住端口 ...