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. PO/VO/BO等对象模型

    PO :persistent object持久对象 1 .有时也被称为Data对象,对应数据库中的entity,可以简单认为一个PO对应数据库中的一条记录. 2 .在hibernate持久化框架中与i ...

  2. “破锣摇滚”乐队(codevs 1444)

    题目描述 Description 你刚刚继承了流行的“破锣摇滚”乐队录制的尚未发表的N(1 <= N <= 20)首歌的版权.你打算从中精选一些歌曲,发行M(1 <= M <= ...

  3. free(): invalid next size (fast/normal)问题

    本文转自 http://blog.sina.com.cn/s/blog_77f1e27f01019qq9.html  ,在此感谢! c++编译常会出现free(): invalid next size ...

  4. Android中mesure过程详解

    我们在编写layout的xml文件时会碰到layout_width和layout_height两个属性,对于这两个属性我们有三种选择:赋值成具体的数值,match_parent或者wrap_conte ...

  5. Set和Map

    Set和Map

  6. spring的IOC和AOP

     spring的IOC和AOP 1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是 ...

  7. css局部概念的理解:

    1.DIV-Padding理解:一直以来对div中的padding属性,一直不理解,使用最多的也就是margin,padding是div的内空间的相对距离,margin是div的外部相对位置,如果用一 ...

  8. Java Hour 16 来个CURD吧!

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 突然想到我最近一直在追的小说,作者每天都会更新两章,而且质量挺高.所以从这篇开 ...

  9. loj 1165(bfs+康托展开)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...

  10. 【shiro】一、基础概念

    来源:http://blog.csdn.net/swingpyzf/article/details/46342023/ &&&& http://jinnianshilo ...