JDBC增刪改查和查唯一的完整代碼解析
第一部分代碼(實(shí)體類)
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語(yǔ)句 * @param params 參數(shù)數(shù)組 * @return 受影響的行數(shù) */ 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; } /** * 查詢結(jié)果集的方法 * @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(); } }
/** * 查詢唯一的結(jié)果 * @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; }
第四部分 測(cè)試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("個(gè)"); hehe.setHehe_gender("b"); HeheDao _hd=new HeheDao(); if(_hd.insert(hehe)) System.out.println("成功!"); else System.out.println("失敗!"); } }
- java jdbc連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查操作
- Java使用JDBC連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
- Java開(kāi)發(fā)Oracle數(shù)據(jù)庫(kù)連接JDBC Thin Driver 的三種方法
- JSP使用JDBC連接MYSQL數(shù)據(jù)庫(kù)的方法
- java使用jdbc連接數(shù)據(jù)庫(kù)工具類和jdbc連接mysql數(shù)據(jù)示例
- jdbc連接sqlserver數(shù)據(jù)庫(kù)示例
- JDBC 連接MySQL實(shí)例詳解
- jsp+servlet+jdbc實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查
- jdbc實(shí)現(xiàn)連接和增刪改查功能
相關(guān)文章
詳解Java?ThreadPoolExecutor的拒絕策略
這篇文章主要介紹了Java?ThreadPoolExecutor的拒絕策略,本文對(duì)于線程的池的幾種策略進(jìn)行詳細(xì)的講解,在實(shí)際的生產(chǎn)中需要集合相關(guān)的場(chǎng)景來(lái)選擇合適的拒絕策略,需要的朋友可以參考下2022-08-08SpringBoot框架實(shí)現(xiàn)切換啟動(dòng)開(kāi)發(fā)環(huán)境和測(cè)試環(huán)境
這篇文章主要介紹了SpringBoot框架實(shí)現(xiàn)切換啟動(dòng)開(kāi)發(fā)環(huán)境和測(cè)試環(huán)境,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringCloud微服務(wù)調(diào)用丟失請(qǐng)求頭的問(wèn)題及解決方案
在Spring Cloud 中微服務(wù)之間的調(diào)用會(huì)用到Feign,但是在默認(rèn)情況下,Feign 調(diào)用遠(yuǎn)程服務(wù)存在Header請(qǐng)求頭丟失問(wèn)題,下面給大家分享SpringCloud微服務(wù)調(diào)用丟失請(qǐng)求頭的問(wèn)題及解決方案,感興趣的朋友一起看看吧2024-02-02SpringBoot在項(xiàng)目停止(服務(wù)停止/關(guān)閉退出)之后執(zhí)行的方法
這篇文章主要給大家介紹了SpringBoot在項(xiàng)目停止(服務(wù)停止/關(guān)閉退出)之后執(zhí)行的兩種方法,實(shí)現(xiàn)DisposableBean接口和使用@PreDestroy注解,文中有詳細(xì)的代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12java 實(shí)現(xiàn)反射 json動(dòng)態(tài)轉(zhuǎn)實(shí)體類--fastjson
這篇文章主要介紹了java 實(shí)現(xiàn)反射 json動(dòng)態(tài)轉(zhuǎn)實(shí)體類--fastjson,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02Spring Boot 安全 API 構(gòu)建之加密解密功能的實(shí)踐記錄
本文詳述了如何在SpringBoot3.3環(huán)境中實(shí)施API加密的最佳實(shí)踐,包括選擇合適的加密算法,密鑰管理,數(shù)據(jù)加密,防止加密漏洞,安全日志記錄,測(cè)試和監(jiān)控等方面,同時(shí),文章也對(duì)RSA非對(duì)稱加密和AES對(duì)稱加密的實(shí)現(xiàn)步驟進(jìn)行了詳細(xì)的解析2024-10-10idea下如何設(shè)置項(xiàng)目啟動(dòng)的JVM運(yùn)行內(nèi)存大小
這篇文章主要介紹了idea下如何設(shè)置項(xiàng)目啟動(dòng)的JVM運(yùn)行內(nèi)存大小問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12經(jīng)典的Java面試題及回答集錦(基礎(chǔ)篇)
本文給大家收藏整理了java面試題及回答,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03詳解在SpringBoot中@Transactional事物操作和事物無(wú)效問(wèn)題排查
這篇文章主要介紹了詳解在SpringBoot中@Transactional事物操作和事物無(wú)效問(wèn)題排查,本文詳細(xì)的介紹了SpringBoot中集成使用@Transactional注解操作事物以及事物開(kāi)啟后無(wú)效的問(wèn)題排查,需要的朋友可以參考下2021-06-06