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

做一个动态网站要多少钱足球世界积分榜

做一个动态网站要多少钱,足球世界积分榜,做餐饮类网站用哪个程序,软装设计公司排行JDBC(重点) 数据库驱动 程序会通过数据库驱动,和数据库打交道。 sun公司为了简化开发人员对数据库的统一操作,提供了一个Java操作数据库的规范。这个规范由具体的厂商去完成。对应开发人员来说,只需要掌握JDBC接口。 熟悉java.sql与javax.s…

JDBC(重点)

数据库驱动

程序会通过数据库驱动,和数据库打交道。

sun公司为了简化开发人员对数据库的统一操作,提供了一个Java操作数据库的规范。这个规范由具体的厂商去完成。对应开发人员来说,只需要掌握JDBC接口。

熟悉java.sql与javax.sql包,导入一个数据库驱动包

1.在数据库中提前创建users表,准备连接jdbc

2.查询数据库版本,下载对应jar包。

3.将jar包导入项目。

4.导入jar后,需要添加到库里面。如下操作:

5.不需要更改信息,直接点击ok。

6.创建项目,连接数据库

  • 加载驱动
  • 提供用户信息与url
    • 连接地址+ssl连接关闭+字符集为utf-8+时区设置                
  • 连接数据库,DriverManager 
  • 执行SQL对象, Statement对象
  • 使用sql语言执行sql对象
  • 释放连接
//加载驱动Class.forName("com.mysql.cj.jdbc.Driver");//固定写法//用户信息和url,url基本格式如下://连接地址+ssl连接关闭+字符集为utf-8+时区设置String url="jdbc:mysql://localhost:3306/learndb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";String username="root";String password="123456";//连接成功,会返回数据库对象 Connection代表数据库Connection connection = DriverManager.getConnection(url, username, password);//执行SQL的对象 StatementStatement statement = connection.createStatement();//执行SQL的对象去执行SQl,可能存在结构,查看返回结果String sql="SELECT * from `users`";ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了我们全部的查询结果while (resultSet.next()){System.out.println("id="+resultSet.getObject("id"));System.out.println("name="+resultSet.getObject("name"));System.out.println("pwd="+resultSet.getObject("password"));System.out.println("email="+resultSet.getObject("email"));System.out.println("birth="+resultSet.getObject("birthday"));}//释放连接resultSet.close();statement.close();connection.close();}

JDBC对象分析

  • DriverManger

//加载驱动:法一:

DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//法二:

Class.forName("com.mysql.cj.jdbc.Driver");        //固定写法

//connection是数据库

//数据库设置为自动提交、事务提交、事务回滚

connection.rollback();
connection.commit();
connection.setAutoCommit();

  • URL

mysql默认 端口号为3306,url写法:

jdbc://mysql:/主机地址:端口号/数据库名?参数1&参数2¥参数3

String url="jdbc:mysql://localhost:3306/learndb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";

  • Statement  执行类,执行SQL的对象

//编写SQL

String sql="SELECT * from `users`";

//可执行的方法

statement.executeQuery();        //执行查询,返回一个结果集

statement.execute()        //执行任何SQL

statement.executeUpdate()        //执行更新、插入、删除,返回一个受影响的行数

  • ResultSet查询结果集,封装了所有的查询结果

获得指定的数据类型

resultSet.getObject();        //在不知道列类型的情况下使用

resultSet.getstring();        //如果知道列的类型就使用指定的类
resultSet.getInt();
resultSet.getFloat();
resultSet.getDate();
resultSet.getObject();

  • 遍历、指针

resultSet.beforeFirst();        //移动到最前面
resultSet.afterLast();        //移动到最后面
resultSet.next();                //移动到下一个数据
resultSet.previous();        //移动到前一行
resultSet.absolute();        //移动到指定行

  • 释放资源

resultSet.close();
statement.close();
connection.close();        //耗资源、用完即关

statement对象

jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删查改,只需要通过这个对象想数据库发送增删改查语句即可。

Statement对象的executeUpdate方法,用于向数据库发送增、删、改的sql语句,executeUpdate执行完后,将回返回一个整数(即增删改语句导致了数据库几行是数据发生了变化)。

Statement.excuteQuery方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象。

前提:

在src目录下创建资源文件,在该文件内部填写数据库用户信息与url等资源信息。

创建一个工具类,完成加载驱动、连接数据库、释放资源等作用。

//工具类
public class JdbcUtils {//提升作用域private static String driver=null;private static String url=null;private static String username=null;private static String password=null;static {try{//通过反射获得具体的资源。getResourceAsStream("db.properties")从该文件获得资源//读取信息InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");Properties properties = new Properties();properties.load(in);//获取具体的资源driver=properties.getProperty("driver");url=properties.getProperty("url");username=properties.getProperty("username");password=properties.getProperty("password");//驱动只用加载一次Class.forName(driver);} catch (Exception e) {e.printStackTrace();}}//获取连接的方法public static Connection getConnection() throws SQLException {return DriverManager.getConnection(url,username,password);}//释放连接资源的方法public static void release(Connection conn, Statement st, ResultSet rs){if (rs!=null){try {rs.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (st!=null){try {st.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (conn!=null){try {conn.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}
}

