JDBC增删改查和查唯一的完整代码解析
第一部分代码(实体类)
packagecom.wf.entity; publicclassHehe{ privateinthehe_id; privateStringhehe_name; privateStringhehe_gender; publicintgetHehe_id(){ returnhehe_id; } publicvoidsetHehe_id(intheheId){ hehe_id=heheId; } publicStringgetHehe_name(){ returnhehe_name; } publicvoidsetHehe_name(StringheheName){ hehe_name=heheName; } publicStringgetHehe_gender(){ returnhehe_gender; } publicvoidsetHehe_gender(StringheheGender){ hehe_gender=heheGender; } }
第二部分BaseDao(增删改查和查唯一)
packagecom.wf.dao; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.PreparedStatement; importjava.sql.ResultSet; importjava.sql.SQLException; publicclassBaseDao{ protectedstaticfinalStringDRIVER="com.mysql.jdbc.Driver"; protectedstaticfinalStringURL="mysql://localhost:3306/mysql"; protectedstaticfinalStringUSER="root"; protectedstaticfinalStringPASSWORD="******"; protectedConnectionconnection=null; protectedPreparedStatementpreparedStatement=null; protectedResultSetresultSet=null; protectedvoidgetconnection()throwsClassNotFoundException,SQLException{ Class.forName(DRIVER); this.connection=DriverManager.getconnection(URL,USER,PASSWORD); } /** *通用的增删改方法 *@paramsqlSQL语句 *@paramparams参数数组 *@return受影响的行数 */ protectedintexecuteUpdate(Stringsql,String[]params){ intresult=-1; try{ this.getconnection(); this.preparedStatement=this.connection.prepareStatement(sql); if(null!=params){ for(inti=0;i<params.length;i++){ this.preparedStatement.setString(i+1,params[i]); } } result=this.preparedStatement.executeUpdate(); }catch(ClassNotFoundExceptione){ e.printStackTrace(); }catch(SQLExceptione){ e.printStackTrace(); }finally{ this.close(); } returnresult; } /** *查询结果集的方法 *@paramsql *@paramparams */ protectedvoidexecuteQuery(Stringsql,String[]params){ try{ this.getconnection(); this.preparedStatement=this.connection.prepareStatement(sql); if(null!=params){ for(inti=0;i<params.length;i++){ this.preparedStatement.setString(i+1,params[i]); } } this.resultSet=this.preparedStatement.executeQuery(); }catch(ClassNotFoundExceptione){ e.printStackTrace(); }catch(SQLExceptione){ e.printStackTrace(); } }
/** *查询唯一的结果 *@returnObject */ protectedObjectexecuteQueryUnique(Stringsql,String[]params){ Objectobject=null; try{ this.getconnection(); this.preparedStatement=this.connection.prepareStatement(sql); if(null!=params){ for(inti=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(ClassNotFoundExceptione){ e.printStackTrace(); }catch(SQLExceptione){ e.printStackTrace(); } returnobject; } protectedvoidclose(){ try{ if(null!=this.resultSet) this.resultSet.close(); if(null!=this.preparedStatement) this.preparedStatement.close(); if(null!=this.connection) this.connection.close(); }catch(SQLExceptione){ e.printStackTrace(); } }
第三部分HeheDao
packagecom.wf.dao; importjava.sql.Connection; importjava.sql.PreparedStatement; importjava.sql.SQLException; importcom.wf.entity.Hehe; publicclassHeheDaoextendsBaseDao{ publicbooleaninsert(Hehehehe){ Stringsql="insertintohehe(hehe_name,hehe_gender)values(?,?)"; String[]params=newString[]{hehe.getHehe_name(),hehe.getHehe_gender()}; returnthis.executeUpdate(sql,params)>0?true:false; }
第四部分测试Test_BaseDao_Insert
packagecom.wf.test; importcom.wf.dao.HeheDao; importcom.wf.entity.Hehe; publicclassTest_BaseDao_Insert{ publicstaticvoidmain(String[]args){ Hehehehe=newHehe(); hehe.setHehe_name("个"); hehe.setHehe_gender("b"); HeheDao_hd=newHeheDao(); if(_hd.insert(hehe)) System.out.println("成功!"); else System.out.println("失败!"); } }