你直接在其他的 servlet 或者 jsp 中调用  conn 这个对象吧,有必要放到 web.xml 中吗?

解决方案 »

  1.   

    数据库的连接参数也可以配置在.properties资源文件中
      

  2.   

    一个javabean封装好,用的时候调用就好了嘛
      

  3.   

    你上面说的把数据库操作封装在一个javaBean中,用于被其他的jsp,servlet都是可以的
    你直接把你的javaBean所在的路径导入就可以了
      

  4.   

    放在一个.properties文件中,然后直接用javaBean来读取,会更简单使用!
    无需都往web.xml中挤web.xml中放入这些东西,只能用servlet通过getInitParameter来获取,去到之后还得赋值给javaBean
    可以通过继承策略,把你的servlet在继承一个基类,然后在基类中写这部分代码
      

  5.   

    比如先建立一个db.properties文件,放在com.yourcompany.common包下,文件中内容如下:
    DRIVER=com.mysql.jdbc.Driver
    URL=jdbc:mysql://localhost:3306/chinatravellinks
    USER=root
    PASSWORD=
    你再建立一个类读取改资源文件,读取代码如下:
    ClassLoader cl = this.getClass().getClassLoader(); 
    InputStream is = cl.getResourceAsStream("com/yourcompany/"+db.properties);
    Properties props = new Properties(); 
    props.load(is); 
    String driver= props.getProperty("DRIVER");
    String url= props.getProperty("URL");
    String user= props.getProperty("USER");
    String password= props.getProperty("PASSWORD"); 
      

  6.   

    上面的有点小错误!
    比如先建立一个db.properties文件,放在com.yourcompany.common包下,文件中内容如下: 
    DRIVER=com.mysql.jdbc.Driver 
    URL=jdbc:mysql://localhost:3306/chinatravellinks 
    USER=root 
    PASSWORD= 
    你再建立一个类读取改资源文件,读取代码如下: 
    ClassLoader cl = this.getClass().getClassLoader(); 
    InputStream is = cl.getResourceAsStream("com/yourcompany/common"+db.properties); 
    Properties props = new Properties(); 
    props.load(is); 
    String driver= props.getProperty("DRIVER"); 
    String url= props.getProperty("URL"); 
    String user= props.getProperty("USER"); 
    String password= props.getProperty("PASSWORD"); 
      

  7.   

    1.我如何在封装好的数据库连接javabean(conn.java)里获得servle里得到的参数呢? 设计上有些问题
      

  8.   

    为什么我读不到properties里的内容呢?如果这样,是不是用户访问一次都读它,那性能怎么比得上WEB.XML里呢?
    com.conn.preConn.properties
    DRIVER=com.mysql.jdbc.Driver
    URL=jdbc\:mysql\://localhost\:3306/chinatravellinks
    USER=root
    PASSWORD=com.conn.Conn.java
    public class Conn {
    String driver; // JDBC驱动程序,对应web.xml的driver属性
    String url; // 数据库URL,对应web.xml的url属性
    String user; // 用户名
    String password; // 用户密码 Connection conn;
    Statement stmt;
    ResultSet rs; public Conn() {
    super(); ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream inputStream = classLoader
    .getResourceAsStream("preConn.properties");
    Properties properties = new Properties();
    try {
    properties.load(inputStream);
    driver = properties.getProperty("DRIVER");
    url = properties.getProperty("URL");
    user = properties.getProperty("USER");
    password = properties.getProperty("PASSWORD");
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } // driver = "com.mysql.jdbc.Driver";
    // url = "jdbc:mysql://localhost:3306/chinatravellinks";
    // user = "root";
    // password = null; } /**
     * 功能:初始化连接数据库参数
     * 
     * @param driver
     * @param url
     * @param user
     * @param password
     */
    /**
     * 功能:连接数据库
     * 
     * @return:Connection对象conn
     * @throws Exception
     */
    public Connection getConn() throws Exception {
    try {
    Class.forName(driver);
    conn = (Connection) DriverManager
    .getConnection(url, user, password);
    return conn;
    } catch (SQLException e) {
    System.err.println(e.getMessage());
    throw e;
    }
    } /**
     * 功能:为执行SQL作准备,查询操作时专用 <br>
     * 
     * @return:Statement对象stmt
     */
    public Statement getStmtread() {
    try {
    conn = getConn();
    stmt = (Statement) conn.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    return stmt;
    } catch (Exception e) {
    System.err.println(e.getMessage());
    e.printStackTrace();
    }
    return null;
    } /**
     * 功能:为执行SQL作准备,非查询操作时用 <br>
     * 
     * @return:Statement对象stmt
     */
    public Statement getStmt() {
    try {
    conn = getConn();
    stmt = (Statement) conn.createStatement();
    return stmt;
    } catch (Exception e) {
    System.err.println(e.getMessage());
    e.printStackTrace();
    }
    return null;
    }
    }
      

  9.   

    通过通过.properties来存放数据库属性可以啦..
    可是:
    请问我通过.properties来存放数据库连接属性是不是用户访问数据信息时都会读取里面的连接属性而对性能影响很不好? 
    就是说这种存放数据库连接属性的方法是不可取的?
    用监听器是否可以实现通过web.xml存放连接数据库属性?
      

  10.   

    写成static方法,服务器启动的时候加载一次不就好了。