第一部分代码 (实体类 实体类
package com.wf.entity; classe publique hehe {private int hehe_id; chaîne privée hehe_name; chaîne privée hehe_gender; public int gethehe_id () {return hehe_id;} public void sethehe_id (int heheid) {hehe_id = heheid;} public string gethehe_name () {return hehe_name;} Sethehe_name (String hehename) {hehe_name = hehename;} public String gethehe_gender () {return hehe_gender;} public void Sethehe_gender (String hehegender) {hehe_gender = hehegeder;}}第二部分 Basedao (增删改查和查唯一)
Package com.wf.dao; Importer java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultSet; Url de chaîne finale statique = "mysql: // localhost: 3306 / mysql"; chaîne finale statique protégée user = "root"; chaîne finale statique protégée par mot de passe = "******"; connexion protégée Connexion = null; Protected préparent préparé () lull; Class.forname (pilote); this.connection = driverManager.getConnection (URL, utilisateur, mot de passe);} / *** 通用的增删改方法 * @param sql sql 语句 * @param params 参数数组 * @return 受影响的行数 * / protégé int execUteUpDate (String sql, string [] params) {int result = -1; {this.getConnection (); this.PreparedStationment = this.connection.preparestatement (sql); if (null! = params) {for (int i = 0; i <params.length; i ++) {this.preparedstatement.setsstring (i + 1, params [i]);}} résultat = résultat = this.preparedstatement.executeupdate ();} catch (classNotFoundException e) {e.printStackTrace ();} Catch (sqlexception e) {e.printStackTrace ();} Enfin {this.close ();} Retour Résultat;} / *** 查询结果集的方法 * @param sql * @Param PARAMS * sql, string [] params) {try {this.getConnection (); this.preparedStation = 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 objet * / objet protégé EXECUTEQUERYUnique (String Sql, String [] Params) {objet objet = null; try {this.getConnection (); this.preparedStatement = this.connection.prepareStatement (sql); if (null! = Params) {pour (int i = 0; i <A paramshing; i ++) {pour (int i = 0; {this.preparedstatement.setstring (i + 1, params [i]);}} this.resultSet = this.preparedstatement.executequery (); if (this.resultSet.next ()) objet = this.resultSet.getObject (1);} Catch (ClassNotFoundException e) {E.PrintStackTrace ();} e) {e.printStackTrace ();} Return Object;} Protected void closed () {try {if (null! = this.resultSet) this.resultSet.close (); if (null! = this.preparedstatement) this.preparedstatement.close (); if (null! = this.connection) this.connection.close (); 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 étend basasedao {public boolean insert (hehe hlehe) {string sql = "insert in hehe (hehe_name, hehe) {string sql =" insert in hehe (hehe_name, hehe) {string sql = "insert in hehe (hehe_name, hehe) {String sql =" insert in hehe (hehe_name, hehe) {String sql = "insert in hene (hehe valeurs (? ,?) "; String [] params = new String [] {hehe.gethehe_name (), hehe.gethehe_gender ()}; return this.executeupdate (sql, params)> 0? vrai: faux;}第四部分 测试 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 (ba "); heheda; _hd = new Hehedao (); if (_hd.insert (hehe)) System.out.println ("成功!"); elsesystem.out.println ("失败!");}}