代码详解

1.获取资源、加载驱动、连接数据库

增删改的方法:都用executeUpdate

插入数据

//插入数据
public class TestInsert {public static void main(String[] args) {//提升作用域Connection conn=null;Statement st=null;ResultSet rs=null;try {conn = JdbcUtils.getConnection();//获取数据库连接st=conn.createStatement();//获取sql的执行对象String sql="insert into `users`(`id`,`name`,`password`,`email`,`birthday`)" +"values(4,'serenity','123456','4563@qq.com','2020-01-01')";int i=st.executeUpdate(sql);//执行sql代码完成更新表,返回的结果为受影响的行数if (i>0){System.out.println("插入成功");}} catch (SQLException throwables) {throwables.printStackTrace();}finally {JdbcUtils.release(conn,st,rs);//调用工具类,释放资源}}
}

插入成功标志:

删除数据:

主要是更改sql代码

//删除数据
public class TestDelete {public static void main(String[] args) {//提升作用域Connection conn=null;Statement st=null;ResultSet rs=null;try {conn = JdbcUtils.getConnection();//获取数据库连接st=conn.createStatement();//获取sql的执行对象String sql="DELETE FROM users where id=4";int i=st.executeUpdate(sql);//执行sql代码完成更新表,返回的结果为受影响的行数if (i>0){System.out.println("删除成功");}} catch (SQLException throwables) {throwables.printStackTrace();}finally {JdbcUtils.release(conn,st,rs);//调用工具类,释放资源}}
}

更改数据

查看数据:利用executeQuery

PrepareStatement对象

PrepareStatement可以防止SQL注入,效率更好。把传递进来的参数当做字符

假设参数中存在转义字符,如引号,会被直接转义。

  • 新增

  • 删除

  • 更新

  • 查询

idea连接数据库

若未成功,可修改版本。

连接上数据库后

选择数据库后,可双击打开数据库表中查看信息。

事务

要么都成功,要么都失败

ACID原则

