第一部分代码(实体类)
package 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;}public void setHehe_id(int heheId){hehe_id=heheId;}public String getHehe_name() {return hehe_name;}public batal sethehe_name (string hehename) {hehe_name = hehename;} public string gethehe_gender () {return hehe_gender;} void sethehe_gender (string hehegender) {hehe_gender = hehegender;}}第二部分 berdasarkan (增删改查和查唯一)
Paket com.wf.dao; impor java.sql.connection; impor java.sql.driverManager; impor java.sql.preparedstatement; impor java.sql.resultset; protected stor (comysqcception; complexcception; class public class class class classedAo {protected static static static static dorongan = "comy. final String URL="mysql://localhost:3306/mysql";protected static final String USER="root";protected static final String PASSWORD="******";protected Connection connection=null;protected PreparedStatement preparedStatement=null;protected ResultSet resultSet =null;protected void getconnection() throws ClassNotFoundException, SQLException{ Class.forname (driver); this.connection = driverManager.getConnection (url, user, password);}/*** 通用的增删改方法* @param sql sql 语句* @param params 参数数组* @return 受影响的行数*/protected int executeupdate (string sql, string [] params) {int result = -1; try; {this.getConnection (); this.preparedStatement = this.connection.prepareStatement (sql); if (null! = params) {for (int i = 0; i <params.length; i ++) {this.prepareStatement.setstring (i+1, params [i]); this.preparedstatement.executeUpdate ();} catch (classnotfoundException e) {e.printstacktrace ();} catch (sqlexcepte e) {e.printStackTrace ();} akhirnya {this.close ();} return;}/*** 查询结果集的方法* @paryam* @paryam (); sql, string [] params) {coba {this.getConnection (); this.preparedStatement = this.connection.preparestateMent (sql); if (null! = params) {for (int i = i <params.length; i ++) {this.preparement.setring (params.length; i ++) {this.preparstatement.setstring (i] {i] {this.spreparement. } this.resultSet = this.preparedstatement.executeQuery ();} catch (classnotfoundException e) {e.printstacktrace ();} catch (sqlexception e) {e.printstacktrace ();}} /*** 查询唯一的结果*@Return objek*/Objek Protected ExecuteQueryUlique (String SQL, String [] params) {objek objek = null; coba {this.getConnection (); this.preparedStatement = this.connection.preparestateMent (sql); if (null! = Params) {for i untuk (sql); if (null! = Params) {for i) (sql); if (null! = Params) {for i untuk (null! {this.preparedstatement.setstring (i+1, params [i]);}} this.ResultSet = this.preparedstatement.executeQuery (); if (this.resultset.next () objek = this.resultset.getObject (1);} catch (classNext (classnext (classNound exceptset.getObject (1);} catch (classnext (classnexound.) ; catch (sqlexception 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 extends BaseDao{public boolean insert(Hehe hehe){String sql="insert into hehe (hehe_name, hehe_gender) nilai (?,?) "; string [] params = string baru [] {hehe.gethehe_name (), hehe.gethehe_gender ()}; kembalikan ini.executeupDate (sql, params)> 0? Benar: false;}第四部分 测试 test_basedao_insert
Paket com.wf.test; import com.wf.dao.hehedao; import com.wf.entity.hehe; kelas publik test_basedao_insert {public static void main (string [] args) {hehe hehe = new Hehe (); hehe.sethehe_name ("个"); hehe.sethehe_gender ("b"); hehedao _hd = hehedao baru (); if (_hd.insert (hehe)) sistem.out.println ("成功!"