我是靠谱客的博主 背后美女,这篇文章主要介绍JDBC连接Oracle数据库,并于之交互,现在分享给大家,希望可以做个参考。

理论知识摘要

  1. JDBC:Java dataBase connectivity 称为Java数据库连接,它是一种用于数据库访问的应用程序API,由一组用Java语言编写的类和接口组成,有了JDBC就可以用同一语法对多种关系型数据库进行访问,而不用担心其数据库操作语言的差异。比如,就不用 为了访问Mysql数据库写了一个程序而访问Oracle的时候又写一个程序等等
    java应用程序和不同的关系型数据库之间的连接通过jdbc实现
  2. 数据库驱动程序的分类:
    jdbc-odbc桥:把JDBC API调用转换成ODBC API 调用, 然后ODBC API调用针对供应商的ODBC 驱动程序来访问数据库, 即利用JDBC- ODBC 桥通过ODBC来存储数据源 。
    本地API驱动:本地api驱动直接把jdbc调用转变为数据库的标准调用再去访问数据库. 这种方法需要本地数据库驱动代码。
    网络协议驱动:它使用一种与具体数据库无关的协议将数据库请求发送给一个中间服务器
    本地协议驱动:这种驱动直接把jdbc调用转换为符合相关数据库系统规范的请求.由于改类型的驱动写的应用可以直接和数据库服务器通讯,这种类型的驱动完全由java实现,因此实现了平台独立性。
    数据库驱动程序
    3.DAO设计模式[data access object 数据存取对象]
    dao起着转换器的作用,把实体类和数据库中的表对应了起来。把业务逻辑和持久化数据隔离开来
    dao模式的作用

实现连接到Oracle数据库并对其进行操作

1.将 加载Oracle数据库驱动、连接到数据、关闭数据库三种操作封装到com.dadong.util.BaseConn.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.dadong.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * 基础连接类 * 方法: * 静态方法 加载驱动程序 * 公有方法 建立数据库连接 * 公有方法 关闭数据库连接 * @author Mr * */ public class BaseConn { //静态方法执行之前就已经加载了驱动程序 static{ try { // Class.forName("com.mysql.cj.jdbc.Driver");//mysql连接驱动官方新版本中驱动的加载转移到另一个包下 Class.forName("oracle.jdbc.OracleDriver");//这里使用的是oracle数据库驱动,待会要和Oracle进行交互 System.out.println("loading Success!!!"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 建立与数据库的连接 * @param url a database url of the form jdbc:subprotocol:subname * @param user 用户名 * @param pwd 密码 * @return 数据库连接/null */ public Connection getConnection(String url, String user, String pwd){ Connection connection = null; try { connection = DriverManager.getConnection(url, user, pwd);//DriverManager驱动管理获取一个数据库的会话 System.out.println("connection Success!!!"); } catch (SQLException e) { e.printStackTrace(); } return connection; } //关闭与数据库的连接 public void closeConnection(Connection connection){ if(connection!=null){ try { connection.close();//释放数据库连接和JDBC资源 System.out.println("close jdbc`s resources Success!!!"); } catch (SQLException e) { e.printStackTrace(); } } } }
2.建立和Oracle Scott数据库中EMP表一致的实体类com.dadong.entity.Emp.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.dadong.entity; import java.sql.Date; /** * 数据库表emp对应的实体类 * @author Mr * */ public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Integer sal; private Integer comm; private Integer deptno; public Emp(){ } public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Integer sal, Integer comm, Integer deptno) { super(); this.empno = empno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.deptno = deptno; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Integer getSal() { return sal; } public void setSal(Integer sal) { this.sal = sal; } public Integer getComm() { return comm; } public void setComm(Integer comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]"; } }
3.DAO设计模式的应用com.dadong.dao.EmpDao.java
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.dadong.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.dadong.entity.Emp; import com.dadong.util.BaseConn; /** * 数据库表EMP的操作类 * OR映射 object <--> relation * @author Mr * */ public class EmpDao { /*String url = "jdbc:mysql://127.0.0.1:3306/dadong?useUnicode=true&characterEncoding=utf-8&useSSL=false"; String user = "root"; String pwd = "123456";*/ String url = "jdbc:oracle:thin:@127.0.0.1:1521/xe"; String user = "scott"; String pwd = "tiger"; BaseConn bConn = new BaseConn(); public List<Emp> selectList(){ List<Emp> emps = new ArrayList<Emp>(); Connection connection = bConn.getConnection(url, user, pwd); String sql = "select * from emp"; try { PreparedStatement pst = connection.prepareStatement(sql);//预处理sql ResultSet rSet = pst.executeQuery();//执行查询 while(rSet.next()){ emps.add(new Emp(rSet.getInt("empno"), rSet.getString("ename"), rSet.getString("job"), rSet.getInt("mgr"), rSet.getDate("hiredate"), rSet.getInt("sal"), rSet.getInt("comm"), rSet.getInt("deptno"))); } pst.close();//关闭查询,自动commit } catch (SQLException e) { e.printStackTrace(); } finally { bConn.closeConnection(connection);//写在finally中的好处是,无论是否抛出异常,数据库连接最后都会发关闭,避免出现连接过多的情况 } return emps; } }
4.测试,从连接Oracle数据库到从数据库emp表中读取数据打印到控制台
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.dadong.test; import java.util.List; import com.dadong.dao.EmpDao; import com.dadong.entity.Emp; public class Test { public static void main(String[] args) { EmpDao empDao = new EmpDao(); List<Emp> emps = empDao.selectList(); for(Emp item: emps){ System.out.println(item.toString()); } } }

测试结果

补充

Oracle数据库的驱动程序可以取官网下载,如果安装了Oracle客户端,直接到 安装目录/jdbc/lib下去查找合适的版本。
jdk1.5—–ojdbc5.jar
jdk1.6—–ojdbc6.jar
jdk1.7—–ojdbc6.jar
jdk1.8—–ojdbc6.jar(最新的)

最后

以上就是背后美女最近收集整理的关于JDBC连接Oracle数据库,并于之交互的全部内容,更多相关JDBC连接Oracle数据库内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部