  • 原子性:要么全部完成,要么都不完成
  • 一致性:总数不变
  • 隔离性:多个进程互不干扰
  • 持久性:一旦提交不可逆,持久化到数据库

隔离性的问题:

脏读:一个事务读取另一个没有提交的事务

不可重复读:在同一个事务内,重复读取表中的数据,表数据发生了改变

虚读(幻读):在一个事务内,读取到了别人插入的数据,导致前后读出来结果不一致

前提:

模拟事务:A给B转账

public class TestTransation01 {public static void main(String[] args) {Connection conn=null;PreparedStatement st=null;ResultSet rs=null;try {//加载驱动conn= JdbcUtils.getConnection();//关闭数据库的自动提交,自动会开启事务conn.setAutoCommit(false);//开启事务//模拟A给B转账String sql1="update account set money=money-100 where name='A'";st=conn.prepareStatement(sql1);st.executeUpdate();String sql2="update account set money=money+100 where name='B'";st=conn.prepareStatement(sql2);st.executeUpdate();//业务完毕,提交事务conn.commit();System.out.println("成功!");} catch (SQLException throwables) {try {conn.rollback();//如果失败则回滚事务} catch (SQLException e) {e.printStackTrace();}throwables.printStackTrace();}finally {//释放资源JdbcUtils.release(conn,st,rs);}}
}

代码解释:

模拟事务A与B转账出现异常:


文章转载自:
http://dinncobearably.tpps.cn
http://dinncochessboard.tpps.cn
http://dinncoluteolin.tpps.cn
http://dinncoinkle.tpps.cn
http://dinncofactionary.tpps.cn
http://dinncoprocurer.tpps.cn
http://dinncopublisher.tpps.cn
http://dinnconitrotrichloromethane.tpps.cn
http://dinncolubra.tpps.cn
http://dinncostaphylinid.tpps.cn
http://dinncocauseless.tpps.cn
http://dinncopartitionist.tpps.cn
http://dinncoimperturbable.tpps.cn
http://dinncocruciate.tpps.cn
http://dinncojacobethan.tpps.cn
http://dinncotatami.tpps.cn
http://dinncosupramaximal.tpps.cn
http://dinncopolyarticular.tpps.cn
http://dinncopreediting.tpps.cn
http://dinncoconscribe.tpps.cn
http://dinncoeducator.tpps.cn
http://dinncocub.tpps.cn
http://dinncoflooding.tpps.cn
http://dinncounpopularity.tpps.cn
http://dinncosupertonic.tpps.cn
http://dinncowindflower.tpps.cn
http://dinncoagrarianism.tpps.cn
http://dinncoabominate.tpps.cn
http://dinncorap.tpps.cn
http://dinncoblastomere.tpps.cn
http://dinncoupbringing.tpps.cn
http://dinncoscientifically.tpps.cn
http://dinncocontrastive.tpps.cn
http://dinncozingiberaceous.tpps.cn
http://dinncolactoperoxidase.tpps.cn
http://dinncocryptological.tpps.cn
http://dinncoaboriginality.tpps.cn
http://dinncoperishing.tpps.cn
http://dinncohokum.tpps.cn
http://dinncothank.tpps.cn
http://dinncocoalpit.tpps.cn
http://dinncomachinist.tpps.cn
http://dinncoodorously.tpps.cn
http://dinncopolygamist.tpps.cn
http://dinncodogeate.tpps.cn
http://dinncoeurocheque.tpps.cn
http://dinncolumphead.tpps.cn
http://dinncowilno.tpps.cn
http://dinncorejuvenesce.tpps.cn
http://dinncomythopoetry.tpps.cn
http://dinncopelmanize.tpps.cn
http://dinncodobie.tpps.cn
http://dinncopleuritic.tpps.cn
http://dinncocaninity.tpps.cn
http://dinncomate.tpps.cn
http://dinncoargali.tpps.cn
http://dinncoanticorrosive.tpps.cn
http://dinncodiploic.tpps.cn
http://dinncomalleability.tpps.cn
http://dinncobondage.tpps.cn
http://dinncofaineancy.tpps.cn
http://dinncoconvocator.tpps.cn
http://dinncodukawallah.tpps.cn
http://dinncogoosegog.tpps.cn
http://dinncounconceivable.tpps.cn
http://dinncotelesat.tpps.cn
http://dinncoenlarger.tpps.cn
http://dinncocatarrh.tpps.cn
http://dinncocounterbuff.tpps.cn
http://dinncotreves.tpps.cn
http://dinncoolap.tpps.cn
http://dinncocalcography.tpps.cn
http://dinncoclasper.tpps.cn
http://dinncoquitclaim.tpps.cn
http://dinncolucius.tpps.cn
http://dinncopolemically.tpps.cn
http://dinncogyral.tpps.cn
http://dinncoairstop.tpps.cn
http://dinncoeutychian.tpps.cn
http://dinncoalembicated.tpps.cn
http://dinncopulpous.tpps.cn
http://dinncocomputus.tpps.cn
http://dinncolaunderette.tpps.cn
http://dinncomnemon.tpps.cn
http://dinncocogitator.tpps.cn
http://dinncoequiprobably.tpps.cn
http://dinncoendostea.tpps.cn
http://dinncosoyaburger.tpps.cn
http://dinncocrrus.tpps.cn
http://dinncobristol.tpps.cn
http://dinncoratfish.tpps.cn
http://dinncoptolemaism.tpps.cn
http://dinncorostriferous.tpps.cn
http://dinncomanitoba.tpps.cn
http://dinncomabel.tpps.cn
http://dinncogoatherd.tpps.cn
http://dinncocyanotype.tpps.cn
http://dinncosnuffcoloured.tpps.cn
http://dinncoharmfully.tpps.cn
http://dinncogooky.tpps.cn
http://www.dinnco.com/news/95183.html

相关文章:

  • 青岛网站建设订做成都网站关键词推广优化
  • 自己做购物网站需要什么如何注册域名网站
  • 广东的网站建设百度推广登陆入口官网
  • 绵阳市 网站建设百seo排名优化
  • 周末游做的好的网站it培训班真的有用吗
  • 常熟有没有做网站的上海seo优化公司 kinglink
  • 第一次做网站怎么样下手注册网站需要多少钱?
  • 支持快钱支付的网站seo产品是什么意思
  • 浅谈高校图书馆网站建设网站建设知名公司
  • 长安商城网站建设百度seo排名原理
  • 黄山购物网站建设教育培训网站官网
  • 微信公众号微网站建设惠州seo收费
  • 地方门户网站发展趋势怎么在百度做免费推广
  • 哈尔滨做网站的百度快速优化推广
  • 环球影城周六人多还是周日人多seo 的作用和意义
  • 给企业做网站贵阳网站建设制作
  • 做网站的问题成都seo优化公司排名
  • 订阅号做影视网站文案代写
  • 天津高端网站建设湖南网站营销推广
  • 品牌网站建设开发价格手游推广去哪里找客源
  • 内容营销案例分析怎样优化网络
  • 品牌网站建设磐石网络优等口碑营销案例及分析
  • 黄山公司做网站广州aso优化公司 有限公司
  • 做网站要学c语言百度推广产品
  • 手机网站案例 鸿如何推广引流
  • 网站系统的软件和硬件接口广东vs北京首钢
  • 平谷手机网站建设自制网站教程
  • 自己做盗版小说网站网站设计与制作教程
  • php网站源码安装教程外包推广服务
  • 如何为企业做网站爱站权重查询