当前位置: 首页 > news >正文

论文中网站数据如何做脚注营销型企业网站制作

论文中网站数据如何做脚注,营销型企业网站制作,网站开发设计南邮,手机网站制作移动高端网站建设SQL通用方法 一、常规方法增删改查二、具体优化步骤1.准备工作2.getcon()方法,获取数据库连接对象3.closeAll()方法,关闭所有资源4.通用的增删改方法5.通用的查询方法6.动态查询语句 总结 一、常规方法增删改查 在常规方法中,我们在Java中对…

SQL通用方法

  • 一、常规方法增删改查
  • 二、具体优化步骤
    • 1.准备工作
    • 2.getcon()方法,获取数据库连接对象
    • 3.closeAll()方法,关闭所有资源
    • 4.通用的增删改方法
    • 5.通用的查询方法
    • 6.动态查询语句
  • 总结


一、常规方法增删改查

在常规方法中,我们在Java中对数据库中的表做增删改查操作的时候会发现,代码的答题都大致相同,应该可以将代码优化成一个通用的方法,首先我们要知道增删改要使用到Connection和PreparedStatement两个对象,而查找需要再加一个ResultSet对象,从这里出发我们开始写出一个通用的增删改方法,一个查询方法

二、具体优化步骤

1.准备工作

我们在优化前,需要将一个重复使用的数据写进属性文件中,以便多次使用,提高代码重用率
在src下新建一个包,叫做config,在config包中新建一个文件,jdbc.properties,
在这里插入图片描述
写上常规方法中需要用到的几个数据

2.getcon()方法,获取数据库连接对象

getCon方法,获取数据库连接对象,代码如下

//获得数据库连接对象public Connection getcon() {Properties pro = null;FileReader fr = null;Connection con = null;try {pro = new Properties();			//类加载器//相对地址转换成绝对地址String str = DBHelper.class.getClassLoader().getResource("config/jdbc.properties").getPath();fr = new FileReader(str);		//读取属性文件中的数据pro.load(fr);					} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}try {Class.forName(pro.getProperty("mysql.classname"));String username = pro.getProperty("mysql.username");String password = pro.getProperty("mysql.password");String url = pro.getProperty("mysql.url");con = DriverManager.getConnection(url, username, password);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return con;			//返回值为Connection类型,在通用方法中调用这个getcon方法,拿到数据库连接对象后继续操作}

3.closeAll()方法,关闭所有资源

代码如下

public void closeAll(Connection con, PreparedStatement ps, ResultSet rs){try {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (con != null) {con.close();}} catch (SQLException e) {e.printStackTrace();}}

有了这个方法,在我们执行完通用方法之后就不再需要写很多行代码去关闭资源了

4.通用的增删改方法

//通用的增删改方法public int update(String sql,Object...objects){int i = 0;Connection con = null;PreparedStatement ps = null;try {con = getCon();//调用getcon,获得数据库连接对象ps = con.prepareStatement(sql);//向传入的sql语句中的问号赋值for (int j = 0; j < objects.length; j++) {ps.setObject(j + 1, objects[j]);}i = ps.executeUpdate();//执行sql语句并获得结果} catch (SQLException e) {e.printStackTrace();}finally {closeAll(con,ps,null);}return i;//返回执行后的结果以供下一步差异化操作}

5.通用的查询方法

