JDBCUtil,java

package cn.qst.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {
private static final String DBDRIVER = "com.mysql.jdbc.Driver";

private static final String DBURL = "jdbc:mysql://localhost:3306/studyweb?useUnicode=true&characterEncoding=utf-8&useSSL=false";

private static final String DBUSER = "root";

private static final String DBPASSWORD = "root";
/**
* 获取数据库连接
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
//加载驱动
Class.forName(DBDRIVER);
//创建数据库连接
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}

/**
* 关闭数据连接类
* 连接 预编译命令 、 执行命令 执行结果集
*/
public static void closeAll(Connection conn,PreparedStatement pstmt,Statement stmt,ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

***********************************************************************************

用以上JDBCUtil.java进行实现增删改查基本操作,例子如下

dao中PublicNotice 接口的定义

package cn.qst.dao;

import java.util.List;

import cn.qst.vo.Notice;

public interface PublicNotice {
int insertnotice(Notice notice);
List<Notice> displaypublicnotice();
int deletenotice(int noticeId);
int updatenotice(Notice notice);
Notice displayEditnotice(int noticeId);
}

dao中接口PublicNotice 方法的实现

package cn.qst.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.qst.dao.PublicNotice;
import cn.qst.util.JDBCUtil;
import cn.qst.vo.Notice;

public class PublicNoticeDaoImpl extends JDBCUtil implements PublicNotice {
private Connection conn=null;
private PreparedStatement pstmt=null;
private Statement stmt=null;
private ResultSet rs=null;

@Override
public int insertnotice(Notice notice) {
int count=0;
String sql="insert into Notice(title,context,publicerId,publicer,writeDate) values(?,?,?,?,?)";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,notice.getTitle());
pstmt.setString(2, notice.getContext());
pstmt.setInt(3,notice.getPublicerId());
pstmt.setString(4, notice.getPublicer());
pstmt.setTimestamp(5, notice.getWriteDate());
count=pstmt.executeUpdate();
} catch (SQLException e) {

e.printStackTrace();
}

return count;
}

@Override
public List<Notice> displaypublicnotice() {
List<Notice> list=new ArrayList<Notice>();
Notice notice=null;
String sql="select noticeId,title,context,publicerId,publicer,writeDate from notice";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
notice=new Notice();
notice.setNoticeId(rs.getInt("noticeId"));
notice.setTitle(rs.getString("title"));
notice.setContext(rs.getString("context"));
notice.setPublicerId(rs.getInt("publicerId"));
notice.setPublicer(rs.getString("publicer"));
notice.setWriteDate(rs.getTimestamp("writeDate"));
list.add(notice);
}
} catch (SQLException e) {

e.printStackTrace();
}finally{ // 7。关闭连接
super.closeAll(conn, pstmt, stmt, rs);
}

return list;
}

@Override
public int deletenotice(int noticeId) {
int count=0;
String sql="delete from notice where noticeId=?";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, noticeId);
count=pstmt.executeUpdate();
} catch (SQLException e) {

e.printStackTrace();
}finally{ // 7。关闭连接
super.closeAll(conn, pstmt, stmt, rs);
}

return count;
}

@Override
public int updatenotice(Notice notice) {
int count=0;
String sql="update notice set title=?,context=?,writeDate=? where noticeId=?";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,notice.getTitle());
pstmt.setString(2, notice.getContext());
pstmt.setTimestamp(3, notice.getWriteDate());
pstmt.setInt(4,notice.getNoticeId());
count=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
super.closeAll(conn, pstmt, stmt, rs);
}
return count;
}

@Override
public Notice displayEditnotice(int noticeId) {
Notice notice=null;
String sql="select noticeId,title,context,publicerId,publicer,writeDate from notice where noticeId=?";
conn=super.getConnection();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, noticeId);
rs=pstmt.executeQuery();
while(rs.next()){
notice=new Notice();
notice.setNoticeId(rs.getInt("noticeId"));
notice.setTitle(rs.getString("title"));
notice.setContext(rs.getString("context"));
notice.setPublicerId(rs.getInt("publicerId"));
notice.setPublicer(rs.getString("publicer"));
notice.setWriteDate(rs.getTimestamp("writeDate"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
super.closeAll(conn, pstmt, stmt, rs);
}

return notice;
}

}

