2020久久超碰欧美精品最新亚洲欧美日韩久久精品,国产福利电影一区二区三区,亚洲欧美日韩一区在线观看,亚洲国产欧美日韩欧美特级,亚洲欧美日韩成人一区久久,欧美日韩精品一区二区三区不卡,国产欧美日韩va另类影音先锋,亚洲欧美日韩久久精品,亚洲欧美日韩国产成人精品影院,亚洲国产欧美日韩精品一区二区三区,欧美日韩国产成人高清视频,日韩久久精品国产免费观看频道,久久人人爽人人爽从片av高清,国产精品综合一区二区

首頁技術文章正文

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開發培訓課程

分享到:
在線咨詢 我要報名
和我們在線交談!