iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >jdbc学习总结3------javab
  • 401
分享到

jdbc学习总结3------javab

jdbcjavab 2023-01-31 06:01:52 401人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

 1.测试类的内容: 在包:com.hanchao.test中   package com.hanchao.test;  import com.hanchao.dao.UserDao; import com.hanchao.entity.U

 1.测试类的内容:

在包:com.hanchao.test中

 

  1. package com.hanchao.test; 
  2.  
  3. import com.hanchao.dao.UserDao; 
  4. import com.hanchao.entity.User; 
  5.  
  6.  
  7. public class Test { 
  8.  
  9.      
  10.  
  11.     public static void main(String[] args) { 
  12.          
  13.          
  14.  
  15.      
  16.          
  17.  
  18.          
  19.          
  20. //      UserDao userDao = new UserDao(); 
  21. //      userDao.delete(21); 
  22.          
  23.          
  24.         UserDao userDao = new UserDao(); 
  25.         userDao.retrieve(22); 
  26.          
  27.     } 
  28.      

2.实体类的写法:com.hanchao.entity

 

  1. package com.hanchao.entity; 
  2.  
  3. public class User { 
  4.  
  5.      
  6.      
  7.     private int id; 
  8.     private String username; 
  9.     private String address; 
  10.      
  11.     //下面是setter...getter.. 
  12.     public int getId() { 
  13.         return id; 
  14.     } 
  15.     public void setId(int id) { 
  16.         this.id = id; 
  17.     } 
  18.     public String getUsername() { 
  19.         return username; 
  20.     } 
  21.     public void setUsername(String username) { 
  22.         this.username = username; 
  23.     } 
  24.     public String getAddress() { 
  25.         return address; 
  26.     } 
  27.     public void setAddress(String address) { 
  28.         this.address = address; 
  29.     } 
  30.      
  31.      

3.dao的写法:com.hanchao.dao

 

  1. package com.hanchao.dao; 
  2.  
  3. import java.sql.Connection; 
  4. import java.sql.DriverManager; 
  5. import java.sql.PreparedStatement; 
  6. import java.sql.ResultSet; 
  7. import java.sql.SQLException; 
  8.  
  9. import com.hanchao.entity.User; 
  10.  
  11.  
  12. public class UserDao { 
  13.  
  14.      
  15.      
  16.      
  17.     public int insert(User user) { 
  18.         Connection con = null; 
  19.         PreparedStatement sta = null; 
  20.         int rows = 0; 
  21.          
  22.         try { 
  23.              
  24.             Class.forName("com.Mysql.jdbc.Driver"); 
  25.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
  26.              
  27.             String sql = "insert into t_user(username,address) value(?,?)"; 
  28.             sta = con.prepareStatement(sql); 
  29.             sta.setString(1, user.getUsername()); 
  30.             sta.setString(2, user.getAddress()); 
  31.              
  32.             rows = sta.executeUpdate(); 
  33.             if(rows > 0) { 
  34.                 System.out.println("operate successfully!!"); 
  35.             } 
  36.              
  37.         } catch (ClassNotFoundException e) { 
  38.             e.printStackTrace(); 
  39.         } catch (SQLException e) { 
  40.             e.printStackTrace(); 
  41.         } finally { 
  42.             if(sta != null) { 
  43.                 try { 
  44.                     sta.close(); 
  45.                 } catch (SQLException e) { 
  46.                     e.printStackTrace(); 
  47.                 } finally { 
  48.                     if(con != null) { 
  49.                         try { 
  50.                             con.close(); 
  51.                         } catch (SQLException e) { 
  52.                             e.printStackTrace(); 
  53.                         } 
  54.                     } 
  55.                 } 
  56.             } 
  57.         } 
  58.         return rows; 
  59.     } 
  60.      
  61.      
  62.     public int update(User user) { 
  63.         Connection con = null; 
  64.         PreparedStatement sta = null; 
  65.         int rows = 0; 
  66.          
  67.         try { 
  68.             Class.forName("com.mysql.jdbc.Driver"); 
  69.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
  70.              
  71.             String sql = "update t_user set address=?,username=? where id=?"; 
  72.             sta = con.prepareStatement(sql); 
  73.             sta.setString(1, user.getAddress()); 
  74.             sta.setString(2, user.getUsername()); 
  75.             sta.setInt(3, user.getId()); 
  76.              
  77.             rows = sta.executeUpdate(); 
  78.             if(rows > 0) { 
  79.                 System.out.println("ok"); 
  80.             } 
  81.         } catch (ClassNotFoundException e) { 
  82.             e.printStackTrace(); 
  83.         } catch (SQLException e) { 
  84.             e.printStackTrace(); 
  85.         } finally { 
  86.             if(sta != null) { 
  87.                 try { 
  88.                     sta.close(); 
  89.                 } catch (SQLException e) { 
  90.                     e.printStackTrace(); 
  91.                 } finally { 
  92.                     if(con != null) { 
  93.                         try { 
  94.                             con.close(); 
  95.                         } catch (SQLException e) { 
  96.                             e.printStackTrace(); 
  97.                         } 
  98.                     } 
  99.                 } 
  100.             } 
  101.         } 
  102.         return rows; 
  103.     } 
  104.      
  105.      
  106.     public int delete(int id) { 
  107.         Connection con = null; 
  108.         PreparedStatement sta = null; 
  109.         int rows = 0; 
  110.          
  111.         try { 
  112.             Class.forName("com.mysql.jdbc.Driver"); 
  113.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
  114.              
  115.             String sql = "delete from t_user where id=?"; 
  116.             sta = con.prepareStatement(sql); 
  117.             sta.setInt(1, id); 
  118.              
  119.             rows = sta.executeUpdate(); 
  120.             if(rows > 0) { 
  121.                 System.out.println("ok,ok,ok"); 
  122.             } 
  123.              
  124.         } catch (ClassNotFoundException e) { 
  125.             e.printStackTrace(); 
  126.         } catch (SQLException e) { 
  127.             e.printStackTrace(); 
  128.         } finally { 
  129.             if(sta != null) { 
  130.                 try { 
  131.                     sta.close(); 
  132.                 } catch (SQLException e) { 
  133.                     e.printStackTrace(); 
  134.                 } finally { 
  135.                     if(con != null) { 
  136.                         try { 
  137.                             con.close(); 
  138.                         } catch (SQLException e) { 
  139.                             e.printStackTrace(); 
  140.                         } 
  141.                     } 
  142.                 } 
  143.             } 
  144.         } 
  145.         return rows; 
  146.     } 
  147.      
  148.      
  149.      
  150.     public void retrieve(int id) { 
  151.         Connection con = null; 
  152.         PreparedStatement sta = null; 
  153.         ResultSet rs = null; 
  154.          
  155.         try { 
  156.             Class.forName("com.mysql.jdbc.Driver"); 
  157.             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
  158.              
  159.             String sql = "select id,username,address from t_user where id=?"; 
  160.             sta = con.prepareStatement(sql); 
  161.             sta.setInt(1, id); 
  162.              
  163.             rs = sta.executeQuery(); 
  164.             if(rs.next()) { 
  165.                 int idd = rs.getInt("id"); 
  166.                 String username = rs.getString("username"); 
  167.                 String adrress = rs.getString("address"); 
  168.                 System.out.println(idd+"\t"+username+"\t"+adrress); 
  169.             } 
  170.              
  171.         } catch (ClassNotFoundException e) { 
  172.             e.printStackTrace(); 
  173.         } catch (SQLException e) { 
  174.             e.printStackTrace(); 
  175.         } finally { 
  176.             if(rs != null) { 
  177.                 try { 
  178.                     rs.close(); 
  179.                 } catch (SQLException e) { 
  180.                     e.printStackTrace(); 
  181.                 } finally { 
  182.                     if(sta != null) { 
  183.                         try { 
  184.                             sta.close(); 
  185.                         } catch (SQLException e) { 
  186.                             e.printStackTrace(); 
  187.                         } finally { 
  188.                             if(con != null) { 
  189.                                 try { 
  190.                                     con.close(); 
  191.                                 } catch (SQLException e) { 
  192.                                     e.printStackTrace(); 
  193.                                 } 
  194.                             } 
  195.                         } 
  196.                     } 
  197.                 } 
  198.             } 
  199.         } 
  200.     } 
  201.      

 看dao中的内容,我们发现几个方法中的代码有很多重复的!!所以在下一篇文章中,我们要对代码进行优化

 

我把前三篇文章的例子,包括数据库,放在一个压缩包里!名称为:jdbc学习例子.rar

需要的同学可以去下载!!

--结束END--

本文标题: jdbc学习总结3------javab

本文链接: https://www.lsjlt.com/news/189896.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • jdbc学习总结3------javab
     1.测试类的内容: 在包:com.hanchao.test中   package com.hanchao.test;  import com.hanchao.dao.UserDao; import com.hanchao.entity.U...
    99+
    2023-01-31
    jdbc javab
  • BGP学习 总结3
      BGP DECISION PROCESS 1 largest weight ( local orininated path :32768 other 0 ) 2 largest local-preefernce (BGP default...
    99+
    2023-01-31
    BGP
  • python 学习总结3
    Python蟒蛇绘制 一、实现程序如下 1 import turtle 2 turtle.setup (650, 350, 200, 200)#turtle的绘图窗体turtle.setup(width, height, start...
    99+
    2023-01-30
    python
  • AIX 5L学习总结3
    1.-bash-3.00$ prtconf | more //查看配置信息可以看到大部分配置信息。 -bash-3.00$ prtconf | more System Model: IBM,9110-51A Machin...
    99+
    2023-01-31
    AIX
  • Kubernetes 学习总结(3) M
    APIserver符合RESTful风格,支持GET/PUT/DELETE/POST等各种操作。所以也支持kubectl通过一系列命令对各处资源进行管理控制。常用的资源1)、workLoad(工作负载型资源,运行APP,对外提供服务): P...
    99+
    2023-01-31
    Kubernetes
  • Java 多线程学习总结3
    在上一篇中,我们当然希望a++,b++执行完之后,show方法再来show.我们需要的是“原子”动作,一次性地把a++,b++不间断地执行。在java中是利用“互斥”的方法,互斥谁呢?互斥的是相同对象的加锁代码。如果我们把第一篇的SomeB...
    99+
    2023-01-31
    多线程 Java
  • Mongodb学习总结
    Mongodb相关操作总结 2020/4/2 Mongodb使用的是类似与json字符串的形式存储数据 [ { key:value }, { key:value }, ] Mongodb使用了不存在的对...
    99+
    2019-08-19
    Mongodb学习总结
  • mysqlimport学习总结
    原文链接: https://www.modb.pro/db/23208xy 摘要:mysqlimport是MySQL数据库提供的一个命令行程序,可用于数据导入。...
    99+
    2022-10-18
  • WorkFlow学习总结
     最近在工作中要实现一种“流程审批”的功能,查阅资料得知,workFlow技术可以满足我的需求,于是就开始沉下心来好好学习一下这门技术。总结的学习资料也拿出来和大家共享一下。  什么是工作流: 工作流...
    99+
    2023-06-05
  • python 学习总结5
    字符串类型及操作 一、字符串类型的表示   (1)字符串:由0个或多个字符组成的有序字符序列     例如:“请输入带有符号的温度值” 或者‘c’都是字符串   (2)字符串是字符的有序序列,可以对其中的字符进行索引     例如:“请”...
    99+
    2023-01-30
    python
  • 函数学习总结
    定义 自己总结:就相当于现实中各种用途的工具,有着对数据进行各种处理的功能(实质就是比较复杂的变量?!) 分类  自定义函数和Python语言已经定义过的常用的内置函数 自定义函数的组成部分 def 函数名(参数1,参数2...): '...
    99+
    2023-01-31
    函数
  • Python学习总结__Day1
    一、Python是一门什么类型语言 1、解释型:一边编译一边执行,劣势是运行速度慢,但通过运用PyPy交互解释器(JIT技术)会让python程序执行速度快很多。优势是可移植性强。 2、强类型:即类型安全类型。除非通过强制转换,否则变量类...
    99+
    2023-01-31
    Python
  • python学习总结-----pytho
    一、python简介    python 是一种面向对象、解释性的脚本语言,遵循 GPL 协议。语法简单、社区强大、丰富的库支持。又被称为“胶水语言”。能把其他语言(主要C/C++)写的模块很轻松的结合在一起。二、python 安装 win...
    99+
    2023-01-31
    python pytho
  • mysql学习总结(一)
        作为一名小白,今天开始上传自己的学习总结。 ...
    99+
    2016-11-21
    mysql学习总结(一)
  • python 学习总结2
    温度转换问题 一、温度转换   目前有两种表示温度的方法一种是摄氏度另一种是华氏度,摄氏度的结冰点为0度,沸点为100度将温度等分刻画,华氏度的结冰点为32度,沸点为212度将温度进行等刻度划分。   现需要将按格式输入的摄氏度转换为华氏...
    99+
    2023-01-30
    python
  • python 学习总结1
    计算机与程序设计 一、计算机的概念   1.计算机是根据指令操作数据的设备。   2.计算机主要包括两个功能性一个是功能性另一个是计算性   功能性是对数据的操作,表现为数据计算,输入输出处理和结果存储   可编程性是根据一系列指令自动的...
    99+
    2023-01-30
    python
  • python 学习总结4
    数字类型及操作 一、整数类型   (1)python中的整数与数学中的概念是一致的,可以正也可以负,没有取值范围。        pow(x,y)函数是计算x的y次幂,想计算多大就多大。   (2)在整数类型中有四种进制形式      十...
    99+
    2023-01-30
    python
  • Python学习总结(二)----pyt
         继续学习python中,越来越发现python的方便,也找到了一些python与C/C++的一些相同点与不同点。由于我看的书中缺乏编程练习题,我就在想如何能够尽快地熟悉python。由于我一直在参加算法竞赛,所以就想到了用Pyth...
    99+
    2023-01-31
    Python pyt
  • spring框架学习总结
    目录Spring 框架概述Spring优点Spring体系结构Spring拓展Spring Boot与Spring CloudSpring IoC 容器 (IoC 也称为依赖项注入(...
    99+
    2022-11-12
  • list学习内容总结
    list定义:list[]或者[]内任意类型的内容,多个用逗号分开 name1 = list(['Cyberpunk2077', 'Jim', 2077]) name2 = list('Cyberpunk2077') name3 = ...
    99+
    2023-01-31
    内容 list
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作