jdbc封装的类的更多相关文章

  1. 创建Jdbc封装工具类

    jdbc.propertie url=jdbc:mysql:///empye user=root password=root driver=com.mysql.jdbc.Driver 读取资源文件  ...

  2. JDBC封装的工具类

    1. JDBC封装的工具类 public class JDBCUtil { private static Properties p = new Properties(); private static ...

  3. MySQL JDBC常用知识,封装工具类,时区问题配置,SQL注入问题

    JDBC JDBC介绍 Sun公司为了简化开发人员的(对数据库的统一)操作,提供了(Java操作数据库的)规范,俗称JDBC,这些规范的由具体由具体的厂商去做 对于开发人员来说,我们只需要掌握JDBC ...

  4. 优化JDBC封装

    可重用性较强的JDBC封装 以下为代码,注释中写了主要思想 主类 com.util.JDBCUtil.java package com.util; import java.lang.reflect.F ...

  5. JDBC和驱动类Driver

    什么是JDBC? JDBC(Java DataBase Connectivity),是一套面向对象的应用程序接口(API),制定了统一的访问各类关系数据库的标准接口,为各个数据库厂商提供了标准的实现. ...

  6. JDBC相关的类介绍

    JDBC 背景:1996年,Sun公司推出了Java数据库连接(Java Database Connectivity JDBC)工具包的第一个版本.该工具包使得程序员可以使用结构化语言SQL连接到一个 ...

  7. SpringMVC 自动封装枚举类的方法

    springmvc默认无法自动封装枚举类,解决方法如下: 1.枚举类 public enum GoodsPromoteEnum { /** * 0 精品 */ fine("精品", ...

  8. iOS NSURLSession 封装下载类

    周六日鼓捣NSURLSession,效率虽然低下,最后还是有了一点点眉目.昨天和汤老师一起测试,又对它加深了一点理解.趁热打铁,先总结一下. 封装的类,对外用的方法,我写的是类方法,所以,在类方法中, ...

  9. 封装application类

    <?php  //判断用户是否是通过入口文件访问   if(!defined('ACCESS')){     echo '非法请求';     die;   }   //封装初始化类   cla ...

随机推荐

  1. .net core webapi 前后端开发分离后的配置和部署

    背景:现在越来越多的企业都采用了在开发上前后端分离,前后端开发上的分离有很多种,那么今天,我来分享一下项目中得的前后端分离. B/S  Saas 项目:(这个项目可以理解成个人中心,当然不止这么点功能 ...

  2. 使用Keepalived构建LVS高可用集群

    LVS的DR模型配置+Keepalive部署 介绍 下图为DR模型的通信过程,图中的IP不要被扑结构中的IP迷惑,图里只是为了说明DR的通信原理,应用到本例中的拓扑上其工作原理不变. 拓扑结构 服务器 ...

  3. ifrem上传文件后显示

    ifrem上传文件后显示 1.上传文件按钮    <a class="btn btn-primary pull-right" id="data-upload&quo ...

  4. Java虚拟机三:OutOfMemoryError异常分析

    根据Java虚拟机规范,虚拟机内存中除过程序计数器之外的运行时数据区域都会发生OutOfMemoryError(OOM),本文将通过实际例子验证分析各个数据区域OOM的情况.为了更贴近生产,本次所有例 ...

  5. 六大设计原则(三)DIP依赖倒置原则

    依赖倒置原则DIP(Dependence Inversion Principle) 依赖倒置原则的含义 高层模块不能依赖低层模块,二者都应该依赖其抽象. 抽象不应该依赖于细节. 细节应该依赖抽象. 什 ...

  6. 首发福利!全球第一开源ERP Odoo系统架构部署指南 电子书分享

    引言 Odoo,以前叫OpenERP,是比利时Odoo S.A.公司开发的一个企业应用软件套件,开源套件包括一个企业应用快速开发平台,以及几千个Odoo及第三方开发的企业应用模块.Odoo适用于各种规 ...

  7. salesforce初探

      Salesforce的商业模式? 从做CRM SAAS起家,可以理解为在线CRM,不需要硬件和服务器,输入用户名和密码就能登陆使用.2007年推出PaaS平台Force.com,可以说,它依托CR ...

  8. 【Android】用Cubism 2制作自己的Live2D——官方App样例源码学习(1)!

    前言- 上几篇文章,我们一个一个的研究了Cubism官方提供的Android使用Live2D的简单例子,但是依旧和大家平时见到的还是有很大差距的.在研究了代码差不多一周以后,我决定还是用文字的形式记录 ...

  9. SQL2005打SP4补丁报错:无法安装Windows Installer MSP文件解决方案

    错误如图: 解决方案分享如下: 第一步:卸载下图红框圈住的玩艺. 第二步:把SP4补丁文件解压,找到下图红框圈住的玩艺: 第三步:重新运行SP4补丁安装文件,安装正常.

  10. Ambari 常用的 REST API 介绍

    源码文档路径:ambari\ambari-server\docs\api\v1 swagger风格api文档:https://www.cnblogs.com/felixzh/p/10694724.ht ...