第一部分代码 (实体类)
حزمة com.wf.entity ؛ الطبقة العامة hehe {private int hehe_id ؛ سلسلة خاصة hehe_name ؛ سلسلة خاصة hehe_gender ؛ public int gethehe_id () {return hehe_id ؛} public void sethehe_id (int heheid) {hehe_id = hheid ؛ sethehe_name (String hehename) {hehe_name = hehename ؛} السلسلة العامة gethehe_gender () {return hehe_gender ؛} public void sethehe_gender (String hehegender) {hehe_gender = hehgender ؛}}}第二部分 على أساس (增删改查和查唯一)
package com.wf.dao ؛ استيراد java.sql.connection ؛ استيراد java.sql.drivermanager ؛ استيراد java.sql.preparedstatement ؛ استيراد java.sql.resultset ؛ url url = "mysql: // localhost: 3306/mysql" ؛ محمية static string user = "root" ؛ محمية static final string password = "******" ؛ اتصال محمي = null ؛ محمية classStateStement = null ؛ resultset resultset = null ؛ void getConnection () classnotembere ، sqlexpoundation et it led. class.forname (driver) ؛ this.connection = drivermanager.getConnection (url ، المستخدم ، كلمة المرور) ؛}/*** 通用的增删改方法* param sql sql 语句* param params 参数数组* regurn 受影响的行数*/protected int executeupdate (String sql ، string [] params) {this.getConnection () ؛ this.preparedStatement = this.connection.preparestatement (sql) ؛ if (null! = params) {for (int i = 0 ؛ i <params.length ؛ i ++) {this.preparedStateMement.SetString (i+1 ، params [i]) this.preparedstatement.executeupdate () ؛} catch (classnotfoundException e) {E.PrintStackTrace () ؛} catch (sqlexception e) {e.printstacktrace () ؛} أخيرًا {this.close () ؛} sql ، string [] params) {try {this.getConnection () ؛ this.prepedStatement = this.connection.preparestatement (sql) ؛ if (null! = params) {for (int i = 0 ؛ i <params.length ؛ i ++) } this.resultset = this.preparedStatement.executequery () ؛} catch (classnotfoundException e) { /*: {this.preparedStatement.SetString (i+1 ، params [i]) ؛}} this.resultset = this.prepedStateMent.executequery () ؛ if (this.resultset.next {E.PrintStackTrace () ؛} كائن الإرجاع ؛} محمي void close () {try {if (null! = this.resultset) this.resultset.close () ؛ if (null! = this.prepedstatement) this.preparedStateMent.close () ؛ {E.PrintStackTrace () ؛}}第三部分 هيداو
package com.wf.dao ؛ استيراد java.sql.connection ؛ استيراد java.sql.prepedstatement ؛ استيراد java.sql.sqlexception ؛ استيراد com.wf.entity.hehe ؛ القيم (؟ ،؟) "؛ string [] params = new string [] {hehe.gethehe_name () ، hehe.gethehe_gender ()} ؛ إرجاع this.executeupdate (sql ، params)> 0؟ صحيح: خطأ ؛}第四部分 测试 test_basedao_insert
package com.wf.test ؛ import com.wf.dao.hehedao ؛ import com.wf.Entity. _hd = new hehedao () ؛ if (_hd.insert (hehe)) system.out.println ("成功!") ؛ elsesystem.out.println ("失败!") ؛}}