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. CodeForces - 417E(随机数)

    Square Table Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  2. Android 如何让EditText不自动获取焦点

    解决之道:在EditText的父级控件中找一个,设置成 android:focusable="true"     android:focusableInTouchMode=&quo ...

  3. show processlist

    mysql> show processlist; #mysql服务器查看有那些主机连进来,并列出它们查什么库 +-----+------+-----------+------+--------- ...

  4. .net学习笔记---IIS 处理模型及ASP.NET页面生命周期

    本文是基于IIS6的处理模型. 当一个客户端页面访问IIS试图获取一些信息的时候,发生了什么事情?一个请求在通过了HTTP管道后又发生了什么?本文主要是描述这两个过程,即IIS处理asp.net请求和 ...

  5. Ubuntu12.04 安装openjdk-8-jdk

    参考文章:http://ubuntuhandbook.org/index.php/2015/01/install-openjdk-8-ubuntu-14-04-12-04-lts/ OpenJDK J ...

  6. Java Hour 28 HashSet

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 28 HashSet 为查找而生 LinkedList查找效率低下, ...

  7. SAE上传web应用(包括使用数据库)教程详解及问题解惑

    转自:http://blog.csdn.net/baiyuliang2013/article/details/24725995 SAE上传web应用(包括使用数据库)教程详解及问题解惑: 最近由于工作 ...

  8. HDU 4974 Dracula and Ethan 优先队列

    Dracula and Ethan Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching competition ...

  9. SQL Server 2005 中实现通用的异步触发器架构

    在SQL Server 2005中,通过新增的Service Broker可以实现异步触发器的处理功能.本文提供一种使用Service Broker实现的通用异步触发器方法. 在本方法中,通过Serv ...

  10. linux根分区扩容

    Linux 根分区扩容 1.fdisk –l  (红线部分为新添加的硬盘) 2.磁盘格式化 3. mkfs.ext3 -T largefile /dev/sde(格式化上面的分区) 4. vgdisp ...