金瞳魔帝传,趣多多兑换网站,土布鸟
第一部分代码(实体类)
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("失败!"); } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论