JDBC增删改查和查唯一的完整代码解析
更新时间:2016年12月25日 17:08:40 作者:long_street_to_walk
这篇文章主要介绍了JDBC增删改查和查唯一的完整代码解析,代码分为第四部分,每部分代码都不错,对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("失败!");
}
}
相关文章
详解Java ThreadPoolExecutor的拒绝策略
这篇文章主要介绍了Java ThreadPoolExecutor的拒绝策略,本文对于线程的池的几种策略进行详细的讲解,在实际的生产中需要集合相关的场景来选择合适的拒绝策略,需要的朋友可以参考下2022-08-08
SpringBoot在项目停止(服务停止/关闭退出)之后执行的方法
这篇文章主要给大家介绍了SpringBoot在项目停止(服务停止/关闭退出)之后执行的两种方法,实现DisposableBean接口和使用@PreDestroy注解,文中有详细的代码讲解,具有一定的参考价值,需要的朋友可以参考下2023-12-12
java 实现反射 json动态转实体类--fastjson
这篇文章主要介绍了java 实现反射 json动态转实体类--fastjson,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-02-02
Spring Boot 安全 API 构建之加密解密功能的实践记录
本文详述了如何在SpringBoot3.3环境中实施API加密的最佳实践,包括选择合适的加密算法,密钥管理,数据加密,防止加密漏洞,安全日志记录,测试和监控等方面,同时,文章也对RSA非对称加密和AES对称加密的实现步骤进行了详细的解析2024-10-10
详解在SpringBoot中@Transactional事物操作和事物无效问题排查
这篇文章主要介绍了详解在SpringBoot中@Transactional事物操作和事物无效问题排查,本文详细的介绍了SpringBoot中集成使用@Transactional注解操作事物以及事物开启后无效的问题排查,需要的朋友可以参考下2021-06-06


最新评论