在下图目录中可以找到驱动包oracle的jdbc驱动包,拷贝到工程即可使用
创建java工程waterboss,建立lib文件夹,将ojdbc.jar拷贝到此文件夹,然后add build path
我们通常编写badao负责加载驱动,获取数据库连接,关闭资源,代码如下:
package cn.itcast.waterboss.dao;import java.sql.sqlexception;/** * 基本数据访问类 * @author administrator * */public class badao { //加载驱动 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", "waterur", "itcast" ); } /** * 关闭资源 * @param rs * @param stmt * @param conn */ public static void cloall( java.sql.resultt rs, java.sql.statement stmt, java.sql.connection conn ) { //关闭结果集 if (rs != null) { try { rs.clo(); } catch (sqlexception e) { e.printstacktrace(); } } //关闭执行对象 if (stmt != null) { try { stmt.clo(); } catch (sqlexception e) { e.printstacktrace(); } } //关闭执行对象 if (conn != null) { try { conn.clo(); } 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 hounumber; //门牌号 private string watermeter; //水表编号 private date adddate; //登记日期 private long ownertypeid; //业主类型 id public long getid() { return id; } public void tid(long id) { this.id = id; } public string getname() { return name; } public void tname(string name) { this.name = name; } public long getaddressid() { return addressid; } public void taddressid(long addressid) { this.addressid = addressid; } public string gethounumber() { return hounumber; } public void thounumber(string hounumber) { this.hounumber = hounumber; } public string getwatermeter() { return watermeter; } public void twatermeter(string watermeter) { this.watermeter = watermeter; } public date getadddate() { return adddate; } public void tadddate(date adddate) { this.adddate = adddate; } public long getownertypeid() { return ownertypeid; } public void townertypeid(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 = badao.getconnection(); stmt = conn.preparestatement("inrt into t_owners values(?,?,?,?,?,?,?)"); stmt.tlong(1, owners.getid()); stmt.tstring(2, owners.getname()); stmt.tlong(3, owners.getaddressid()); stmt.tstring(4, owners.gethounumber()); stmt.tstring(5, owners.getwatermeter()); stmt.tdate(6, new java.sql.date(owners.getadddate().gettime())); stmt.tlong(7, own线面平行判定定理ers.getownertypeid()); stmt.execute(); } catch (sqlexception e) { e.printstacktrace(); } finally { badao.cloall(null, stmt, conn); } } /** * 修改业主 * @param owners */ public static void update(owners owners) { java.sql.connection conn = null; java.sql.preparedstatement stmt = null; try { conn = badao.getconnection(); st海上日出教案mt = conn.preparestatement( "update t_owners t name=?,addressid=?,hounumber=?," + "watermeter=?,adddate=?, ownertypeid=? where id=?" ); stmt.tstring(1, owners.getname()); stmt.tlong(2, owners.getaddressid()); stmt.tstring(3, owners.gethounumber()); stmt.tstring(4, owners.getwatermeter()); stmt.tdate(5, new java.sql.date(owners.getadddate().gettime())); stmt.tlong(6, owners.getownertypeid()); stmt.tlong(7, owners.getid()); stmt.execute(); } catch (sqlexception e) { e.printstacktrace(); } finally { badao.cloall(null, stmt, conn); 带有月亮的诗句} } /** * 删除业主 * @param owners */ public static void delete(long id) { java.sql.connection conn = null; java.sql.preparedstatement stmt = null; try { conn = badao.getconnection(); stmt = conn.preparestatement("delete from t_owners where id=?"); stmt.tlong(1, id); stmt.execute(); } catch (sqlexception e) { e.printstacktrace(); } finally { badao.cloall(null, stmt, conn); } }}
本文发布于:2023-04-05 11:39:34,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/6a70f7873d60a35c0ab161bde3b623aa.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:oracle远程连接配置(oracle配置远程连接数据库教程).doc
本文 PDF 下载地址:oracle远程连接配置(oracle配置远程连接数据库教程).pdf
留言与评论(共有 0 条评论) |