国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

JDBC增刪改查和查唯一的完整代碼解析

2019-11-26 13:23:07
字體:
來源:轉載
供稿:網友

第一部分代碼(實體類)

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("成功!");elseSystem.out.println("失敗!");}}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 云梦县| 石渠县| 肃南| 遂宁市| 东乡族自治县| 湘西| 广宗县| 扬州市| 乐业县| 米易县| 嘉黎县| 木里| 中宁县| 龙胜| 翁源县| 新乡县| 澄城县| 竹山县| 乾安县| 沐川县| 离岛区| 陇川县| 乃东县| 兰西县| 淮阳县| 墨竹工卡县| 南通市| 克什克腾旗| 安乡县| 安西县| 油尖旺区| 文山县| 昌江| 津市市| 卢湾区| 永年县| 新余市| 大新县| 自治县| 宝兴县| 炉霍县|