竹笋

首页 » 问答 » 灌水 » JDBC连接oracle数据库步骤黑马
TUhjnbcbe - 2023/4/23 19:23:00
湖南白癜风医院 http://baijiahao.baidu.com/s?id=1714948075655183856&wfr=spider&for=pc

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

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

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

(二)BaseDao

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

packagecn.itcast.waterboss.dao;importjava.sql.SQLException;/***基本数据访问类*

authorAdministrator**/publicclassBaseDao{//加载驱动static{try{Class.forName("oracle.jdbc.driver.OracleDriver");}catch(ClassNotFoundExceptione){e.printStackTrace();}}/***获取数据库连接*

return*

throwsSQLException*/publicstaticjava.sql.ConnectiongetConnection()throwsSQLException{returnjava.sql.DriverManager.getConnection("jdbc:oracle:thin:

..80.10::orcl","wateruser","itcast");}/***关闭资源*

paramrs*

paramstmt*

paramconn*/publicstaticvoidcloseAll(java.sql.ResultSetrs,java.sql.Statementstmt,java.sql.Connectionconn){//关闭结果集if(rs!=null){try{rs.close();}catch(SQLExceptione){e.printStackTrace();}}//关闭执行对象if(stmt!=null){try{stmt.close();}catch(SQLExceptione){e.printStackTrace();}}//关闭执行对象if(conn!=null){try{conn.close();}catch(SQLExceptione){e.printStackTrace();}}}}JDBC驱动为:

oracle.jdbc.OracleDriver

连接字符串(瘦连接):

jdbc:oracle:thin:

虚拟机的IP::orcl

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

1.创建实体类

packagecn.itcast.waterboss.entity;importjava.util.Date;/***业主实体类*

authorAdministrator**/publicclassOwners{privateLongid;//编号privateStringname;//业主名称privateLongaddressid;//地址编号privateStringhousenumber;//门牌号privateStringwatermeter;//水表编号privateDateadddate;//登记日期privateLongownertypeid;//业主类型IDpublicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicLonggetAddressid(){returnaddressid;}publicvoidsetAddressid(Longaddressid){this.addressid=addressid;}publicStringgetHousenumber(){returnhousenumber;}publicvoidsetHousenumber(Stringhousenumber){this.housenumber=housenumber;}publicStringgetWatermeter(){returnwatermeter;}publicvoidsetWatermeter(Stringwatermeter){this.watermeter=watermeter;}publicDategetAdddate(){returnadddate;}publicvoidsetAdddate(Dateadddate){this.adddate=adddate;}publicLonggetOwnertypeid(){returnownertypeid;}publicvoidsetOwnertypeid(Longownertypeid){this.ownertypeid=ownertypeid;}}2.创建Dao类实现增删改

packagecn.itcast.waterboss.dao;importcn.itcast.waterboss.entity.Owners;importjava.sql.SQLException;/***业主数据访问类*

authorAdministrator**/publicclassOwnersDao{/***新增业主*

paramowners*/publicstaticvoidadd(Ownersowners){java.sql.Connectionconn=null;java.sql.PreparedStatementstmt=null;try{conn=BaseDao.getConnection();stmt=conn.prepareStatement("insertintoT_OWNERSvalues(?,?,?,?,?,?,?)");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,newjava.sql.Date(owners.getAdddate().getTime()));stmt.setLong(7,owners.getOwnertypeid());stmt.execute();}catch(SQLExceptione){e.printStackTrace();}finally{BaseDao.closeAll(null,stmt,conn);}}/***修改业主*

paramowners*/publicstaticvoidupdate(Ownersowners){java.sql.Connectionconn=null;java.sql.PreparedStatementstmt=null;try{conn=BaseDao.getConnection();stmt=conn.prepareStatement("updateT_OWNERSsetname=?,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,newjava.sql.Date(owners.getAdddate().getTime()));stmt.setLong(6,owners.getOwnertypeid());stmt.setLong(7,owners.getId());stmt.execute();}catch(SQLExceptione){e.printStackTrace();}finally{BaseDao.closeAll(null,stmt,conn);}}/***删除业主*

paramowners*/publicstaticvoiddelete(Longid){java.sql.Connectionconn=null;java.sql.PreparedStatementstmt=null;try{conn=BaseDao.getConnection();stmt=conn.prepareStatement("deletefromT_OWNERSwhereid=?");stmt.setLong(1,id);stmt.execute();}catch(SQLExceptione){e.printStackTrace();}finally{BaseDao.closeAll(null,stmt,conn);}}}

1
查看完整版本: JDBC连接oracle数据库步骤黑马