欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oracle远程连接配置(oracle配置远程连接数据库教程)

程序员文章站 2023-11-24 22:14:52
(一)创建工程,引入驱动包在下图目录中可以找到驱动包oracle的jdbc驱动包,拷贝到工程即可使用创建java工程waterboss,建立lib文件夹,将ojdbc.jar拷贝到此文件夹,然后add...

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

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

oracle远程连接配置(oracle配置远程连接数据库教程)

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

oracle远程连接配置(oracle配置远程连接数据库教程)

(二)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);
    }
  }
}