JDBC增删改查和查唯一的完整代码解析
作者:long_street_to_walk 发布时间:2023-12-22 15:53:30
标签:jdbc,增删,改查
第一部分代码(实体类)
package com.wf.entity;
public class Hehe{
private int hehe_id;
private String hehe_name;
private String hehe_gender;
public int getHehe_id(){
return hehe_id;
}
public void setHehe_id(int heheId){
hehe_id=heheId;
}
public String getHehe_name() {
return hehe_name;
}
public void setHehe_name(String heheName) {
hehe_name = heheName;
}
public String getHehe_gender() {
return hehe_gender;
}
public void setHehe_gender(String heheGender) {
hehe_gender = heheGender;
}
}
第二部分 BaseDao(增删改查和查唯一)
package com.wf.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao{
protected static final String DRIVER="com.mysql.jdbc.Driver";
protected static final String URL="mysql://localhost:3306/mysql";
protected static final String USER="root";
protected static final String PASSWORD="******";
protected Connection connection=null;
protected PreparedStatement preparedStatement=null;
protected ResultSet resultSet =null;
protected void getconnection() throws ClassNotFoundException, SQLException{
Class.forName(DRIVER);
this.connection =DriverManager.getconnection (URL,USER,PASSWORD);
}
/**
* 通用的增删改方法
* @param sql SQL语句
* @param params 参数数组
* @return 受影响的行数
*/
protected int executeUpdate(String sql ,String[]params){
int result=-1;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}
}
result= this.preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.close();
}
return result;
}
/**
* 查询结果集的方法
* @param sql
* @param params
*/
protected void executeQuery(String sql,String[]params){
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}
}
this.resultSet=this.preparedStatement.executeQuery();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 查询唯一的结果
* @return Object
*/
protected Object executeQueryUnique(String sql,String[]params){
Object object=null;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}
}
this.resultSet=this.preparedStatement.executeQuery();
if(this.resultSet.next())
object=this.resultSet.getObject(1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return object;
}
protected void close(){
try {
if(null!=this.resultSet)
this.resultSet.close();
if(null!=this.preparedStatement)
this.preparedStatement.close();
if(null!=this.connection)
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
第三部分 HeheDao
package com.wf.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.wf.entity.Hehe;
public class HeheDao extends BaseDao{
public boolean insert(Hehe hehe){
String sql="insert into hehe(hehe_name,hehe_gender) values(?,?)" ;
String []params=new String[]{hehe.getHehe_name(),hehe.getHehe_gender()};
return this.executeUpdate(sql, params)>0? true:false;
}
第四部分 测试Test_BaseDao_Insert
package com.wf.test;
import com.wf.dao.HeheDao;
import com.wf.entity.Hehe;
public class Test_BaseDao_Insert {
public static void main(String[] args) {
Hehe hehe=new Hehe();
hehe.setHehe_name("个");
hehe.setHehe_gender("b");
HeheDao _hd=new HeheDao();
if(_hd.insert(hehe))
System.out.println("成功!");
else
System.out.println("失败!");
}
}
来源:http://www.cnblogs.com/wffj150926/archive/2016/12/25/6219908.html


猜你喜欢
- 本文实例为大家分享了Springboot整合pagehelper分页展示的具体代码,供大家参考,具体内容如下一、添加依赖查找maven中pa
- 简介OCSP在线证书状态协议是为了替换CRL而提出来的。对于现代web服务器来说一般都是支持OCSP的,OCSP也是现代web服务器的标配。
- 前言不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息。获取用户信息就是获取公众号下微信用户的
- 一、国际化准备资源文件,资源文件的命名格式如下:baseName_language_country.propertiesbaseName_l
- 1.注解方式,yml文件配置上以下就可以直接使用mybatis-plus: mapper-locations: classpath:mapp
- Properties属性文件中的值有等号和换行Spring配置Shiro的过滤器时,有个filterChainDefinitions属性,值
- 开发中对版本进行检查并更新的需求基本是所有应用必须有的功能,可是在实际开发中有些朋友就容易忽略一些细节。版本更新的基本流程:一般是将本地版本
- 以下弹出框是框的实现,放入到SWT项目下就可运行。1.提示框MessageBox mb = new MessageBox(shell,SWT
- Java游戏俄罗斯方块的实现实例 java小
- 效果如下: 布局文件activity_main.xml如下:<RelativeLayout xmlns:android="h
- 栈栈(stack)又名堆栈,它是一种运算受限的线性表 。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。
- 背景近期因实际项目需要,在特定操作下触发定位请求,取到用户位置及附近位置。问题:经初步选型,最终决定接入百度定位,按照百度定位SDK And
- 一、先看结果1.1创造营2020撑腰榜前三甲创造营2020撑腰榜前三名分别是 希林娜依·高、陈卓璇 、郑乃馨>>>df1[
- 1. strlen(),计算字符串长度 int strlen(const char string) {
- 文章描述以下主要还是使用到了ffmpeg命令,分别实现了给视频添加图片水印以及文字水印。开发环境.NET Framework版本:4.5开发
- 1、mybatis-plus相信大家在日常的开发中用的最多的就是 mybatis-plus了吧,作为一个 MyBatis (opens ne
- 将通用算法放入具体类(HeapSorter),并将通用算法必须调用的方法定义在接口(HeapSorterHandle)中,从这个接口派生出D
- 问题提出:一般在生产环境上,日志的级别是INFO以上,但有时候程序出现问题(如SQL报错),少量日志不能尽快定位问题,这时候可以动态修改日志
- Zipkin是一种分布式跟踪系统。它有助于收集解决微服务架构中延迟问题所需的时序数据。它管理这些数据的收集和查找。Zipkin的设计基于&n
- 本文实例为大家分享了Android实现View滑动效果的具体代码,供大家参考,具体内容如下一、View的滑动简介View的滑动是Androi