第一部分代码(实体类)
Paket com.wf.entity; öffentliche Klasse hehe {private int hehe_id; private string hehe_name; Sethehe_name (String heHename) {hehe_name = heHename;} public String gethe_gender () {return hehe_gender;} public void SetheHe_gender (String Hehegender) {hehe_gender = hehegender;}}}}}}}}}}}}}}}}}}}第二部分 basiertao (增删改查和查唯一) (增删改查和查唯一)
Paket com.wf.dao; import Java.sql.Connection; import Java.sql.drivermanager; Import Java.SQL.PrepararedStatement; String url = "mysql: // localhost: 3306/mysql"; Protected Static Final String user = "root"; Protected Static Final String password = "******"; geschützte Verbindung. Class.Forname (Treiber); this.connection = driverManager.getConnection (URL, Benutzer, Passwort);}/*** 通用的增删改方法* @param SQL SQL 语句* @param Params 参数数组* @return 受影响的行数*/protected int executeUpdate (String SQL, String [] Params) {int result = -1; {this.getConnection (); this.PreeParedStatement = this.connection.Preparestatement (sql); if (null! this.preparedStatement.executeUpdate();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally{this.close();}return result;}/*** 查询结果集的方法* @param sql* @param params*/protected void executeQuery(String sql, string [] params) {try {this.getConnection (); this.PreeParedStatement = this.connection.Preparestatement (SQL); if (null! } this.resultset = this.PreeParedStatement.executeQuery ();} catch (classNotFoundException e) {e.printstacktrace ();} catch ( /*** 查询唯一的结果*@return Object*/Protected Object executeQueryUnique (String SQL, String [] Params) {Object Object = null; try {this.getConnection (); this.PreeParedStatement = this.connection.preparestatement (SQL); if (null! {this.PreeParedStatement.setString (i+1, params [i]);}} this.resultset = this.PreeParedStatement.executeQuery (); if (this.resultSet.Next ()) Object = this.Resultset.getObject (1); e) {e.printStacktrace ();} return -Objekt;} protected void close () {try {if (null! e) {e.printstacktrace ();}}第三部分 Hehedao
Paket com.wf.dao; import Java.sql.connection; Import Java.sql.prepararedStatement; Import Java.Sql.SqLexception; Werte (?,?) "; String [] params = new String [] {hehe.gethehe_name (), hehe.gethehe_gels ()}; Richtig: false;}第四部分 测试 test_basedao_insert
Paket com.wf.test; import com.wf.dao.hehedao; import com.wf.entity.heehe; _hd = new hehedao (); if (_hd.insert (hehe) system.out.println ("成功!"); ElseSystem.out.println ("失败!");}}