时间:2021-05-20
第一部分代码(实体类)
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 void setHehe_name(String heheName) {hehe_name = heheName;}public String getHehe_gender() {return hehe_gender;}public void setHehe_gender(String heheGender) {hehe_gender = heheGender;}}第二部分 BaseDao(增删改查和查唯一)
package com.wf.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class BaseDao{protected static final String DRIVER="com.mysql.jdbc.Driver";protected static 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.preparedStatement.setString(i+1, params[i]);} }result= 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.preparedStatement=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 Object*/protected Object executeQueryUnique(String sql,String[]params){Object object=null;try {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.resultSet=this.preparedStatement.executeQuery();if(this.resultSet.next())object=this.resultSet.getObject(1);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return object;}protected void close(){try {if(null!=this.resultSet)this.resultSet.close();if(null!=this.preparedStatement)this.preparedStatement.close();if(null!=this.connection)this.connection.close();} 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) values(?,?)" ;String []params=new String[]{hehe.getHehe_name(),hehe.getHehe_gender()};return this.executeUpdate(sql, params)>0? true:false;}第四部分 测试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("b");HeheDao _hd=new HeheDao();if(_hd.insert(hehe))System.out.println("成功!");elseSystem.out.println("失败!");}}声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
用AngularJS实现对表格的增删改查(仅限前端),具体代码:实现表格的增删改查.add{position:relative;top:-40px;left:1
复制代码代码如下:extJs中常用到的增删改查操作的示例代码
Java连接MongoDB进行增删改查的操作1.创建数据库的连接,进行增删改查(分别为接口和实现类)packagecom.dao;importjava.util
ORM对象关系映射在数据库中,实现对数据的增删改查,使用的是SQ语句,在django中,通过python代码,实现对数据库的增删改查,这就是ORM。在pytho
增删改查是对数据库最基本的操作,下面详细为大家介绍下如何连接数据库以及增删改查等等相关知识。 1.链接数据库通用方法:conn.php 代码如下: 2