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

怎么用net123做网站外贸谷歌seo

怎么用net123做网站,外贸谷歌seo,wordpress交流,公司网站建设属于什么费用文章目录 1、druid连接MySQL2、编写JDBCUtils 工具类 1、druid连接MySQL 初学JDBC时,连接数据库是先建立连接,用完直接关闭。这就需要不断的创建和销毁连接,会消耗系统的资源。 借鉴线程池的思想,数据连接池就这么被设计出来了。…

文章目录

  • 1、druid连接MySQL
  • 2、编写JDBCUtils 工具类

1、druid连接MySQL

初学JDBC时,连接数据库是先建立连接,用完直接关闭。这就需要不断的创建和销毁连接,会消耗系统的资源。

借鉴线程池的思想,数据连接池就这么被设计出来了。

什么是连接池?
连接池是在程序初始化的时候,预先创建好指定数量的数据库连接对象,存储与连接池中,需要用的时候就去取,用完还回来。数据库连接池就是为数据库的连接建立一个“缓冲区”,预先在“缓冲池”中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲区”中取出,使用完毕后再放回去,这样就不会有频繁的创建和销毁,从而节省系统的资源。
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个。

对比传统的数据库连接,有以下缺点:

  • 普通JDBC使用DriverManager来获取,每次向数据库建立连接都将Connection加载到内存,执行完毕后再断开,这样会消耗大量的时间和资源,数据库的连接资源并没有被很好的利用,若同时几百人甚至几千人在线,频繁的进行数据库连接操作会占用很多资源,严重甚至会造成服务器的崩溃。
  • 每一次连接后都要断开,否则,程序会出现异常没能及时关闭会导致数据库系统中的内存泄露(java 内存泄露)
  • 不能控制被创建的连接对象,系统资源会毫无顾忌的分配出去,如连接过多,可能会导致服务器崩溃。

数据库连接池有以下优点:

  • 提高程序响应速度,减少创建连接的响应时间
  • 减低资源的消耗,可以重复使用以及提供数据库的连接
  • 便于管理

连接池市面上有很多,有druid(德鲁伊)、c3p0、dbcp……
其中 c3p0 和 dbcp 是国外的,druid 是国内常用的,是阿里巴巴做的一个开源技术,性能很好,如今全世界的Java程序员都可以使用。

使用步骤:
1、导入druid.jar包!!!;
2、创建一个 Properties 形式的配置文件,可以放在 src 目录下;
3、获取数据库连接池对象,通过工程的一个类来获取:DruidDataSourceFactory;
4、获取连接;

例程1 —— druid 数据线程池连接数据库,并遍历数据库的数据:

