第一部分代码) (实体类)
package com.wf.entity; public class hehe {private int hehe_id; 개인 문자열 hehe_name; 개인 문자열 hehe_gender; public int gethehe_id () {return hehe_id;} public void sethehe_id (int heheid) {hehe _id = heheid;} retring gethehe_name () 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;}}짐
패키지 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 strestic {com.mysql.dc.drication "; 최종 문자열 URL = "mysql : // localhost : 3306/mysql"; 보호 된 정적 최종 문자열 user = "root"; 보호 된 정적 최종 문자열 암호 = "******"; 보호 된 연결 연결 = null; 보호 된 준비된 상태 준비 = null; 보호 된 결과 세트 세트 = null; class.forname (드라이버); this.connection = drivermanager.getConnection (url, user, password);}/*** 通用的增删改方法* @param sql sql 语句* @param params 参数数组* @return 受影响的行数*/protected int executeUpdate (String sql, String [] params) {int result = -1; {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 (classNotFoundExcept e) {e.printstacktrace ();} catch (sqlexception e) {e.printstacktrace ();} 최종적 {this.close ();} return result;}* @param sql* @param params*/parected voidected void*/parected voide. 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! = params). {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); (sqlexception e) {e.printstacktrace ();} return object;} protected void close () {try {if (if (ull! = this.resultset) this.resultset.close (); if (null! = this.preparedStatement) this.preparedStatement.close (); if (null! = this.connection) this.connection.close () catch (sqlexcection e) {e.printstacktrace ();}}Hehedao
패키지 com.wf.dao; import java.sql.connection; import java.sql.preparedStatement; import java.sql.sqlexception; import com.wf.entity.hehe; 공개 클래스 Hehedao {hehe hehe) {hehe hehe) {hehe hehe). hehe (hehe_name, hehe_gender) value (?,?) "; string [] params = new String [] {hehe.gethehe.gethehe_name (), hehe.gethehe_gender ()}; return this.executeUpdate (sql, params)>? 참 : 거짓;}第四部分 第四部分 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 ( "成功!"); elsessystem.out.