JSP简单访问数据库
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简单访问数据库的更多相关文章
- JSP中访问数据库
在JSP中访问数据库使用的是JSTL标签,本文不按照http://wiki.jikexueyuan.com/project/jsp/database-access.html此方法进行实践,而是采用之前 ...
- javaweb jdbc实现简单的数据库基本操作和servlet的作用域以及jsp标签的使用
一,工具类,分页类和连接数据库jdbc package com.direct.util; import java.sql.Connection; import java.sql.DriverManag ...
- JSP简单实现统计网页访问次数
JSP简单实现统计网页访问次数 需求:统计网页的访问次数 核心思想:利用application对象,将访问次数的信息放入application对象中,每次访问就+1.这里利用了application对 ...
- PHP与JSP简单比较
比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较php和Java的Web开发.以下就几个主要方面进行的比较: 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上手 ...
- 2017.11.12 web中JDBC 方式访问数据库的技术
JavaWeb------ 第四章 JDBC数据库访问技术 在JavaWeb应用程序中数据库访问是通过Java数据库连接(JavaDateBase Connectivity简称JDBC)数据库的链接一 ...
- 使用Spring.net中对Ado.net的抽象封装来访问数据库
使用Spring.net中对Ado.net的抽象封装来访问数据库 Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入 ...
- Spring实战6:利用Spring和JDBC访问数据库
主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...
- 基于JQuery+JSP的无数据库无刷新多人在线聊天室
JQuery是一款非常强大的javascript插件,本文就针对Ajax前台和JSP后台来实现一个无刷新的多人在线聊天室,该实现的数据全部存储在服务端内存里,没有用到数据库,本文会提供所有源程序,需要 ...
- SQL ser 跨实例访问数据库
SqlServer如何跨实例访问数据库 Exec sp_droplinkedsrvlogin LinkName,NullExec sp_dropserver LinkName go EXEC sp_a ...
随机推荐
- 安装tar.bz2文件
(1) 解包 – tar jxvf softname-10.0.1.tar.gz -C /usr/src/(-C指的是把文件解压到后面的路径下,此处可以不选) – cd /usr/src/softna ...
- Android之网络编程
本文主要包括三方面内容 Httpurlconnection中doGet与doPost方法实现提交数据到服务器 HttpClient中doGet与doPost方法实现提交数据到服务器 android-a ...
- div img居中的方式
想让div中的img水平和垂直都居中,可以将img放在div中,img的样式:height:100%;width:100%; 外部定义div的宽度和高度,然后定义line-height行高,div外部 ...
- Harris角点
1. 不同类型的角点 在现实世界中,角点对应于物体的拐角,道路的十字路口.丁字路口等.从图像分析的角度来定义角点可以有以下两种定义: 角点可以是两个边缘的角点: 角点是邻域内具有两个主方向的特征点: ...
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
- MATLAB学习笔记(七)——MATLAB解方程与函数极值
(一)线性方程组求解 包含n个未知数,由n个方程构成的线性方程组为: 其矩阵表示形式为: 其中 一.直接求解法 1.左除法 x=A\b; 如果A是奇异的,或者接近奇异的.MATLAB会发出警告信息的. ...
- C#将DataTable转换成list的方法
本文实例讲述了C#将DataTable转换成list及数据分页的方法.分享给大家供大家参考.具体如下: /// <summary> /// 酒店评论列表-分页 /// </su ...
- Codeforces Round #292 (Div. 2)
A. Drazil and Date 无算法,判断(s - (a + b)) % 2是否为零,若零,表示在s步内还能走向其他的地方并且回来 否则,都是No #include <cstdio> ...
- LightOJ1382 The Queue(树形DP)
题目大概是说给一棵树的n个结点从1到n编号,要求每个结点的编号大于其父结点,问有多少种编号方式. 想了挺久的,感觉有点眉目,最后画了下样例YY出解法: 首先求出以每个结点为根的子树大小,记为size[ ...
- BZOJ3906 : Trie
将输入的Trie建成AC自动机,并建出fail树. 那么操作1等价于在给定点的子树的并集里都加1. 操作2等价于查询给定点到根节点路径的并集的权值和. 求出DFS序后,对于操作1,将点按进入时间戳从小 ...