jdbc操作oracle数据库(增删改查)_第1页
jdbc操作oracle数据库(增删改查)_第2页
jdbc操作oracle数据库(增删改查)_第3页
jdbc操作oracle数据库(增删改查)_第4页
jdbc操作oracle数据库(增删改查)_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

jdbc 操作操作 oracle 数据库数据库 增删改查增删改查 博客分类 博客分类 JAVA 积累 SQLOracleJDBCDAO DAOFactory java Java 代码代码 1 package cn mldn lxh factory 2 3 import cn mldn lxh dao 4 import cn mldn lxh dao impl 5 6 public class DAOFactory 7 8 public static PersonDAO getPersonDAOInstance 9 10 return new PersonDAOImpl 11 12 DataBaseConnection java Java 代码代码 1 package cn mldn lxh dbc 2 import java sql 3 4 主要功能就是连接数据库 关闭数据库 5 public class DataBaseConnection 6 7 private final String DBDRIVER oracle jdbc driver OracleDriver 8 private final String DBURL jdbc oracle thin localhost 1521 MLDN 9 private final String DBUSER scott 10 private final String DBPASSWORD tiger 11 private Connection conn null 12 13 public DataBaseConnection 14 15 try 16 17 Class forName DBDRIVER 18 this conn DriverManager getConnection DBURL DBUSER DBPASSWORD 19 20 catch Exception e 21 22 23 24 25 取得数据库连接 26 public Connection getConnection 27 28 return this conn 29 30 31 关闭数据库连接 32 public void close 33 34 try 35 36 this conn close 37 38 catch Exception e 39 40 41 42 Person java Java 代码代码 1 package cn mldn lxh vo 2 3 值对象 包含属性 setter getter 方法 4 public class Person 5 6 private String id 7 private String name 8 private String password 9 private int age 10 private String email 11 12 生成 getter setter 方法 13 public void setId String id 14 15 this id id 16 17 public void setName String name 18 19 this name name 20 21 public void setPassword String password 22 23 this password password 24 25 public void setAge int age 26 27 this age age 28 29 public void setEmail String email 30 31 this email email 32 33 public String getId 34 35 return this id 36 37 public String getName 38 39 return this name 40 41 public String getPassword 42 43 return this password 44 45 public int getAge 46 47 return this age 48 49 public String getEmail 50 51 return this email 52 53 PersonDAO java Java 代码代码 1 package cn mldn lxh dao 2 3 import java util 4 import cn mldn lxh vo 5 6 规定出了操作 person 表在此项目里的全部方法 7 public interface PersonDAO 8 9 增加操作 10 public void insert Person person throws Exception 11 修改操作 12 public void update Person person throws Exception 13 删除操作 14 public void delete String id throws Exception 15 按 ID 查询操作 16 public Person queryById String id throws Exception 17 查询全部 18 public List queryAll throws Exception 19 模糊查询 20 public List queryByLike String cond throws Exception 21 PersonDAOImpl java Java 代码代码 1 package cn mldn lxh dao impl 2 import java sql 3 import java util 4 import cn mldn lxh vo 5 import cn mldn lxh dbc 6 import cn mldn lxh dao 7 8 此类需要完成具体的数据库操作 需要 JDB 代码 9 public class PersonDAOImpl implements PersonDAO 10 11 增加操作 12 public void insert Person person throws Exception 13 14 String sql INSERT INTO person id name password age email VALUES 15 PreparedStatement pstmt null 16 DataBaseConnection dbc null 17 18 下面是针对数据库的具体操作 19 try 20 21 连接数据库 22 dbc new DataBaseConnection 23 pstmt dbc getConnection prepareStatement sql 24 pstmt setString 1 person getId 25 pstmt setString 2 person getName 26 pstmt setString 3 person getPassword 27 pstmt setInt 4 person getAge 28 pstmt setString 5 person getEmail 29 进行数据库更新操作 30 pstmt executeUpdate 31 pstmt close 32 33 catch Exception e 34 35 throw new Exception 操作出现异常 36 37 finally 38 39 关闭数据库连接 40 dbc close 41 42 43 修改操作 44 public void update Person person throws Exception 45 46 String sql UPDATE person SET name password age email WHERE id 47 PreparedStatement pstmt null 48 DataBaseConnection dbc null 49 50 下面是针对数据库的具体操作 51 try 52 53 连接数据库 54 dbc new DataBaseConnection 55 pstmt dbc getConnection prepareStatement sql 56 pstmt setString 1 person getName 57 pstmt setString 2 person getPassword 58 pstmt setInt 3 person getAge 59 pstmt setString 4 person getEmail 60 pstmt setString 5 person getId 61 进行数据库更新操作 62 pstmt executeUpdate 63 pstmt close 64 65 catch Exception e 66 67 throw new Exception 操作出现异常 68 69 finally 70 71 关闭数据库连接 72 dbc close 73 74 75 删除操作 76 public void delete String id throws Exception 77 78 String sql DELETE FROM person WHERE id 79 PreparedStatement pstmt null 80 DataBaseConnection dbc null 81 82 下面是针对数据库的具体操作 83 try 84 85 连接数据库 86 dbc new DataBaseConnection 87 pstmt dbc getConnection prepareStatement sql 88 pstmt setString 1 id 89 进行数据库更新操作 90 pstmt executeUpdate 91 pstmt close 92 93 catch Exception e 94 95 throw new Exception 操作出现异常 96 97 finally 98 99 关闭数据库连接 100 dbc close 101 102 103 按 ID 查询操作 104 public Person queryById String id throws Exception 105 106 Person person null 107 String sql SELECT id name password age email FROM person WHERE id 108 PreparedStatement pstmt null 109 DataBaseConnection dbc null 110 111 下面是针对数据库的具体操作 112 try 113 114 连接数据库 115 dbc new DataBaseConnection 116 pstmt dbc getConnection prepareStatement sql 117 pstmt setString 1 id 118 进行数据库查询操作 119 ResultSet rs pstmt executeQuery 120 if rs next 121 122 查询出内容 之后将查询出的内容赋值给 person 对象 123 person new Person 124 person setId rs getString 1 125 person setName rs getString 2 126 person setPassword rs getString 3 127 person setAge rs getInt 4 128 person setEmail rs getString 5 129 130 rs close 131 pstmt close 132 133 catch Exception e 134 135 throw new Exception 操作出现异常 136 137 finally 138 139 关闭数据库连接 140 dbc close 141 142 return person 143 144 查询全部 145 public List queryAll throws Exception 146 147 List all new ArrayList 148 String sql SELECT id name password age email FROM person 149 PreparedStatement pstmt null 150 DataBaseConnection dbc null 151 152 下面是针对数据库的具体操作 153 try 154 155 连接数据库 156 dbc new DataBaseConnection 157 pstmt dbc getConnection prepareStatement sql 158 进行数据库查询操作 159 ResultSet rs pstmt executeQuery 160 while rs next 161 162 查询出内容 之后将查询出的内容赋值给 person 对象 163 Person person new Person 164 person setId rs getString 1 165 person setName rs getString 2 166 person setPassword rs getString 3 167 person setAge rs getInt 4 168 person setEmail rs getString 5 169 170 将查询出来的数据加入到 List 对象之中 171 all add person 172 173 rs close 174 pstmt close 175 176 catch Exception e 177 178 throw new Exception 操作出现异常 179 180 finally 181 182 关闭数据库连接 183 dbc close 184 185 return all 186 187 模糊查询 188 public List queryByLike String cond throws Exception 189 190 List all new ArrayList 191 String sql SELECT id name password age email FROM person WHERE name LIKE or email LIKE 192 PreparedStatement pstmt null 193 DataBaseConnection dbc null 194 195 下面是

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论