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"%>

Test connecting mysql db

<%!

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.");

}

%>

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 一. Java基础部分.................................................
    wy_sure阅读 3,840评论 0 11
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,681评论 1 114
  • 感赏儿子今天早饭前主动刷牙洗脸! 感赏儿子晚上自己准备晚餐,自己照顾自己,不让妈妈操心! 感赏儿子每次没带手机都会...
    晓莉_f92b阅读 89评论 0 2