jdbc封装的类
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封装的类的更多相关文章
- 创建Jdbc封装工具类
jdbc.propertie url=jdbc:mysql:///empye user=root password=root driver=com.mysql.jdbc.Driver 读取资源文件 ...
- JDBC封装的工具类
1. JDBC封装的工具类 public class JDBCUtil { private static Properties p = new Properties(); private static ...
- MySQL JDBC常用知识,封装工具类,时区问题配置,SQL注入问题
JDBC JDBC介绍 Sun公司为了简化开发人员的(对数据库的统一)操作,提供了(Java操作数据库的)规范,俗称JDBC,这些规范的由具体由具体的厂商去做 对于开发人员来说,我们只需要掌握JDBC ...
- 优化JDBC封装
可重用性较强的JDBC封装 以下为代码,注释中写了主要思想 主类 com.util.JDBCUtil.java package com.util; import java.lang.reflect.F ...
- JDBC和驱动类Driver
什么是JDBC? JDBC(Java DataBase Connectivity),是一套面向对象的应用程序接口(API),制定了统一的访问各类关系数据库的标准接口,为各个数据库厂商提供了标准的实现. ...
- JDBC相关的类介绍
JDBC 背景:1996年,Sun公司推出了Java数据库连接(Java Database Connectivity JDBC)工具包的第一个版本.该工具包使得程序员可以使用结构化语言SQL连接到一个 ...
- SpringMVC 自动封装枚举类的方法
springmvc默认无法自动封装枚举类,解决方法如下: 1.枚举类 public enum GoodsPromoteEnum { /** * 0 精品 */ fine("精品", ...
- iOS NSURLSession 封装下载类
周六日鼓捣NSURLSession,效率虽然低下,最后还是有了一点点眉目.昨天和汤老师一起测试,又对它加深了一点理解.趁热打铁,先总结一下. 封装的类,对外用的方法,我写的是类方法,所以,在类方法中, ...
- 封装application类
<?php //判断用户是否是通过入口文件访问 if(!defined('ACCESS')){ echo '非法请求'; die; } //封装初始化类 cla ...
随机推荐
- 对Javascript 类、原型链、继承的理解
一.序言 和其他面向对象的语言(如Java)不同,Javascript语言对类的实现和继承的实现没有标准的定义,而是将这些交给了程序员,让程序员更加灵活地(当然刚开始也更加头疼)去定义类,实现继承 ...
- AI2(App Inventor 2)离线版服务器(2019.04.28更新)
我们的目标:搭建一个本地多用户的App Inventor 2 服务器 演示: http://ai2.fsyz.net [旧 win] http://ai2n.fsyz.net [新 Ce ...
- 从壹开始 [ Nuxt.js ] 之一 || 为开源收录Bug之 TiBug项目 开篇讲
缘起 哈喽大家周二好呀,刚刚经历过了几天火车抢票,整个人都不好了,不知道小伙伴对今年的春节是否还一如既往的期待呢,眼看都要春节了,本来也想写篇2018总结篇,但是怕不免会出现鸡汤文的窠臼嫌疑,想想还是 ...
- mysql实现主从备份
mysql 主从备份的原理: 主服务器在做数据库操作的时候将所有的操作通过日志记录在binlog里面,有专门的文件存放.如localhost-bin.000003,这种,从服务器 和主服务配置好关系后 ...
- css中margin为负数的深入研究
注:以下实验的元素均为块级元素,inline-block和inline本身对margin某些方向上都是无效的,所以这里不予讨论. margin-left或者margin-right为负数 当块元素wi ...
- 聊聊 JUC 并发包
今天开始我们聊聊 Java 并发工具包中提供的一些工具类,本文主要从并发同步容器和并发集合工具角度入手,简单介绍下相关 API 的用法与部分实现原理,旨在帮助大家更好的使用和理解 JUC 工具类. 在 ...
- zookeeper源码 — 一、单机启动
zookeeper一般使用命令工具启动,启动主要就是初始化所有组件,让server可以接收并处理来自client的请求.本文主要结构: main入口 配置解析 组件启动 main入口 我们一般使用命令 ...
- Smobiler Service是什么?(Smobiler——.NET移动开发平台)
在得知Smobiler即将推出新产品SmobilerService之后,许多人第一个疑问便是——Smobiler Service是什么? Smobiler的开发者对这个exe窗口一定不陌生,有时候因为 ...
- 浅析C语言中的整形类型
在C语言中,可以把 字符型.短整形.整形.长整形都看作是整形,同属于整形家族这个大类型. 这些类型的大小,默认是否有符号等一些知识点较零散,较容易混淆,所以特地整理如下. 一 类型存储字节长度说明 ...
- (转)ceph 常用 运维命令--查看信息 - 不错的文档
下面是测试验证环节 1. 创建一个 pool rbd create foo --size 4 --image-format 2 --image-feature layering 2. 挂载和格式化 r ...