当前位置:首页 > 开发 > Web前端 > JavaScript > 正文

JSP简单访问数据库

发表于: 2015-04-19   作者:香水浓   来源:转载   浏览:
摘要: 学习使用javaBean,代码很烂,仅为留个脚印 public class DBHelper { private String driverName; private String url; private String user; private String password; private Connection connection; privat
学习使用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简单访问数据库

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
下面是数据库的结构,为了简化操作该数据库只有一个表player,这个表只有PlayerID、PlayerName、Pla
1:编译 linux直接执行以下命令: ./configure --prefix=/work/postgresql-9.1.9/binpost 当进行交
1 结构 2 LoginTestServlet.java package com.test; import java.io.IOException; import java.sql.
应用程序服务器使您可以使用服务器端的资源,例如数据库。例如,动态页可以指示应用程序服务器从数
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML
将工程打包成WAR,然后发布到weblogic下,不能访问JSP,这个貌似很基础的问题确困扰了我1晚+1上午 首
将工程打包成WAR,然后发布到weblogic下,不能访问JSP,这个貌似很基础的问题确困扰了我1晚+1上午 首
1、 如何实现访问控制 用户直接访问网站的某个页面,系统会去查询是否保存有该用户的登录信息,如果
需求 现在网站首页有一个新闻图片轮播的功能,五张图片按照顺序,随机使用各种特效进行替换,点击图
1、mysql的limit关键字 (DAO) select * from tablename limit startPoint, numberPerPage; tablen
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号