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


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

文章插图
(一)创建工程,引入驱动包在下图目录中可以找到驱动包ORACLE的JDBC驱动包,拷贝到工程即可使用
创建java工程waterboss,建立lib文件夹,将ojdbc.jar拷贝到此文件夹,然后add build path
(二)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 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);}}/*** 删除业主* @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);}}}


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: