JDBC连接oracle数据库步骤
(一)创建工程,引入驱动包在下图目次中可以找到驱动包ORACLE的JDBC驱动包,拷贝到工程即可使用
https://p6.toutiaoimg.com/large/pgc-image/eca91e4faddc4bcab522dd233397c976
创建java工程waterboss,建立lib文件夹,将ojdbc.jar拷贝到此文件夹,然后add build path
https://p3.toutiaoimg.com/large/pgc-image/4bd3a46d77344b9f8bfd59e87d0eaee8
(二)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; //业主类型 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.创建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); }}} 还可以再封装一下,动态传递参数,让查询返回list《map》修改返回记录数 转发了
页:
[1]