---
title: "JSP简单访问数据库"
author: "香水浓"
date: "2015-04-19T06:36:12Z"
tags: ["JSP", "MySQL", "SQL"]
categories: ["Web前端"]
slug: "jsp"
draft: false
lastmod: "2015-04-19T07:10:11Z"
id: 2203665
url: "https://794818247.iteye.com/blog/2203665"
---

学习使用javaBean，代码很烂，仅为留个脚印 \[code="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; } } \[/code] \[code="java"] &lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt; Test connecting mysql db &lt;%! private int id = 0; %&gt; &lt;% 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."); } %&gt; \[/code] \[code="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. \[/code] Note：本意是要让log打印出来是哪个session在执行，看到结果...又长知识了 \[color=red]&lt;%! %&gt;\[/color] 查了下，觉得这种说法有道理，暂不深究： &lt;%%&gt;是代码段,在由jsp转换成Servlet后 &lt;%%&gt;中的代码是放在serive方法中,相当于doGet()和doPost()方法 &lt;%!%&gt;是jsp声明,用来定义属性和方法的,在由jsp转换成Servlet后 &lt;%!%&gt;中的代码是放serive方法之外的