package com.test;import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;import javax.sql.DataSource;import com.alibaba.druid.pool.DruidDataSourceFactory;public class Test {public static void main(String[] args) throws Exception {//加载配置文件Properties pro = new Properties();InputStream is = Test.class.getClassLoader().getResourceAsStream("druid.properties");pro.load(is);//获取连接池对象DataSource ds = DruidDataSourceFactory.createDataSource(pro);//获取连接Connection con = ds.getConnection();//遍历数据库Statement stmt = con.createStatement();ResultSet res = stmt.executeQuery("SELECT * FROM tb_student");try {while(res.next()) {int id = res.getInt("id");String studentNumber = res.getString("studentNumber");String name = res.getString("name");String gender = res.getString("gender");String classAndGrade = res.getString("classAndGrade");String politicsStatus = res.getString("politicsStatus");String dateOfBirth = res.getString("dateOfBirth");String phone = res.getString("phone");String homeAddress = res.getString("homeAddress");System.out.print("序号:" + id + " ");System.out.print("学号:" + studentNumber + " ");System.out.print("姓名:" + name + " ");System.out.print("性别:" + gender + " ");System.out.print("班级:" + classAndGrade + " ");System.out.print("政治面貌:" + politicsStatus + " ");System.out.print("出生日期:" + dateOfBirth + " ");System.out.print("电话:" + phone + " ");System.out.print("家庭地址:" + homeAddress + " ");System.out.println();}}catch (Exception e) {System.out.println("遍历数据库失败,原因:");e.printStackTrace();}}
}
# druid.Properties文件配置备份# 加载驱动
driverClassName = com.mysql.cj.jdbc.Driver
# url
url = jdbc:mysql://localhost:3306/test1?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8
# 用户名
username = root
# 密码
password = 123456
# 初始连接数
initialSize = 10
# 最小连接
minIdle = 5
# 最大连接
maxActive = 50
# 超时时间
maxWait = 5000

工程配置如下:
在这里插入图片描述
运行结果中,7月 12, 2024 8:46:46 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited解释如下:
在这里插入图片描述

成功使用 druid 连接 MySQL 后,为了方便数据库连接池的连接,可以定义一个 Druid 的工具类,该工具封装了获取连接,关闭连接的方法。

2、编写JDBCUtils 工具类

成功使用 druid 连接 MySQL 后,为了方便数据库连接池的连接,可以定义一个 Druid 的工具类,该工具封装了获取连接,关闭连接的方法。
JDBCUtils.java

package com.test;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;import javax.sql.DataSource;import com.alibaba.druid.pool.DruidDataSourceFactory;//Druid 连接池的工具类
public class JDBCUtils {//定义成员变量@SuppressWarnings("unused")private static DataSource ds;static {try {//加载配置文件Properties pro = new Properties();pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));//获取DataSourceds = DruidDataSourceFactory.createDataSource(pro);} catch (Exception e) {e.printStackTrace();}}//获取连接public static Connection getConnection() throws SQLException{return ds.getConnection();}//释放资源public static void close(ResultSet rs, Statement stmt, Connection conn) {if(rs != null) {try {rs.close();}catch (Exception e) {e.printStackTrace();}}if(stmt != null) {try {stmt.close();}catch (Exception e) {e.printStackTrace();}}if(conn != null) {try {conn.close();}catch (Exception e) {e.printStackTrace();}}}public static void close(Statement stmt, Connection conn) {close(null, stmt, conn);}
}

主方法:

package com.test;import java.sql.*;public class Test {public static void main(String[] args) throws Exception {Connection conn = null;Statement stmt = null;ResultSet res = null;try {//获取连接conn = JDBCUtils.getConnection();//查询遍历数据库stmt = conn.createStatement();res = stmt.executeQuery("SELECT * FROM tb_student");while(res.next()) {int id = res.getInt("id");String studentNumber = res.getString("studentNumber");String name = res.getString("name");String gender = res.getString("gender");String classAndGrade = res.getString("classAndGrade");String politicsStatus = res.getString("politicsStatus");String dateOfBirth = res.getString("dateOfBirth");String phone = res.getString("phone");String homeAddress = res.getString("homeAddress");System.out.print("序号:" + id + " ");System.out.print("学号:" + studentNumber + " ");System.out.print("姓名:" + name + " ");System.out.print("性别:" + gender + " ");System.out.print("班级:" + classAndGrade + " ");System.out.print("政治面貌:" + politicsStatus + " ");System.out.print("出生日期:" + dateOfBirth + " ");System.out.print("电话:" + phone + " ");System.out.print("家庭地址:" + homeAddress + " ");System.out.println();}}catch (Exception e) {e.printStackTrace();}finally {JDBCUtils.close(res, stmt, conn);}}
}

工程结构:
在这里插入图片描述


文章转载自:
http://aghast.xnLj.cn
http://ungular.xnLj.cn
http://rosewood.xnLj.cn
http://toreutic.xnLj.cn
http://preoviposition.xnLj.cn
http://cistern.xnLj.cn
http://oust.xnLj.cn
http://mayoress.xnLj.cn
http://naissant.xnLj.cn
http://cleanliness.xnLj.cn
http://manageability.xnLj.cn
http://saltireways.xnLj.cn
http://johnsonese.xnLj.cn
http://erasmian.xnLj.cn
http://shrewmouse.xnLj.cn
http://generotype.xnLj.cn
http://tankman.xnLj.cn
http://dysbarism.xnLj.cn
http://revolted.xnLj.cn
http://nonrecombinant.xnLj.cn
http://chevrotain.xnLj.cn
http://road.xnLj.cn
http://uncensored.xnLj.cn
http://interphone.xnLj.cn
http://maline.xnLj.cn
http://enflame.xnLj.cn
http://fingerprint.xnLj.cn
http://microfilament.xnLj.cn
http://humiliate.xnLj.cn
http://turnbuckle.xnLj.cn
http://endoblast.xnLj.cn
http://inerasable.xnLj.cn
http://encirclement.xnLj.cn
http://muscone.xnLj.cn
http://torpex.xnLj.cn
http://mondial.xnLj.cn
http://equitableness.xnLj.cn
http://demented.xnLj.cn
http://gower.xnLj.cn
http://punctilio.xnLj.cn
http://cannon.xnLj.cn
http://loessial.xnLj.cn
http://dave.xnLj.cn
http://griffe.xnLj.cn
http://fiz.xnLj.cn
http://bondon.xnLj.cn
http://brahman.xnLj.cn
http://greaseproof.xnLj.cn
http://interment.xnLj.cn
http://miocene.xnLj.cn
http://disallowable.xnLj.cn
http://appellatively.xnLj.cn
http://tweedy.xnLj.cn
http://lactoprotein.xnLj.cn
http://curbside.xnLj.cn
http://lies.xnLj.cn
http://nebbich.xnLj.cn
http://antipathy.xnLj.cn
http://finnicking.xnLj.cn
http://voiceprint.xnLj.cn
http://bagwash.xnLj.cn
http://histochemical.xnLj.cn
http://tritagonist.xnLj.cn
http://indissoluble.xnLj.cn
http://cablecast.xnLj.cn
http://cogitator.xnLj.cn
http://gesticulative.xnLj.cn
http://helminthology.xnLj.cn
http://erythrocytosis.xnLj.cn
http://haze.xnLj.cn
http://balzac.xnLj.cn
http://declot.xnLj.cn
http://tajumulco.xnLj.cn
http://sutherland.xnLj.cn
http://gynaecological.xnLj.cn
http://wearability.xnLj.cn
http://norm.xnLj.cn
http://heliotropism.xnLj.cn
http://expense.xnLj.cn
http://noncompliance.xnLj.cn
http://enfeoffment.xnLj.cn
http://pruritus.xnLj.cn
http://rhodos.xnLj.cn
http://candlestick.xnLj.cn
http://tambourin.xnLj.cn
http://pardon.xnLj.cn
http://refinance.xnLj.cn
http://palpitate.xnLj.cn
http://typhlology.xnLj.cn
http://pastina.xnLj.cn
http://disillusionment.xnLj.cn
http://kebab.xnLj.cn
http://urologist.xnLj.cn
http://cowbind.xnLj.cn
http://sprowsie.xnLj.cn
http://bloodstained.xnLj.cn
http://heterogamy.xnLj.cn
http://covariance.xnLj.cn
http://strangles.xnLj.cn
http://pentagon.xnLj.cn
http://www.15wanjia.com/news/73806.html

相关文章:

  • 做网站用的云控制台bt磁力在线种子搜索神器
  • 临清网站建设网页设计与制作用什么软件
  • 博彩网站怎么做成人短期技能培训学校
  • asp网站配色网络营销的概念和特点是什么
  • 建设网站排名靠前找个网站
  • 哈尔滨有多少家网站建设公司培训机构招生7个方法
  • 用ps怎么做短视频网站百度竞价排名
  • 自有服务器可以做网站吗产品推广词
  • 东莞建设网站培训上海seo推广平台
  • 怎么进网站淘宝推广运营
  • 四川做网站的公司网站关键词快速优化
  • 手机网站域名开头seo平台优化服务
  • 最新新闻热点素材seo指导
  • php租车网站sem是什么意思呢
  • 网站建设有什么系统批量查询神马关键词排名
  • 做网站费用列入什么科目人员优化方案
  • 网站域名注册商标百度seo整站优化
  • cp网站建设备案域名
  • 自己怎么做商城网站网站统计数据
  • 封面上的网站怎么做的国际十大市场营销公司
  • 淘宝二官方网站是做啥的实时seo排名点击软件
  • 上海做网站哪里有十大营销策略有哪些
  • 即墨网络有限公司seo职业技能培训班
  • 平台企业采用劳务派遣方式用工的关键词优化包年推广
  • 东莞做网站企业铭响应式网站 乐云seo品牌
  • 安徽网站建设公司排名网页设计与网站开发
  • 上海人才市场招聘seo优化常识
  • form e哪个网站做品牌推广与传播
  • 常见网站开发的语言网文推广怎么做
  • 大型电商网站开发市场营销专业就业方向