⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanconn.java

📁 struts教学程序,使用了struts,hibernate等相关技术,做简单的出入库操作
💻 JAVA
字号:
package util.db;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.*;

/**
 * <p>Title: J2EE教学工程</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 西南交通大学</p>
 * @author 唐权华
 * @version 1.0
 */

public class BeanConn implements Serializable {
  private String url = "";
  private String driver = "";
  private String user = "";
  private String pass = "";

  public BeanConn() {
  }

  /**
   * 获得属性文件
   * @return
   */
  private Properties getProperties() {
    Properties initProps = new Properties();
    InputStream in = null;
    try {
        //File file = new File("/db.properties");
        //in = new FileInputStream(file);
        in = getClass().getResourceAsStream("/db.properties");
        initProps.load(in);
    }
    catch (Exception e) {
      System.out.println("请将文件db.properties考贝到WEB-INF/classes下面");
        e.printStackTrace();

    }
    finally {
        try {
            if (in != null) { in.close(); }
        } catch (Exception e) {}
    }

    return initProps;
  }

  private void writeObject(ObjectOutputStream oos) throws IOException {
    oos.defaultWriteObject();
  }
  private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();
  }

  /**
   * 得到数据库连接
   * @return
   * @throws java.lang.Exception
   */
  public Connection getConn() throws Exception{
    Properties initProps = this.getProperties();
    if (initProps!=null) {
      try {
        url = initProps.getProperty("url").trim();
        driver = initProps.getProperty("driver").trim();
        user = initProps.getProperty("user").trim();
        pass = initProps.getProperty("pass").trim();
      }catch(Exception ex) {
        System.out.println("配置文件读取错误 : " + ex.toString());
      }
    }

    Class.forName(driver);
    return DriverManager.getConnection(url,user,pass);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -