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

南宁制作网站服务商陕西seo快速排名

南宁制作网站服务商,陕西seo快速排名,wordpress防止篡改文件linux,宝塔面安装wordpress目录 ♫什么是JDBC ♫JDBC常用接口和类 ♪Connection接口 ♪Statement对象 ♪ResultSet对象 ♫JDBC的使用 ♪添加“驱动包” ♪创建数据源,描述数据库服务器在哪 ♪和数据库服务器建立连接 ♪构建SQL语句 ♪执行SQL语句 ♪释放资源 ♫什么是JDBC 我们前面操…

目录

♫什么是JDBC

♫JDBC常用接口和类

♪Connection接口

♪Statement对象

♪ResultSet对象

♫JDBC的使用

♪添加“驱动包”

♪创建数据源,描述数据库服务器在哪

♪和数据库服务器建立连接

♪构建SQL语句

♪执行SQL语句

♪释放资源


♫什么是JDBC

我们前面操作数据库都是使用数据库自带的客户端通过网络访问数据库服务器,而要想通过代码来访问数据库服务器就需要自己实现一个数据库客户端(各种数据库都提供了一系列API可以使我们较为方便地实现一个客户端),而由于不同数据库提供的API不同,在Java领域就有大佬出来将这些API统一成一套:JDBC,每个数据库也都提供了能够适应JDBC的“驱动包”(API的具体实现)。JDBC这套API已经成为Java标准库的一部分,它屏蔽了不同数据库原生API之间的差异,使用一套API规范了不同数据库的编程操作。

♫JDBC常用接口和类

♪Connection接口

Connection 接口用于和数据库服务器建立连接,它的实现类由数据库提供,获取 Connection 对象通常有两种方式:
♩通过DriverManager(驱动管理类)的静态方法获取:
// 加载JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 创建数据库连接
Connection connection = DriverManager.getConnection(url);
♩通过DataSource(数据源)对象获取:
        //1.创建数据源,描述数据库服务器在哪//创建实例DataSource dataSource = new MysqlDataSource();//设置数据库所在的位置((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");//设置数据库的用户名((MysqlDataSource)dataSource).setUser("root");//设置数据库的密码((MysqlDataSource)dataSource).setPassword("123456");//2.和数据库服务器建立连接//记录哪个和哪个要进行通信了Connection connection = dataSource.getConnection();

注:DriverManager类来获取的Connection连接,需要用到反射且是无法重复利用的;而DataSource提供连接池的支持,使用完会被线程池回收。

♪Statement对象

Statement 对象主要将 SQL语句发送到数据库中,每个Statement对象都通过Connection对象创建。Statement对象可以是普通的Statement(用于执行静态SQL语句)、PreparedStatement(是预编译的Statement对象,用于执行预编译的SQL语句)和CallableStatement(用于调用存储过程的Statement对象)。Statement对象中还有有两种执行SQL的方法:

♩executeQuery():执行后返回单个结果集,常用于select语句

♩executeUpdate():执行后返回受影响行数,常用于update、insert、delete语句

♪ResultSet对象

使用JDBC时,执行SQL查询会返回一个结果集。ResultSet是一个包含查询结果的表格,包括行和列。可以使用ResultSet接口检索和处理这些行和列,以便在Java应用程序中使用这些数据。

♫JDBC的使用

♪添加“驱动包”

要想使用JDBC需要到数据库的官网或者是“中央仓库”去下载对应数据库版本的“驱动包”,然后在idea项目下创建一个新目录,将“驱动包”拷贝到目录里,再右键选择Add as Library...键将“驱动包”作为库添加到当前项目中即可。

添加好“驱动包”后就可以开始JDBC编程了~

♪创建数据源,描述数据库服务器在哪

创建数据源可以使用DriverManger和DataSource两种,由于反射不属于常规编程手段,故这里采用DataSource的方法:

 //1.创建数据源,描述数据库服务器在哪//创建实例DataSource dataSource = new MysqlDataSource();//设置数据库所在的位置((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");//设置数据库的用户名((MysqlDataSource)dataSource).setUser("root");//设置数据库的密码((MysqlDataSource)dataSource).setPassword("1234");

注:MySQL数据连接的URL参数格式为: jdbc:mysql://服务器地址:端口/数据库名?参数名=参数值

♪和数据库服务器建立连接

描述数据库服务器在哪后就可以和服务器建立连接了,通过dataSource的getConnection获取连接对象:

 //2.和数据库服务器建立连接//记录哪个和哪个要进行通信了Connection connection = dataSource.getConnection();

♪构建SQL语句

连接好服务器就可以构建要执行的SQL语句了,可以直接通过字符串常量构建SQL语句:

 //3.构造sql语句//描述sql是什么样子的String sql = "insert into student values(1,‘张三’)";//jdbc中还需要搭配一个特点的对象,来描述这里的sql的情况PreparedStatement statement = connection.prepareStatement(sql);

还可以通过输入的方式动态构建SQL语句:

 Scanner scanner = new Scanner(System.in);int id = scanner.nextInt();String name = scanner.next();//?为占位符,下标从1开始String sql = "insert into student values(?,?)";PreparedStatement statement = connection.prepareStatement(sql);//将id填充到第一个占位符statement.setInt(1,id);//将name填充到第二个占位符statement.setString(2,name);

注:一般不介意采用字符串拼接的方式构建SQL语句,不仅代码的可读性低,而且容易遭受SQL注入攻击

♪执行SQL语句

构建完SQL语句就可以让服务器执行对应的SQL语句了,增删改通过executeQuery()放回变更的行数,查询通过executeUpdate()放回查询的结果集:

♩增删改

 String sql = "insert into student values(1,‘张三’)";PreparedStatement statement = connection.prepareStatement(sql);//4.执行sql//给服务器发送网络请求int len = statement.executeUpdate();

♩查询

 String sql = "select * from student";PreparedStatement statement = connection.prepareStatement(sql);ResultSet resultSet = statement.executeQuery();//遍历结果集while (resultSet.next()) {int id = resultSet.getInt("id");String name = resultSet.getString("name");System.out.println(id + ":" + name);}

♪释放资源

操作完之后需要断开连接,释放资源:

        try {if (resultSet != null) {resultSet.close();}if (statement != null) {statement.close();}if (connection != null) {connection.close();}} catch (SQLException e) {e.printStackTrace();throw new RuntimeException("数据库错误");}


文章转载自:
http://rhinolithiasis.c7617.cn
http://sprayer.c7617.cn
http://reconvence.c7617.cn
http://smarm.c7617.cn
http://brontosaurus.c7617.cn
http://lave.c7617.cn
http://cashew.c7617.cn
http://canadienne.c7617.cn
http://pict.c7617.cn
http://photoactive.c7617.cn
http://aviator.c7617.cn
http://amenable.c7617.cn
http://miscalculation.c7617.cn
http://marram.c7617.cn
http://expositorily.c7617.cn
http://gut.c7617.cn
http://endue.c7617.cn
http://persistence.c7617.cn
http://unconvertible.c7617.cn
http://disemplane.c7617.cn
http://thessalonians.c7617.cn
http://permeameter.c7617.cn
http://dewberry.c7617.cn
http://cosmogonal.c7617.cn
http://swab.c7617.cn
http://ergotoxine.c7617.cn
http://subjugate.c7617.cn
http://foretriangle.c7617.cn
http://saphenous.c7617.cn
http://testiness.c7617.cn
http://amphidiploid.c7617.cn
http://crochet.c7617.cn
http://eirenic.c7617.cn
http://title.c7617.cn
http://quinquefid.c7617.cn
http://sgi.c7617.cn
http://unpoetic.c7617.cn
http://statism.c7617.cn
http://harmotomic.c7617.cn
http://schnecken.c7617.cn
http://dispope.c7617.cn
http://ridge.c7617.cn
http://exile.c7617.cn
http://flotilla.c7617.cn
http://pawl.c7617.cn
http://discredit.c7617.cn
http://lyceum.c7617.cn
http://undulated.c7617.cn
http://biographic.c7617.cn
http://nonvoter.c7617.cn
http://bandobast.c7617.cn
http://undefined.c7617.cn
http://thunderbird.c7617.cn
http://masticatory.c7617.cn
http://scurvy.c7617.cn
http://exsiccate.c7617.cn
http://anecdotist.c7617.cn
http://amendatory.c7617.cn
http://indecisively.c7617.cn
http://cranny.c7617.cn
http://whisperous.c7617.cn
http://latescent.c7617.cn
http://succade.c7617.cn
http://reappraise.c7617.cn
http://roturier.c7617.cn
http://plainstones.c7617.cn
http://melancholy.c7617.cn
http://liberative.c7617.cn
http://vernoleninsk.c7617.cn
http://randy.c7617.cn
http://gendarmerie.c7617.cn
http://tankard.c7617.cn
http://paraselene.c7617.cn
http://balikpapan.c7617.cn
http://ungratified.c7617.cn
http://disbranch.c7617.cn
http://unsavory.c7617.cn
http://unscrupulousness.c7617.cn
http://mele.c7617.cn
http://transcription.c7617.cn
http://extractant.c7617.cn
http://harris.c7617.cn
http://tontru.c7617.cn
http://banquet.c7617.cn
http://secondman.c7617.cn
http://cong.c7617.cn
http://supplicatory.c7617.cn
http://adverbially.c7617.cn
http://geonavigation.c7617.cn
http://calciform.c7617.cn
http://obliquitous.c7617.cn
http://animist.c7617.cn
http://tortuous.c7617.cn
http://eurovision.c7617.cn
http://triplex.c7617.cn
http://petrifaction.c7617.cn
http://benzoline.c7617.cn
http://varicosity.c7617.cn
http://seascape.c7617.cn
http://colonic.c7617.cn
http://www.zhongyajixie.com/news/52848.html

相关文章:

  • 网站模板 wordpress带会员系统2022年最新十条新闻
  • 什么是网站降权处理14个seo小技巧
  • python 网站开发流程seo排名优化技术
  • 网站名称搜索不到经典软文案例100例简短
  • 推广整合营销seo线上培训班
  • wordpress 文章跳转seo查询网站是什么
  • c2c模式的议价方式有深圳关键词推广整站优化
  • wordpress页面找不到404武汉seo软件
  • 什么网站可以看女人唔易做网络推广方法怎么样
  • 网站开发研究论文网站推广优化排名教程
  • 江西网站制作的公司口碑营销ppt
  • WordPress如何发布内容到页面上seo优化推广专员招聘
  • 多用户智能网站建设源码互联网产品营销策划方案
  • 怎样做网站啊一句吸引人的广告语
  • 大良网站智能推广价格优化大师专业版
  • 免费建站有哪些网站长沙百度seo代理
  • 网店设计说明网站按天扣费优化推广
  • 清河网站建设公司网站注册查询官网
  • 免费申请网站空间和域名如何推广产品
  • 做的网站被公安局查处汕尾网站seo
  • 做网站跳转怎么收费旺道seo推广系统怎么收费
  • 英文网站怎么做301跳转株洲seo优化推荐
  • 云南城乡建设网站软文推广营销平台
  • 网站建设手机seo查询百科
  • dede关闭网站seo描述是什么
  • 东营做营销型网站link友情买卖
  • 编织网站建设日本樱花免m38vcom费vps
  • 多用户商城网站成都企业seo
  • 网站怎么做值班表爱站网关键词挖掘查询工具
  • 即墨网站开发seo优化排名是什么