第一部分代码(实体类)
パッケージ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;} public string gethehe_name()heheid;} 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 (增删改查和查唯一)
パッケージcom.wf.dao;インポートjava.sql.connection; import java.sql.drivermanager; import java.sql.preparedStatement; Import java.sql.Resultset; Import java.sql.sqlexception; final string url = "mysql:// localhost:3306/mysql"; protected static final string user = "root";保護された静的なstring password = "******";保護された接続接続= null;保護された準備整理= null; class.forname(ドライバー); this.connection = drivermanager.getConnection(url、user、password);}/***通用的增删改方法* @param sql sql {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.preparedStatement.executeUpdate();} catch(classNotFoundException e){e.printstacktrace();} catch(sqlexStacktrace();} fullly {this.close(} return result;}/*** sql、string [] params){try {this.getConnection(); this.preparedStatement = this.connection.preparestatement(sql); if(null!= params){for(int i = 0; i <params.length; i ++){this.preparedtatementmentement.setString(i+1、params [i]; } this.Resultset = this.preparedStatement.executequery();} catch(classNotFoundException e){e.printstacktrace();} catch(sqlexception e){e.printstacktrace();}}} /***查询唯一的结果*@return object*/保護されたオブジェクトExecuteQueryUnique(string sql、string [] params){object object = null; try {this.getConnection(); this.preparedStatement = this.connection.preparestatement(sql); if(null!= params){int i = 0; i <parms.lent; i {this.preparedStatement.setString(i+1、params [i]);}} this.resultset = this.preparedStatement.executequery(); if(this.resultset.next())object = this.resultset.getObject(1);} catch(classnotfoundecseption e); {e.printstacktrace();} return object;} protected void close(){try {if(null!= this.resultset)this.resultset.close(); if(null!= this.preparedStatement)this.preparedstatement.close(); if() {e.printstacktrace();}}hehedao
パッケージcom.wf.dao;インポートjava.sql.connection; import java.sql.preparedStatement; import java.sql.sqlectecete; import com.wf.entity.hehe; public class hehedao extendas basedao {hehe hehe){hehe sql = "sinr hehe(hehe_name、hehe_gender)values(?、?) "; string [] params = new String [] {hehe.gethehe_name()、hehe.gethehe_gender()}; true:false;}第四部分test_basedao_insert
パッケージcom.wf.test;インポートcom.wf.dao.hehedao;インポート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.out.println( "