首页技术文章正文

jdbc连接oracle数据库步骤代码

更新时间:2021-08-06 来源:黑马程序员 浏览量:

IT培训班


(一)创建工程,引入驱动包

在下图目录中可以找到驱动包ORACLE的JDBC驱动包,拷贝到工程即可使用

jdbc链接oracel数据库01

创建java工程waterboss,建立lib文件夹,将ojdbc.jar拷贝到此文件夹,然后add build path

JDBC链接oracle数据02

(二)BaseDao

我们通常编写BaseDao负责加载驱动,获取数据库连接,关闭资源,代码如下:

package cn.itcast.waterboss.dao;

import java.sql.SQLException;

/**
 * 基本数据访问类
 * @author Administrator
 *
 */
public class BaseDao {
  //加载驱动
  static {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  /**
   * 获取数据库连接
   * @return
   * @throws SQLException
   */
  public static java.sql.Connection getConnection() throws SQLException {
    return java.sql.DriverManager.getConnection(
      "jdbc:oracle:thin:@192.168.80.10:1521:orcl",
      "wateruser",
      "itcast"
    );
  }

  /**
   * 关闭资源
   * @param rs
   * @param stmt
   * @param conn
   */
  public static void closeAll(
    java.sql.ResultSet rs,
    java.sql.Statement stmt,
    java.sql.Connection conn
  ) {
    //关闭结果集
    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    //关闭执行对象
    if (stmt != null) {
      try {
        stmt.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    //关闭执行对象
    if (conn != null) {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

JDBC驱动为:

oracle.jdbc.OracleDriver

连接字符串( 瘦连接 ):

jdbc:oracle:thin:@虚拟机的 IP:1521:orcl


(三)业主增删改代码编写

1.创建实体类

package cn.itcast.waterboss.entity;

import java.util.Date;

/**
 * 业主实体类
 * @author Administrator
 *
 */
public class Owners {

  private Long id; //编号
  private String name; //业主名称
  private Long addressid; //地址编号
  private String housenumber; //门牌号
  private String watermeter; //水表编号
  private Date adddate; //登记日期
  private Long ownertypeid; //业主类型 ID

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Long getAddressid() {
    return addressid;
  }

  public void setAddressid(Long addressid) {
    this.addressid = addressid;
  }

  public String getHousenumber() {
    return housenumber;
  }

  public void setHousenumber(String housenumber) {
    this.housenumber = housenumber;
  }

  public String getWatermeter() {
    return watermeter;
  }

  public void setWatermeter(String watermeter) {
    this.watermeter = watermeter;
  }

  public Date getAdddate() {
    return adddate;
  }

  public void setAdddate(Date adddate) {
    this.adddate = adddate;
  }

  public Long getOwnertypeid() {
    return ownertypeid;
  }

  public void setOwnertypeid(Long ownertypeid) {
    this.ownertypeid = ownertypeid;
  }
}

2.创建Dao类实现增删改

package cn.itcast.waterboss.dao;

import cn.itcast.waterboss.entity.Owners;
import java.sql.SQLException;

/**
 * 业主数据访问类
 * @author Administrator
 *
 */
public class OwnersDao {

  /**
   * 新增业主
   * @param owners
   */
  public static void add(Owners owners) {
    java.sql.Connection conn = null;
    java.sql.PreparedStatement stmt = null;
    try {
      conn = BaseDao.getConnection();
      stmt =
        conn.prepareStatement("insert into T_OWNERS 
values(?,?,?,?,?,?,?)");
      stmt.setLong(1, owners.getId());
      stmt.setString(2, owners.getName());
      stmt.setLong(3, owners.getAddressid());
      stmt.setString(4, owners.getHousenumber());
      stmt.setString(5, owners.getWatermeter());
      stmt.setDate(6, new java.sql.Date(owners.getAdddate().getTime()));
      stmt.setLong(7, owners.getOwnertypeid());
      stmt.execute();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      BaseDao.closeAll(null, stmt, conn);
    }
  }

  /**
   * 修改业主
   * @param owners
   */
  public static void update(Owners owners) {
    java.sql.Connection conn = null;
    java.sql.PreparedStatement stmt = null;
    try {
      conn = BaseDao.getConnection();
      stmt =
        conn.prepareStatement(
          "update T_OWNERS set 
    name=?,addressid=?,housenumber=?," +
          "watermeter=?,adddate=?, ownertypeid=? where 
    id=?"
        );
      stmt.setString(1, owners.getName());
      stmt.setLong(2, owners.getAddressid());
      stmt.setString(3, owners.getHousenumber());
      stmt.setString(4, owners.getWatermeter());
      stmt.setDate(5, new java.sql.Date(owners.getAdddate().getTime()));
      stmt.setLong(6, owners.getOwnertypeid());
      stmt.setLong(7, owners.getId());
      stmt.execute();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      BaseDao.closeAll(null, stmt, conn);
    }
  }

  /**
   * 删除业主
   * @param owners
   */
  public static void delete(Long id) {
    java.sql.Connection conn = null;
    java.sql.PreparedStatement stmt = null;
    try {
      conn = BaseDao.getConnection();
      stmt = conn.prepareStatement("delete from T_OWNERS where 
id=?");
      stmt.setLong(1, id);
      stmt.execute();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      BaseDao.closeAll(null, stmt, conn);
    }
  }
}


将本页面链接发给QQ:2217622915获取ORACLE全套视频教程+笔记+源码


猜你喜欢:

Oracle数据库图文教程_CentOS 6.9系统

Oracle数据库与MySQL数据库在数据存储上有什么差异?

Nginx安装和目录结构介绍

黑马程序员java开发培训课程

分享到:
在线咨询 我要报名
和我们在线交谈!