学习使用javaBean,代码很烂,仅为留个脚印
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;
}
}
<%@ 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>
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方法之外的
分享到:
相关推荐
是一个最简单,也是学习jsp必须会的一个入门实验----通过jdbc连接数据库。 采用jsp+access数据库实现,同时使用javabean通过jdbc连接数据库。运行结果将显示从数据库查询出的图书。里面包含了javabean的源代码及解释...
jsp+servlet+javaBean访问数据库的简单例子,但是类设置非常巧妙,大家注意。
简单实现数据库的链接,数据库的访问,插入,修改,删除
JSP之访问JavaBean的一个简单例子 使用useBean,jsp:setProperty,jsp:getProperty使用的一个简单例子 博客参考地址:https://blog.csdn.net/qq_36631076/article/details/79693890
介绍了JSP、JDBC2ODBC技术 ,分析JSP 通过Java Bean 和JDBC 技术访问 Web 数据库 ,并以 SQL Server2000 数据库为例 ,给出用JDBC2ODBC实现的一个简单留言版的访问实例。
jsp调用mysql数据库简单样例, 另外前端跨域的话,在back.jsp中加入以下: response.addHeader( "Access-Control-Allow-Origin","*");//允许所有来源访同 response.addHeader( "Access-Control-Allow-Method","POST,...
自己写的一个简单的用户登录页面,实现与后台数据库的交互。非常适合新手
JSP/Servlet数据库分页原理解析源码下载,注释详尽、简单易懂 将下载后的压缩文件解压后可以得到page_csdn文件夹,用eclipse导入即可,运行之前请运行page.sql数据库脚本,我使用的是MySQL数据库。 部署到Tomcat...
数据库分页原理解析源码下载,注释清楚、简单易懂 将下载后的压缩文件解压后可以得到page_csdn文件夹,用eclipse导入即可,运行之前请运行page.sql数据库脚本,我使用的是MySQL数据库。 部署到Tomcat服务器后,...
在您开始学习 JSP 数据库访问之前,请访问 Java MySQL 连接 来设置相关驱动及配置。 注意: 你可以下载本站提供的 jar 包:mysql-connector-java-5.1.39-bin.jar 下载后把 mysql-connector-java-5.1.39-bin.jar ...
JSP实验环境组建、简单JSP应用、JSP内置对象的应用、Servlet的应用、JSP访问数据库的应用
JSP聊天室chatroom,简单实用,不连接数据库直接可以访问
有助于加深初学者对jsp的理解和使用Mysql数据库 String DBDRIVER = "com.mysql.jdbc.Driver"; String DBURL ="jdbc:mysql://localhost:3306/db_jsp?characterEncoding=utf-8"; //数据库名:tianming String ...
最简单粗暴的mysql数据库访问,实现简单用户名和密码登录功能,最直接的演示 操作jsp+servlet+mysql
孙鑫写的示例,简单而且很好理解,源码,JSP,JavaBean
实例54 利用连接池访问数据库 第8章 JSP中JavaBean的应用 实例55 在JSP中应用JavaBean 实例56 用HTML表单设置JavaBean的属性值 实例57 计数器JavaBean 实例58 查询数据库JavaBean 实例59 购物车JavaBean及其...
基于 MySQL+Tomcat+Servlet+JSP 的简单作业管理系统源码+数据库+项目说明.zip # HomeworkManagement ## 简介 一个基于 MySQL+Tomcat+Servlet+JSP 的简单作业管理系统 --- ## 更新 ### Ver 5.0 - 所有模块...
本项目是基于java的,在jsp中通过访问数据库里的数据实现实现用户登录操作。里面有部分js代码,希望对新手学web有帮助
本文描述的网络考试系统主要是利用浏览器作为界面,利用B/S模式,即用户可利用浏览器直接访问本站点。主要用到的技术是采用JavaWeb技术和MySql数据库等设计出了各种功能。该系统主要有用户管理、功能管理、角色权限...
定义了连接ACCESS数据库的方法,可以通过调用此方法访问数据库。 4. MessageBean.java 用来进行留言管理的bean,定义了添加留言,获取留言数据,以及删除留言的方法。 5. adminInfo.java 定义了管理员字段...