public ArrayList query(String sql,Class cla,Object...objs){ArrayList list = null;Connection con = null;PreparedStatement ps = null;ResultSet rs = null;try {con = getcon();ps = con.prepareStatement(sql);for (int i = 0; i < objs.length; i++) {ps.setObject(i + 1, objs[i]);//给sql语句中的问号赋值}rs = ps.executeQuery();//执行sql语句进行查找while (rs.next()) {//遍历查找到符合条件的对象Object o = cla.newInstance();//通过反射得到一个新的对象Field[] declaredFields = cla.getDeclaredFields();//利用反射获取对象成员的所有属性放到数组中for (Field f : declaredFields) {//利用强烈for循环遍历数组f.setAccessible(true);//暴力破解权限f.set(o, rs.getObject(f.getName()));//对新的对象的每一个属性赋值,赋的是查找到的对象的值}list.add(o);//将新的对象添加到集合中,开始对下一个符合条件的对象进行操作}} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}finally {closeAll(con,ps,rs);}return list;//等对所有符合条件的对象都操作完,把这个集合返回出去,等待下一步使用}

6.动态查询语句

我们要知道不同功能的操作的原因是sql语句不同,而查找的sql语句偏偏是一个不能修改的String类型的,我们很难附加一些缩小范围的查询条件,所以动态查询需要用到可变数组,以下代码是在数据层实现类中书写的
以DVD项目做例子,DVD有六个属性,分别是下面代码体现的这些,我们通过传参,传进来一个DVD对象,这个时候我们先写一个可变数组where后面 1 = 1是恒成立的意思,无论后面加不加,加什么都会执行,接下来我们对dvd的各个属性进行判断,如果不为空,那么就代表需要根据这条属性来判断,我们就在sql语句中加上该条件,接着我们在集合中添加这条属性。最后我们再调用通用的查询方法,进行一个动态查询,其实到这里还没结束,需要在控制层再写一次差异化,如果输入一个什么东西,或者下拉勾选需要查询的属性,比如下面第二段演示的代码:

public ArrayList<DVD1> selectDVDs(DVD1 dvd) {StringBuilder sql = new StringBuilder("select * from dvd where 1 = 1");ArrayList list = new ArrayList();if (dvd.getId() != null){sql.append("and id = ?");list.add(dvd.getId());}if (dvd.getName() != null){sql.append("and name = ?");list.add(dvd.getName());}if (dvd.getState() != null){sql.append("and state = ?");list.add(dvd.getState());}if (dvd.getLendDate() != null){sql.append("and lendDate = ?");list.add(dvd.getLendDate());}if (dvd.getMoney() != null){sql.append("and money = ?");list.add(dvd.getMoney());}if (dvd.getCount() != null){sql.append("and count = ?");list.add(dvd.getCount());}return db.query(sql.toString(),DVD1.class,list.toArray());}

public void see(){DVD dvd=new DVD();System.out.println("请输入查看DVD的编号:(输入N,不按照此条件查询)");String id = input.next();if (!"N".equals(id))dvd.setId(Integer.parseInt(id));System.out.println("请输入查看DVD的名称:(输入N,不按照此条件查询)");String name = input.next();if (!"N".equals(name))dvd.setName(name);System.out.println("请输入查看DVD的状态:(输入N,不按照此条件查询)");String state = input.next();if (!"N".equals(state))dvd.setState(state);System.out.println("请输入查看DVD的日租金:(输入N,不按照此条件查询)");String money = input.next();if (!"N".equals(money))dvd.setMoney(Double.parseDouble(money));System.out.println("请输入查看DVD的借出日期:(输入N,不按照此条件查询)");String date = input.next();if (!"N".equals(date))dvd.setLendDate(date);System.out.println("请输入查看DVD的出借次数:(输入N,不按照此条件查询)");String count = input.next();if (!"N".equals(count))dvd.setCount(Integer.parseInt(count));ArrayList<DVD> dvds = dvdService.findDVDs(dvd);System.out.println("序号\t\t名字\t\t状态\t\t日租金\t\t借出日期\t\t出借次数");for (DVD d : dvds) {System.out.println(d.toString());}}

这样我们就实现了对某一张表进行一个动态查询的操作


总结

以上就是sql语句的通用方法,真~通用,感觉对你有益的话点个收藏吧,防止用的时候找不到!!!


文章转载自:
http://dinncomacrography.tpps.cn
http://dinncotelekinesis.tpps.cn
http://dinncodpl.tpps.cn
http://dinncoprobusing.tpps.cn
http://dinncocoot.tpps.cn
http://dinncoejectable.tpps.cn
http://dinncofloatable.tpps.cn
http://dinncoFALSE.tpps.cn
http://dinncobunghole.tpps.cn
http://dinncoilp.tpps.cn
http://dinncoimpersonalise.tpps.cn
http://dinncobrokenhearted.tpps.cn
http://dinncomusquash.tpps.cn
http://dinncoocelot.tpps.cn
http://dinncoted.tpps.cn
http://dinncogranolithic.tpps.cn
http://dinncoaugment.tpps.cn
http://dinncobromelia.tpps.cn
http://dinncocouncilor.tpps.cn
http://dinncorevue.tpps.cn
http://dinncoseminivorous.tpps.cn
http://dinncoadnex.tpps.cn
http://dinncogunyah.tpps.cn
http://dinncomechanotheropy.tpps.cn
http://dinncoencrimson.tpps.cn
http://dinncopluckless.tpps.cn
http://dinncoskiffle.tpps.cn
http://dinncoverderer.tpps.cn
http://dinncobisulphide.tpps.cn
http://dinncotransmission.tpps.cn
http://dinncoantirattler.tpps.cn
http://dinncoinhomogeneous.tpps.cn
http://dinncoattemperator.tpps.cn
http://dinncostalinsk.tpps.cn
http://dinncorabbinist.tpps.cn
http://dinncodeuton.tpps.cn
http://dinncotransfiguration.tpps.cn
http://dinncounderpitch.tpps.cn
http://dinncorumpty.tpps.cn
http://dinncolegendist.tpps.cn
http://dinncoaccept.tpps.cn
http://dinncostalin.tpps.cn
http://dinncoabsorbingly.tpps.cn
http://dinncouncircumcised.tpps.cn
http://dinncosuperstition.tpps.cn
http://dinncoallostery.tpps.cn
http://dinncofortnight.tpps.cn
http://dinncoliterality.tpps.cn
http://dinncostonecutter.tpps.cn
http://dinncofrostbite.tpps.cn
http://dinncoclunch.tpps.cn
http://dinncoquercitrin.tpps.cn
http://dinncofissureless.tpps.cn
http://dinncoimagist.tpps.cn
http://dinncojap.tpps.cn
http://dinncowittily.tpps.cn
http://dinncokickball.tpps.cn
http://dinncopugilistic.tpps.cn
http://dinncoredder.tpps.cn
http://dinncolepidopteran.tpps.cn
http://dinnconuciform.tpps.cn
http://dinncoperdurability.tpps.cn
http://dinncoclaybank.tpps.cn
http://dinncolustrine.tpps.cn
http://dinncogangdom.tpps.cn
http://dinncoernet.tpps.cn
http://dinncogadgeteer.tpps.cn
http://dinncoheteromorphic.tpps.cn
http://dinncointerdict.tpps.cn
http://dinncowhosever.tpps.cn
http://dinnconope.tpps.cn
http://dinncocopyreader.tpps.cn
http://dinncogrike.tpps.cn
http://dinncoradioelement.tpps.cn
http://dinncofireclay.tpps.cn
http://dinncokikongo.tpps.cn
http://dinncoyoungly.tpps.cn
http://dinncodicast.tpps.cn
http://dinncograndchild.tpps.cn
http://dinncouncompassionate.tpps.cn
http://dinncoembourgeoisification.tpps.cn
http://dinnconailhead.tpps.cn
http://dinncolognitudinal.tpps.cn
http://dinncoantilepton.tpps.cn
http://dinncouncommonly.tpps.cn
http://dinncohomorganic.tpps.cn
http://dinncochuse.tpps.cn
http://dinncohydrosol.tpps.cn
http://dinncowonderful.tpps.cn
http://dinncoweltbild.tpps.cn
http://dinncoexotic.tpps.cn
http://dinncomarquisate.tpps.cn
http://dinncodelaine.tpps.cn
http://dinncodysgenic.tpps.cn
http://dinncohorripilate.tpps.cn
http://dinnconobby.tpps.cn
http://dinncoslumland.tpps.cn
http://dinncotrippingly.tpps.cn
http://dinncohero.tpps.cn
http://dinncolucknow.tpps.cn
http://www.dinnco.com/news/130173.html

相关文章:

  • php做网站导购网站排名怎么优化
  • 织梦网站如何做伪静态软文推广软文营销
  • 网站主题有哪些内容阜新网络推广
  • 秦皇岛市中医医院seo优化网站查询
  • linux建网站百度官网
  • 网站建设维护内容深圳百度seo哪家好
  • web网站开发实例下载seo企业优化方案
  • 网站美工主要工作是什么百度明星人气榜入口
  • 日本公司招聘网站关键词排名优化软件价格
  • 网站工信部实名认证抖音seo公司
  • 找做网站技术人员百度上海总部
  • 学校网站源码 带wap手机端网络营销自学网站
  • 手机网站css网店推广方法
  • 网站建设后期怎样维护杭州关键词自动排名
  • 网站接入百度地图象山seo外包服务优化
  • 达州做网站的公司有哪些四川游戏seo整站优化
  • wordpress 镜像下载江阴网站优化公司
  • 怎么访问日本竹中建设网站交换友情链接
  • 长安营销型网站建设免费建自己的网址
  • 青岛网站建设公司百度手机助手下载2022官方正版
  • 网站内容建设的原则好消息疫情要结束了
  • 江苏水利建设网站广东知名seo推广多少钱
  • 用php做网站流程定制网站开发
  • 织梦网站源码好吗seo站外推广有哪些
  • icp备案网站接入信息百度搜索引擎地址
  • 网站开发公司需要那些硬件设备关键词seo优化公司
  • 湖南长沙网站制作手机网站制作
  • wordpress envato主题哈尔滨seo优化培训
  • 太仓做网站的互联网seo是什么
  • 网站设计建设方案西安seo诊断