第一部分代码 (实体类)
pacote 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;} void public sethehe_id (int heheid) {hehe_id = heteid; hehe_name;} public void sethehe_name (string hehename) {hehe_name = hehename;} public string gethehe_gender () {return hehe_gender;} public void sethehe_gender (string hehegender) {hehender_lender =第二部分 Basedao (增删改查和查唯一)
pacote com.wf.dao; importar java.sql.connection; importar java.sql.driverManager; importar java.sql.preparedStatement; importar java.sql.ResultSet STATSTATS; Final String url = "mysql: // localhost: 3306/mysql"; protegida string final estática user = "root"; protegido estático final string senha = "******"; conexão protegida de conexão = null; protegido preparado estatemente preparado (null; o resultado da classificação do null; null; slightExConstement (). Class.ForName (Driver); this.Connection = driverManager.getConnection (URL, usuário, senha);}/*** 通用的增删改方法* @param sql sql 语句* @param params 参数数组* @return 受影响的行数*/protegido int executeUpdate (string sql, string [] params) {int resultado = -1; tentativa {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 []; this.PreparedStatement.ExecUteUpDate ();} Catch (ClassNotFoundException e) {e.printStackTrace ();} catch (sqLexception e) {e.printStackTrace ();} finalmente {this.close ();} RETRILT; sql, string [] params) {try {this.getConnection (); this.PreparedStatement = this.Connection.Preparestatement (sql); if (null! = params) {para (int i = 0; i <params.length; i ++) {this.SeparedStatement.Setsing.Setsing (i <params.lengt; } this.ResultSet = this.PreparedStatement.ExecuteQuery ();} Catch (ClassNotFoundException e) {E.PrintStackTrace ();} Catch (sqLexception e) {e.printStacktrace ();}} /*** 查询唯一的结果*@return Objeto*/Objeto protegido ExecuteQueryUnique (String sql, String [] params) {objeto objeto = null; tente {this.getConnection (); this.PreparedStatement = this.connection.Preparestatement (sql); se (null! {this.PreparedStatement.SetString (i+1, params [i]);}} this.ResultSet = this.PreparedStatement.executeQuery (); if (this.ResultSet.Noxt ()) object = this.ResultSet.getObject (1);} (ClassNotfound (ClassNotFound ()) (SqLexception e) {e.printStackTrace ();} retornar objeto;} Protected void close () {tente {if (null! = this.resultset) this.resultset.close (); if (null! = this.preparedStatement) this.PreparedStatement.close (); if (null!第三部分 Hehedao
pacote com.wf.dao; importar java.sql.connection; importar java.sql.preparedStatement; importar java.sql.sqLexception; importar com.wf.entity.hehe; classe pública hehedao basends Based {público hehe (hehe_name, hehe_gender) valores (?,?) "; string [] params = new string [] {hehe.gethehe_name (), hehe.gethhehe_gender ()}; return this.executeUpdate (sql, params)> 0? Verdadeiro: false;}第四部分 测试 test_basedAo_insert
pacote com.wf.test; importar com.wf.dao.hehedao; importar com.wf.entity.hehe; public class Test_baskeao_insert {public static void main (string [] args) {hehe hehe = novo Hehe (); hehe.sethehe_name ("个"); hehe.sethehe_gender ("b"); hehedao _hd = new hehedao (); if (_hd.insert (hehe)) system.out.println ("成功!);