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. java基础(六)-----String性质深入解析

    本文将讲解String的几个性质. 一.String的不可变性 对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创建就不可以修改 ...

  2. 响应式WEB设计的基本原则大总结

    响 应式Web设计对于解决多类型屏幕问题来说是个不错方案,但从印刷的角度来看,其却存在着很多的困难.没有固定的页面尺寸.没有毫米或英寸,没有任何物理 限制,让人感到无从下手.随着建立网站可用的各种小工 ...

  3. java爬虫,爬取当当网数据

     背景:女票快毕业了(没错!我是有女票的!!!),写论文,主题是儿童性教育,查看儿童性教育绘本数据死活找不到,没办法,就去当当网查询下数据,但是数据怎么弄下来呢,首先想到用Python,但是不会!!百 ...

  4. Python爬虫入门教程 62-100 30岁了,想找点文献提高自己,还被反爬了,Python搞起,反爬第2篇

    学术搜索 学习理论的知识少不了去检索文献,好多文献为你的实操提供了合理的支撑,我所在的大学内网默认是有知网账户的,非常NICE 今天要完成的网站是 http://ac.scmor.com/ Googl ...

  5. Firemonkey 原生二维码扫描优化

    之前用了ZXing的Delphi版本,运行自带的例子,速度非常慢,与安卓版本的相比查了很多,因此打算使用集成jar的方法,但是总觉得美中不足. 经过一番研究,基本上解决了问题. 主要有两方面的优化: ...

  6. POLARDB · 最佳实践 · POLARDB不得不知道的秘密(二)

    前言 POLARDB For MySQL(下文简称POLARDB)目前是阿里云数据库团队主推的关系型数据库.线上已经有很多企业用户在使用并且稳定运行了很久.当然,由于POLARDB是为云上环境专门打造 ...

  7. git工具使用说明

    一.什么是git?     Git是分布式版本控制系统      概念: 工作区:就是你在电脑里能看到的目录: 暂存区:一般存放在(.git/index)中,所以我们把暂存区有时也叫作索引(index ...

  8. Exp3免杀原理与实践 20164312 马孝涛

    1.实验要求   1.1 正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5分),veil-evasion(0.5分),加壳工具(0.5分),使用shellcode编 ...

  9. 2019腾讯WXG移动客户端暑期实习面经

    微信这个比较迷,二面完官网流程灰了,但是过了一周多突然来三面,下午面完三面晚上HR面,第三天offer call, 莫名其妙过了 之前以为已经挂了,所以没有写面经,现在距一面已经快一个月了,只能还记得 ...

  10. Java成神之路技术整理(长期更新)

    以下是Java技术栈微信公众号发布的关于 Java 的技术干货,从以下几个方面汇总. Java 基础篇 Java 集合篇 Java 多线程篇 Java JVM篇 Java 进阶篇 Java 新特性篇 ...