((()
包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 string [] args [] hehe();