8.4.8jdbc的基本流程

  • 1,加载驱动(选择数据库) oracle.jdbc.driver.OracleDriver

  • 2,建立连接(与数据库建立连接)

  • 3,准备sql

  • 4,封装处理块

  • 5,发送执行sql,得到结果集

  • 6,处理结果

  • 7,关闭连接

public class Class01_JDBC {public static void main(String[] args) throws ClassNotFoundException, SQLException {//1,加载驱动Class.forName("oracle.jdbc.driver.OracleDriver");//2,建立连接Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE","SCOTT","TIGER");//3,准备sqlString sql = "select * from emp";//4,封装处理块Statement state = conn.createStatement();//5,发送执行sql,得到结果集ResultSet result = state.executeQuery(sql);//6,处理结果while (result.next()){int empno = result.getInt(1);String ename = result.getString(2);String job = result.getString(3);int mgr = result.getInt(4);//String hiredate = result.getString(5);double sal = result.getDouble(6);double comm = result.getDouble(7);String deptno = result.getString(8);System.out.println(empno+"----->"+ename+"----->"+job+"----->"+mgr+"----->"+sal+"----->"+comm+"----->"+deptno);}//7,关闭连接conn.close();}
}
​

8.4.9jdbc的基本流程优化

  • 1,异常捕获

  • 2,定义配置文件db.properties实现软编码

public class Class01_JDBC2 {public static void main(String[] args){//Properties创建一个对象Properties pro = new Properties();
​try {pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));} catch (IOException e) {e.printStackTrace();}
​
​// 1.加载驱动 (选择数据库)try {Class.forName(pro.getProperty("driver"));} catch (ClassNotFoundException e) {e.printStackTrace();}//提前声明Connection conn = null;Statement state = null;ResultSet result = null;try {//2.建立连接(与数据库建立连接)conn = DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("username"),pro.getProperty("password"));//3.准备sqlString sql = "select * from dept";//4.封装处理块state = conn.createStatement();//5.发送执行sql,得到结果集result = state.executeQuery(sql);//6.处理结果while(result.next()){Object deptno = result.getObject(1);Object dname = result.getObject(2);Object loc = result.getObject(3);
​System.out.println(deptno+"---->"+dname+"---->"+loc);}} catch (SQLException throwables) {throwables.printStackTrace();} finally {//7.关闭连接if (result!=null){try {result.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (state!=null){try {state.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (conn!=null){try {conn.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}}
}
​

8.5用户的操作

  • 用户的操作: 注册用户 用户登录 1)根据用户名与密码一起查询 2)根据用户名查询,查询到密码与用户输入的密码比较是否相等

  • 修改用户数据 注销用户

  • 注意: 事务默认提交

  • PreparedStatement 预处理块: 1.预先编译,提高效率 2.防止SQL注入

