
文章插圖
(一)創(chuàng)建工程,引入驅動包在下圖目錄中可以找到驅動包ORACLE的JDBC驅動包,拷貝到工程即可使用
創(chuàng)建java工程waterboss,建立lib文件夾,將ojdbc.jar拷貝到此文件夾,然后add build path
(二)BaseDao我們通常編寫B(tài)aseDao負責加載驅動,獲取數(shù)據(jù)庫連接,關閉資源,代碼如下:
package cn.itcast.waterboss.dao;import java.sql.SQLException;/** * 基本數(shù)據(jù)訪問類 * @author Administrator * */public class BaseDao {//加載驅動static {try {Class.forName("oracle.jdbc.driver.OracleDriver");} catch (ClassNotFoundException e) {e.printStackTrace();}}/*** 獲取數(shù)據(jù)庫連接* @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();}}//關閉執(zhí)行對象if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}//關閉執(zhí)行對象if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}}JDBC驅動為:
oracle.jdbc.OracleDriver
連接字符串( 瘦連接 ):
jdbc:oracle:thin:@虛擬機的 IP:1521:orcl
(三)業(yè)主增刪改代碼編寫1.創(chuàng)建實體類
package cn.itcast.waterboss.entity;import java.util.Date;/** * 業(yè)主實體類 * @author Administrator * */public class Owners {private Long id; //編號private String name; //業(yè)主名稱private Long addressid; //地址編號private String housenumber; //門牌號private String watermeter; //水表編號private Date adddate; //登記日期private Long ownertypeid; //業(yè)主類型 IDpublic 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.創(chuàng)建Dao類實現(xiàn)增刪改
package cn.itcast.waterboss.dao;import cn.itcast.waterboss.entity.Owners;import java.sql.SQLException;/** * 業(yè)主數(shù)據(jù)訪問類 * @author Administrator * */public class OwnersDao {/*** 新增業(yè)主* @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);}}/*** 修改業(yè)主* @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 setname=?,addressid=?,housenumber=?," +"watermeter=?,adddate=?, ownertypeid=? whereid=?");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);}}/*** 刪除業(yè)主* @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);}}}
以上關于本文的內容,僅作參考!溫馨提示:如遇健康、疾病相關的問題,請您及時就醫(yī)或請專業(yè)人士給予相關指導!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內容,希望對您有所幫助:- ipad10.2英寸參數(shù)配置 ipad多大尺寸合適
- 一加3系列參數(shù)對比 一加3參數(shù)配置詳情
- 超市滅火器配置規(guī)范
- 手把手教你配置ip地址 192.168.1.1的首選dns
- 遠程異地文件共享方法 mac共享文件夾在哪里
- 詳解聯(lián)想拯救者r720詳細參數(shù) 聯(lián)想y720筆記本配置
- 蘋果12和12pro哪個值得買 蘋果12pro參數(shù)配置詳細
- ios上玩安卓游戲的方法 安卓模擬器電腦配置要求
- 小米9透明版價格_小米9
- 魅族手機體驗簡評 魅族pro6參數(shù)配置
