第一部分代码 (实体类)
Пакет com.wf.entity; открытый класс hehehe {private int hehehe_id; private string hehehe_name; частная строка hehehe_gender; public int gethehe_id () {return hehehe_id;} public void sethehe_id (int heheid) {hehehe_id = heheid; hehehe_name;} public void sethehe_name (String hehename) {hehehe_name = hehename;} public String gethehe_gender () {return hehehe_gender;} public void sethehe_gender (String hehegender) {heHe_gender = HeHegender;第二部分 основан ((增删改查和查唯一)
Пакет com.wf.dao; импорт java.sql.connection; import java.sql.drivermanager; импорт java.sql.preparedstatement; импорт java.sql.resultset; import java.sql.sqlexception; public classao {final static final river = "com.mysql.jdiver; String url = "mysql: // localhost: 3306/mysql"; защищенная статическая конечная строка user = "root"; защищенная статическая конечная строка пароль = "******"; защищенное соединение = null; защищенное подготовлено подготовку Class.forname (драйвер); this.connection = drivermanager.getConnection (url, пользователь, пароль);}/*** 通用的增删改方法* @param sql sql 语句* @param params 参数数组* @return 受影响的行数*/protected int executupdate (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 (classnotfoundexception e) {e.printstacktrace ();} catch (sqlexception e) {e.printstacktrace ();} наконец {this.close ();} return;}/*** 查询结果集的方法 @param sql* @param params* @param* @paras* @param* @param* @param* @param* sql, string [] params) {try {this.getConnection (); this.preparedStatement = this.connection.prepareStatement (sql); if (null! = params) {для (int i = 0; i <params.length; i ++) {this.preparedStatement.setString (i+1, params; } this.Resultset = this.preparedStatement.executequery ();} catch (classnotfoundexception e) {e.printstacktrace ();} catch (sqlexception e) {e.printstacktrace ();}} /*** 查询唯一的结果*@return объект*/Защищенный объект executequeryunique (string sql, string [] params) {object = null; try {this.getConnection (); this.preparedStatement = this.connection.prepareStatement (sql); if (null! {this.preparedStatement.setString (i+1, params [i]);}} this.Resultset = this.preparedStatement.executequery (); if (this.resultset.next ()) obj (SQLexception e) {e.printstackTrace ();} return obj {if (null! = this.ResultSet) this.Resultset.close (); if (null!第三部分 Hehedao
пакет com.wf.dao; import java.sql.connection; import java.sql.preparedStatement; import java.sql.sqlexception; импорт com.wf.entity.hehe; public class hehedao расширяет Fasedao {public boolean inser хе -хе (хе -хе, хе -хе -гендер) значения (?,?) "; string [] params = new String [] {hehehe.gethehe_name (), hehehe.gethehe_gender ()}; return this.executeupdate (sql, params)> 0? Верно: false;}第四部分 测试 test_basedao_insert
Пакет com.wf.test; import com.wf.dao.hehedao; import com.wf.entity.hehe; Hehe (); хе -хе.