public class Class01_User {public static void main(String[] args) {System.out.println(login("88","java","lisi")?"成功":"失败");;}
​//用户登录//预处理块 : sql语句的拼接由处理块实现public static boolean login(String deptno,String dname,String loc){//给类型赋值Connection conn = null;PreparedStatement state = null;ResultSet result = null;//false认为失败boolean flag = false;try {//1.获取连接conn = Class01_JDBCUtils.getConnection();//2.准备sqlString sql = "select * from deptno where deptno=?  and dnamme =? and loc =?";//3.封装预处理块state = conn.prepareStatement(sql);
​//为sql中的?赋值state.setObject(1,deptno);state.setObject(2,dname);state.setObject(3,loc);
​//4.执行,结果result = state.executeQuery();//5.处理结果if(result.next()){flag = true;}}catch (SQLSyntaxErrorException throwables) {System.out.println("遇到sql注入啦");}catch (SQLException throwables) {throwables.printStackTrace();} finally {//6.关闭资源
//            Class01_JDBCUtils.close(conn,state,result);}//7.返回值return flag;}//静态处理块/*public static boolean login(String username,String password){Connection conn= null;Statement state = null;ResultSet result = null;boolean flag = false;
​try {//1.获取连接conn = Class01_JDBCUtils.getConnection();//2.准备sqlString sql = "select * from emp where username='"+username+"' and password = "+password;//3.封装处理块state = conn.createStatement();//4.执行,结果result = state.executeQuery(sql);//5.处理结果if(result.next()){flag = true;}} catch (SQLException throwables) {throwables.printStackTrace();} finally {//6.关闭资源Class01_JDBCUtils.close(conn,state,result);}//7.返回值return flag;}
*///用户注册public static boolean reg(String username,String password){Connection conn= null;Statement state = null;int rows = -1; //影响行数boolean flag = false;
​try {//1.获取连接conn = Class01_JDBCUtils.getConnection();//2.准备sqlString sql = "insert into t_user(username,password) values('"+username+"',"+password+")";//3.封装处理块state = conn.createStatement();//4.执行,结果rows = state.executeUpdate(sql);//5.处理结果if(rows>0){flag = true;}} catch (SQLException throwables) {throwables.printStackTrace();} finally {//6.关闭资源Class01_JDBCUtils.close(conn,state);}//7.返回值return flag;}
}
​

8.5.1jdbc流程封装

  • JDBC流程封装 1.加载驱动 2.获取连接 3.关闭资源

public class Class01_JDBCUtils {
​private static Properties pro = new Properties();
​static{try {pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));} catch (IOException e) {e.printStackTrace();}
​//加载驱动 (选择数据库)try {Class.forName(pro.getProperty("driver"));} catch (ClassNotFoundException e) {e.printStackTrace();}}
​//获取连接public static Connection getConnection() throws SQLException {Connection conn = null;conn = DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("username"),pro.getProperty("password"));return conn;}
​//关闭资源public static void close(Connection conn, Statement state){close(conn,state,null);}
​public static void close(Connection conn, Statement state, ResultSet result){if(result!=null){try {result.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if(state!=null){try {state.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if(conn!=null){try {conn.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}
}

什么是JDBC,JDBC是干嘛用的?相关推荐

  1. php sqlserver jdbc,jdbc sqlserver 分页

    (1). top ... not in , (2). top .... id( max ),(3).游标 这种方法感觉比上面三种要快 ,分享一下跟好的意见 使用的是org.springframewor ...

  2. JAVA基础知识之JDBC——JDBC事务处理及批量更新

    JDBC事务 JDBC的事务由Connection提供,默认是打开的. 要开启事务,则要关闭自动提交, 1 conn.setAutoCommit(false); 提交事务使用 1 conn.commi ...

  3. java day30【数据库连接池 、Spring JDBC : JDBC Template】

    第一章  数据库连接池 1. 概念:其实就是一个容器(集合),存放数据库连接的容器. 当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后 ...

  4. MySQL~Java的数据库编程:JDBC(JDBC的环境配置以及使用)

      目录 JDBC的环境配置 JDBC的使用步骤(Java操作数据库) 建立数据库连接 方式一:DriverManager 方式二:DataSource(数据源/数据库连接池) DataSource与 ...

  5. jdbc(跟着宝哥学java:jdbc) jdbc概念,铁打步骤,jdbc封装,预编译对象,sql攻击

    1 概念 jdbc:java database connection 通过java连接数据库 sun公司为java连接所有数据库提供的一组接口; jdbc驱动:各个数据库厂家为自己的数据库根据jdbc ...

  6. web前端,数据库,jdbc

    刘国斌 77331283   bjliugb@tedu.cn                 ----------------web-day01.html ###网站的架构 - CS:Client S ...

  7. 【MySQL系列】Java的JDBC编程

    目录 ??前言 ??一.背景知识引入 ??二.安装MySQL数据库驱动包,并且导入到项目中 ??三.JDBC的使用 ???3.1 JDBC插入数据操作 ???3.2 JDBC修改数据操作 ???3.3 ...

  8. java后端系统学习总结 02_数据库基础学习、jdbc基础学习、er图基础学习、数据库——(完结)

    猿猿正在系统的学习一些计算机知识,和后端技术栈,目前阶段主要在系统学习java.此专栏,为我学习过程中的学习笔记,便于日后复习回顾来看,也很适合新人学习参考. 以下是猿猿对数据库和java深入的第一遍 ...

  9. mysql和jdbc的区别_JDBC详解

    一. JDBC 简介 1 什么是 JDBC •JDBC(JavaDataBaseConnectivity)java 数据库连接 • 是 JavaEE 平台下的技术规范 • 定义了在 Java 语言中连 ...

最新文章

  1. Missing number
  2. java进程未正常退出
  3. Android开发--RadioButton和CheckBox控件的使用
  4. 解决ubuntu apt-get install出现E:Clould not get lock /var/lib/dpkg/lock
  5. POJ1195Mobile phones
  6. Java黑皮书课后题第6章:*6.15(金融应用:打印税表)程序清单3-5给出了计算税款的程序。使用下面的方法体编写一个计算税款的方法。使用这个方法编写程序,打印可征税人从50000到60000间隔
  7. 红色风格电脑手机数码商城系统网站源码
  8. (33)System Verilog模块与包定义同名类冲突
  9. P1396 营救(并查集+二分)
  10. android在java下建立模块,Android Studio:如何在Android模块中包含Java模块?
  11. 2020 年,开启现代库的基建学习 —— 从项目演进看前端工程化发展
  12. 浙江省计算机数据库三级报名,浙江省计算机等级考试三级数据库技术
  13. Nginx + Tomcat6配置负载均衡
  14. c语言谭浩强第六章答案,C语言谭浩强版本第6章课后练习题答案..doc
  15. JS Bin 在线编辑代码,所见所得
  16. MFC中UpdateData()函数的使用
  17. 灌篮高手全国大赛漫画 (23-31卷) + 十日后
  18. log4j警告的解决办法-狂奔的蜗牛-iteye技术网站
  19. Oracle undo表空间管理
  20. 苏、陕、宁、浙四省主动安全防控/智能视频监控预警设备平台一览

热门文章

  1. 论文阅读——Automatic Testing and Improvement of Machine Translation
  2. 阿里云的云端实践有哪些?
  3. parameterType 用法
  4. 易语言认识易语言数据类型
  5. mac brew安装php7.4
  6. MapGuide空间参考系API
  7. 华盛证券软件测试工程师工资,【恒生电子工资】软件测试工程师待遇-看准网...
  8. DOM自定义属性 getAttribute、setAttribute、removeAttribute
  9. 流体力学概念总结(涉及所有重点)
  10. Liferay的学习