从配置文件读取连接数据库的配置信息

在webapps下新建Web应用程序的目录为conntest:
其目录结构如下:
webapps:
|-conntext
| |-src
| | |-conntest
| | |-DBConfig.java
| | |-ConnDBServlet.java
| |
| |-WEB-INF
| |-classes
| | |-conntest
| | |-database.properties
| | |-DBConfig.class
| | |-ConnDBServlet.class
| |
| |-lib
| | |-mysql-connector-java-5.1.26-bin.jar
| |
| |-web.xml

1.database.properties

#driverClass(数据库驱动类的类名)
#driverClass=oracle.jdbc.OracleDriver
driverClass=com.mysql.jdbc.Driver

#url(要连接数据库的地址)
#url=jdbc:oracle:thin:@192.168.1.180:1521:orcl
#url=jdbc:mysql://127.0.0.1:3306/test
url=jdbc:mysql://127.0.0.1:3306

#user(数据库用户)
#user=scott
user=root

#password(密码)
#password=tiger
password=root


2.web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<context-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>

<servlet>
<servlet-name>ConnDBServlet</servlet-name>
<servlet-class>conntest.ConnDBServlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>root</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ConnDBServlet</servlet-name>
<url-pattern>/conndb</url-pattern>
</servlet-mapping>

</web-app>


3.DBConfig.java

package conntest;

public class DBConfig{
private String driverClass;
private String url;
private String user;
private String password;
public DBConfig(String driverClass, String url, String user, String password){
this.driverClass = driverClass;
this.url = url;
this.user = user;
this.password = password;
}

public String getDriverClass(){
return this.driverClass;
}

public String getUrl(){
return this.url;
}

public String getUser(){
return this.user;
}

public String getPassWord(){
return this.password;
}
}

4.ConnDBServlet.java

package conntest;

import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class ConnDBServlet extends HttpServlet{
private DBConfig dbconfig;

//读取database.properties配置文件的配置信息
public DBConfig readDbConfig(String propFileName) throws IOException{
try{
//主要就是获取输入流
//方法1:通过FileInputStream构造函数得到
//String propFileName = "F:\\webapps\\conntest\\WEB-INF\\classes\\conntest\\database.properties";
//FileInputStream in=new FileInputStream(propFileName);//FileNotFoundException

//方法2:通过Class.getResourceAsStream()
//String propFileName = "/conntest/database.properties";
//InputStream in = this.getClass().getResourceAsStream(propFileName);

//方法3:通过ServletContext.getResourceAsStream()
//String propFileName = "/WEB-INF/classes/conntest/database.properties";
InputStream in = this.getServletContext().getResourceAsStream(propFileName);

Properties props = new Properties();
props.load(in);//IOException

String driverClass = props.getProperty("driverClass");
String url = props.getProperty("url");
String user = props.getProperty("user");
String password = props.getProperty("password");

return new DBConfig(driverClass, url, user, password);
}
catch(FileNotFoundException e) {
new RuntimeException(e);
}
catch(IOException e) {
new RuntimeException(e);
}
return null;
}

//读取web.xml中的配置信息
public DBConfig readDbConfig(){
//<servlet>元素中的<init-param>
// String driverClass = getInitParameter("driverClass");
// String url = getInitParameter("url");
// String user = getInitParameter("user");
// String password = getInitParameter("password");

//<context-param>
String driverClass = getServletContext().getInitParameter("driverClass");
String url = getServletContext().getInitParameter("url");
String user = getServletContext().getInitParameter("user");
String password = getServletContext().getInitParameter("password");
return new DBConfig(driverClass, url, user, password);
}

public void init() throws ServletException{
//String propFileName = "F:\\webapps\\conntest\\WEB-INF\\classes\\conntest\\database.properties";
//String propFileName = "/conntest/database.properties";
String propFileName = "/WEB-INF/classes/conntest/database.properties";
//String propFileName = null;
try{
if(propFileName == null){
dbconfig = readDbConfig();
}
else{
dbconfig = readDbConfig(propFileName);
}
Class.forName(dbconfig.getDriverClass());//ClassNotFoundException
}
catch(ClassNotFoundException e){
throw new ServletException("加载数据库驱动失败!");
}
catch(FileNotFoundException e) {
throw new ServletException("没有找到属性文件!");
}
catch(IOException e) {
throw new ServletException("加载属性文件失败!");
}
}

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
Connection conn = null;
try{
conn = DriverManager.getConnection(dbconfig.getUrl(), dbconfig.getUser(), dbconfig.getPassWord());
res.setContentType("text/html; charset=UTF-8");
PrintWriter out = res.getWriter();
out.println("数据库连接成功!");
out.close();
}
catch(SQLException e){
throw new ServletException(e);
}
finally{
if(conn != null){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
conn = null;
}
}
}
}


5.编译

F:\webapps\conntest>javac -d ./WEB-INF/classes ./src/conntest/DBConfig.java

F:\webapps\conntest>javac -classpath D:\web\tomcat\lib\servlet-api.jar;./WEB-INF/classes -d ./WEB-INF/classes ./src/conntest/ConnDBServlet.java


6.访问:
[url]http:localhost:8080/conntest/conndb[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值