servlet提供初化化数据库连接参数与javabean和JSP困惑,请指教。 你直接在其他的 servlet 或者 jsp 中调用 conn 这个对象吧,有必要放到 web.xml 中吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 数据库的连接参数也可以配置在.properties资源文件中 一个javabean封装好,用的时候调用就好了嘛 你上面说的把数据库操作封装在一个javaBean中,用于被其他的jsp,servlet都是可以的你直接把你的javaBean所在的路径导入就可以了 放在一个.properties文件中,然后直接用javaBean来读取,会更简单使用!无需都往web.xml中挤web.xml中放入这些东西,只能用servlet通过getInitParameter来获取,去到之后还得赋值给javaBean可以通过继承策略,把你的servlet在继承一个基类,然后在基类中写这部分代码 比如先建立一个db.properties文件,放在com.yourcompany.common包下,文件中内容如下:DRIVER=com.mysql.jdbc.DriverURL=jdbc:mysql://localhost:3306/chinatravellinksUSER=rootPASSWORD=你再建立一个类读取改资源文件,读取代码如下: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"); 上面的有点小错误!比如先建立一个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"); 1.我如何在封装好的数据库连接javabean(conn.java)里获得servle里得到的参数呢? 设计上有些问题 为什么我读不到properties里的内容呢?如果这样,是不是用户访问一次都读它,那性能怎么比得上WEB.XML里呢?com.conn.preConn.propertiesDRIVER=com.mysql.jdbc.DriverURL=jdbc\:mysql\://localhost\:3306/chinatravellinksUSER=rootPASSWORD=com.conn.Conn.javapublic 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; }} 通过通过.properties来存放数据库属性可以啦..可是:请问我通过.properties来存放数据库连接属性是不是用户访问数据信息时都会读取里面的连接属性而对性能影响很不好? 就是说这种存放数据库连接属性的方法是不可取的?用监听器是否可以实现通过web.xml存放连接数据库属性? 写成static方法,服务器启动的时候加载一次不就好了。 大家对我会SSH发表下意见 java Junit测试的问题,请高手来帮忙! jsp 缺少 } ,明明没有缺少 hibernate QBC 子查询 急! spring +hibernate +webwork2的问题 求个js 一个java技术网站[http://javalife.cn],欢迎大家来灌 我这有JBuilder7&Weblogic7整和开发培训手册(中文,37页),适合初学者,需要的请留mail help :关于Jbuilder开发ejb的一个问题 浏览器停留在当前页面,再做JSP页面编辑时,感觉反应很慢,这是怎么回事? 多线程的问题 struts2.0查询中文问题
你直接把你的javaBean所在的路径导入就可以了
无需都往web.xml中挤web.xml中放入这些东西,只能用servlet通过getInitParameter来获取,去到之后还得赋值给javaBean
可以通过继承策略,把你的servlet在继承一个基类,然后在基类中写这部分代码
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");
比如先建立一个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");
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;
}
}
可是:
请问我通过.properties来存放数据库连接属性是不是用户访问数据信息时都会读取里面的连接属性而对性能影响很不好?
就是说这种存放数据库连接属性的方法是不可取的?
用监听器是否可以实现通过web.xml存放连接数据库属性?