JDBC连接(javadatabaseconnectivity)
JDBC连接(java database connectivity)jdbc使用的步骤
1.通过反射机制加载驱动类-->>相当于是一个jdbc管理的工具类 2.找到我们的数据库连接池的url 3.使用Driver Manager(驱动管理器)获得到数据库连接 4.通过connect创建一个状态通道 5.就可以使用sql语句向状态通道中传入我们想要实现的sql语句 6.最后,注意一定要关闭所有的连接
通过使用数据库驱动-->>使用java来对数据库进行增删改的操作
注意jar包的添加 具体实现
示例: package com; import java.sql.*; /** * @Author: fyw * @Description:1.通过反射机制加载驱动类-->>相当于是一个jdbc管理的工具类 2.找到我们的数据库连接池的url 3.使用Driver Manager(驱动管理器)获得到数据库连接 4.通过connect创建一个状态通道 5.就可以使用sql语句向状态通道中传入我们想要实现的sql语句 6.最后,注意一定要关闭所有的连接 * @Date Created in 2021-08-28 19:12 * @Modified By: */ public class Demo01 { public static void main(String[] args) { Statement statement = null; ResultSet resultSet = null; Connection connection = null; //1. 加载驱动类 try { Class.forName("com.mysql.cj.jdbc.Driver"); //2. 获得连接 String url = "jdbc:mysql://localhost:3306/yhp2?serverTimezone=UTC"; String userName = "root"; String passWord = "123456"; connection = DriverManager.getConnection(url, userName, passWord); //3. 定义sql语句,创建状态通道,进行sql语句的发送 statement = connection.createStatement(); resultSet = statement.executeQuery("select *from yhp2.emp1"); while (resultSet.next()){//判断是否有下一条数据 System.out.println("姓名:"+resultSet.getString("ename")+"工资:"+resultSet.getDouble("sal")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { try { if (resultSet != null) { resultSet.close(); } //5. 关闭资源 if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
通过状态通道传入sql语句的时候有几种不同的函数用法,注意返回值,
如果是查询executeQuery()-->>resultSet-->>结果集映射
如果是更新操作(包括update,delete,insert)-->>返回的都是int性的影响行数 package com; import java.sql.*; /** * @Author: fyw * @Description: * @Date Created in 2021-08-28 19:12 * @Modified By: */ public class Demo01 { public static void main(String[] args) { Statement statement = null; ResultSet resultSet = null; Connection connection = null; //1. 加载驱动类 try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/yhp2?serverTimezone=UTC"; String userName = "root"; String passWord = "123456"; //2. 获得连接 connection = DriverManager.getConnection(url, userName, passWord); //3. 定义sql语句,创建状态通道,进行sql语句的发送 statement = connection.createStatement(); resultSet = statement.executeQuery("select *from yhp2.emp1"); while (resultSet.next()){//判断是否有下一条数据 System.out.println("姓名:"+resultSet.getString("ename")+"工资:"+resultSet.getDouble("sal")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { try { if (resultSet != null) { resultSet.close(); } //5. 关闭资源 if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } } } sql注入package com; import java.sql.*; /** * @Author: fyw * @Description: * @Date Created in 2021-08-28 19:12 * @Modified By: */ public class Demo04 { public static void main(String[] args) { Statement statement = null; ResultSet resultSet = null; Connection connection = null; //1. 加载驱动类 try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/yhp2?serverTimezone=UTC"; String userName = "root"; String passWord = "123456"; //2. 获得连接 connection = DriverManager.getConnection(url, userName, passWord); //3. 定义sql语句,创建状态通道,进行sql语句的发送 statement = connection.createStatement();//通过连接通道返回一个状态通道,可以通过状态通道进行传入sql语句 // int result = statement.executeUpdate("insert into yhp2.emp1(empno,ename,hiredate,sal) values(100005,"fyw","2001-04-29",20000)"); String username1 = "sdfsdf"; String pwd1 = " "" or 1=1 "; resultSet = statement.executeQuery("select *from `5-3kkb作业`.login where username=""+username1+"" and pwd="+pwd1); if(resultSet.next()){ System.out.println("登录成功"); }else{ System.out.println("登录失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { try { if (resultSet != null) { resultSet.close(); } //5. 关闭资源 if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } } } sql注入解决方案
预状态通道
preparedstatement
1.通过connect连接-->>preparstatement生成我们的预状态通道
2.直接将要执行的sql语句传入-->>关键部分用?代替
3.通过preparstatement.set类型,来传入我们的关键参数 注意:下标是从1开始
4.直接通过预状态通道执行sql package com; import java.sql.*; /** * @Author: fyw * @Description: * @Date Created in 2021-08-28 19:12 * @Modified By: */ public class Demo05 { public static void main(String[] args) { Statement statement = null; ResultSet resultSet = null; Connection connection = null; //1. 加载驱动类 try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/yhp2?serverTimezone=UTC"; String userName = "root"; String passWord = "123456"; //2. 获得连接 connection = DriverManager.getConnection(url, userName, passWord); //3. 防止sql注入,先连接一个预状态通道 PreparedStatement preparedStatement = connection.prepareStatement("select * from `5-3kkb作业`.login where pwd=? and username=?"); //4. 通过对占位符的赋值,进行sql; String username1 = "sdfsdf"; String pwd1 = " "" or 1=1 "; preparedStatement.setString(1,username1); preparedStatement.setString(2,pwd1); //5. 执行sql ResultSet resultSet1 = preparedStatement.executeQuery(); if(resultSet1.next()){ System.out.println("登录成功"); }else{ System.out.println("登录失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { try { if (resultSet != null) { resultSet.close(); } //5. 关闭资源 if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
大家觉得学习电子商务怎么样,前景如何?用时髦的话说,目前在风口上电子商务和线下销售一样。只是销售的场所发生了变化。围绕寡头电商平台开展。电子商务就是互联网思维下的营销模式。电商这个概念我个人感觉并不明确。首先从字面理解
如何看待张捷回家必经之路被人装摄像头和中信不续聘张捷?支持张捷教授,严查幕后黑手!资本已经的力量已经肆无忌惮了!中国的资本已经结成利益联盟,或者千丝万缕的联系,是与普通大众立场根本不同利益根本不同的一个特殊既得利益群体(中国新兴资产阶
43岁京东副总裁患绝症成渐冻人,斥资千万只为活命,自救也是救人他是身价过亿的京东集团副总裁互联网财税联盟会长中国电子发票的推动者却在人生正值辉煌时一夜之间从人生赢家变成了绝症患者,甚至被宣判了死刑,因为目前世界上唯一的一款特效药,也只能多让他
周三,A股10年来最大IPO启动申购近10年来A股最大IPO,真的来了。中国移动回归A股,本周三即将启动申购。根据安排,12月20日至24日,A股将有6只新股启动申购,包括3只科创板2只沪市主板1只创业板。其中,中国
柳传志执掌联想进入世界500强,却抵不过玩嘴的司马南柳传志执掌联想30余年,联想控股总资产6500亿,净资产过1000亿。现在变成联想发展好与柳传志没有半毛钱关系,联想问题都是柳传志问题麻烦问吃瓜群众一个问题,联想靠汉卡发家起步联想
市场份额仅剩2,华为海思芯片的处境让人无奈近日,著名市场调研机构Counterpoint公布了2021年第三季度全球智能手机芯片出货量情况。下面,就让我们一起来看一看吧。据报告显示,在第三季度中,整体市场表现十分良好,出货
10!Q3展锐移动芯片全球市占率创纪录赛道Hyper12月,展锐第三次以黑马身姿驰骋业界。此前,展锐于今年5月和9月,曾两次让市场意外。来自权威市场调查机构的统计数据显示,2021年第三季度,展锐消费级移动芯片全球市场占有率,首次达
docker启动nginx基本命令查看可用版本dockersearchnginxNAMEDESCRIPTIONSTARSOFFICIALAUTOMATEDnginxOfficialbuildofNginx。
Go语言学习第三周goroutinggoroutine是Go语言中的轻量级线程实现,由Go运行时(runtime)管理。Go程序会智能地将goroutine中的任务合理地分配给每个CPU。1创建gorouting和调
如果字节跳动上市,市值会是多少?字节跳动如果上市,以其目前在中国乃至世界的互联网市场份额来看,市值应该超过扎克伯格Meta市值的60(Meta市值9197亿美元),市值在3。7万亿人民币5万亿人民币之间。根据字节
雪梨林珊珊网店被封杀直播电商运营需遵守规则近日,网红主播雪梨和林珊珊因逃税被罚款淘宝网店双12当天被封等事引发热议。网经社电子商务研究中心网络零售部主任高级分析师莫岱青指出,雪梨林珊珊被多家平台封杀,无论是对自身还是对宸帆