第一部分代码(实体类)
paquete coma com.wf.entity; public class jhehe {private int jhehe_id; private String jehe_name; private string jehe_gender; public int gethehe_id () {return jehe_id;} public void void setheheHe_id (int jeheid) {hehe_id = heheid; jehe_name;} public void sethehe_name (string jehename) {jehe_name = hehename;} public string gethehe_gender () {return jehe_gender;} public void sethehe_gender (string hehegender) {hehe_gender = hehegender;}}}}第二部分 Basado (增删改查和查唯一) (增删改查和查唯一)
paquete com.wf.dao; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedStatement; import java.sql.ressset; import java.sql.sqlexception; clase pública buildizada {Cadena final de finalización estatica protegida = "com.mysql.jdbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc.dbc. Cadena url = "mysql: // localhost: 3306/mysql"; static static final string user = "root"; static static final string contraseña = "********"; conexión protegida Connection = null; protegido preparado que se prepara preparado = null; resultado protegido resultset = null; protegido vaciConnection () Suelta classNotFoundException, sqlexceptualception, sqlexception { Class.forname (controlador); this.connection = drivermanager.getConnection (url, user, contraseña);}/*** 通用的增删改方法* @param sql sql 语句* @param params 参数数组* @return 受影响的行数*/protegido int ejecututeUpdate (string sql, string [] params) {int result = -1; intent {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]);}}} resultado = = this.preparedStatement.ExeCuteUpDate ();} catch (ClassNotFoundException e) {E.PrintStackTrace ();} Catch (SqLException e) {E.PrintStackTRace ();} Finalmente {this.Close ();} return Result;}/*** 查询结果集的方法* @param sql* @param parámetos*/protegido VoiDECETECT sql, string [] params) {try {this.getConnection (); this.preparedStatement = this.connection.preparestatement (sql); if (null! = params) {for (int i = 0; i <ams.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); if (null! = Params) {int i = 0; i <i <params.length; i ++); i ++) {this.preparedStatement.setString (i+1, params [i]);}} this.ResultSet = this.preparedStatement.ExeCuteQuery (); if (this.resultSet.next ()) objeto = this.ResultSet.getObject (1);} Catch (classNotFoundException e) {E.PrintStActActace (); e) {E.PrintStackTrace ();} return Object;} protegido void Close () {try {if (null! = this.resultSet) this.resultSet.close (); if (null! = this.preparedStatement) this.preparedStatement.close (); if (null! e) {E.PrintStackTrace ();}}第三部分 hehedao
paquete com.wf.dao; import java.sql.connection; import java.sql.preparedStatement; import java.sql.sqlexception; import com.wf.entity.hehe; public class Hehedao extiende a BuildAo {Public Boolean Insert (hehe hehe) {String sql = "Insert in ine into in ine into inional inionalionalionalionalionalionionalionionalionionalionionalionionalionionalionionalionionalionionalionionalionionalionalionaltrionionalionalionalionalionalionalionalionalionalionalionalionalionalionionionionionalionionalionalionalionalionalionalional jeje (jehe_name, jehe_gender) valores (?,?) "; string [] params = new String [] {jehe.gethehe_name (), jehe.gethehe_gender ()}; devuelve esto.executeUpdate (sql, params)> 0? verdadero: falso;}第四部分 测试 test_basedao_insert
paquete com.wf.test; import com.wf.dao.hehedao; import com.wf.entity.hehe; public class test_basedao_insert {public static void main (string [] args) {jeje jeje = nuevo Jehe (); jeje.sethehe_name ("个"); jeje.sethehe_gender ("b"); jehedao _hd = new hehedao (); if (_hd.insert (jehe)) system.out.println ("成功!"); Elsesystem.out.println ("") 失败! ")