((()
包com.wf.entity; public class hehe {private int int hehe_id; private string hehe_name; private string hehe_gender; public int int int int int int int int int int int int int int int gethehe_id(){return hehe_id;} public void sethehe_id sethehe_id(int heheid(int heheid) hehe_name;} public void sethehe_name(字符串hehename){hehe_name = hehename;} public string gethehe_gender(){return hehe_gender;} public void sethehe_gender(string hehegender(string hehegender)第二部分基于基础(增删改查和查唯一)
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静态最终字符串url =“ mysql:// localhost:3306/mysql”;受保护的静态最终字符串用户=“ root”;受保护的静态最终字符串密码=“ *******”;受保护的连接= null; protected procked pregend statement pregendstatement pregendStatement pregendSetement = null; class.forname(驱动程序); this.connection = drivermanager.getConnection(url,用户,密码);}/***通用的增删改方法* @param sql sql sql语句* @param params参数数组* @return @return受影响的行数*/protected int executeUpdate(string scl,string sql,string,strings strips = -1 params) {this.getConnection(); this.preparedStatement = this.connection.preparestatement(sql); if(null!= params){for(int i = 0; i <params.length; i <paramsement; i ++) this.preparedstatement.executeupdate();} catch(classNotFoundException e){e.printStackTrace();} catch(sqlexception e){e.printstacktrace();}最后sql,string [] params){try {try {this.getConnection(); this.preparedStatement = this.connection.preparestatement(sql); if(null!= params){for(int i = 0; i = 0; i <params.length; i ++; i ++) } this.ResultSet = this.preparedstatement.executequery();} catch(classNotFoundException e){e.printstacktrace();} catch(sqlexception e){e.printstacktrace() /***查询唯一的结果*@return对象*/受保护的对象executequeryunique(字符串sql,string [] params){object object = null; try {this.getConnection(); this.preparedStatement = this.connection.connection.preparestatement(sql); if(null!params) {this.preparedStatement.SetString(i+1,params [i]);}} this.ResultSet = this.preparedStatement.executequery(); if(this.ResultSet.Next() (sqlexception e){e.printStackTrace();}返回对象;} protected void closed(){try {if(null!= this.ResultSet)this.resultset.close(); if(null!= this.preparedStatement)this.preparedStatement.close.close.close.close(null!捕获(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()};返回this.executeUpdate(sql,params)> 0? true:false;}第四部分测试test_basedao_insert
软件包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();