Java 从context.xml文件获取数据库凭据

Java 从context.xml文件获取数据库凭据,java,mysql,eclipse,Java,Mysql,Eclipse,我有一个java项目,在Eclipse中实现了一些API 我有db.java文件,可以与MySQL数据库通信 我希望将MySQL凭据保存在/META-INF/context.xml文件中,而不是保存在java文件中 你知道怎么做吗 这是我当前的代码: public class db { private String userName = null; private String password = null; private String dbName = null;

我有一个java项目,在Eclipse中实现了一些API

我有
db.java
文件,可以与MySQL数据库通信

我希望将MySQL凭据保存在
/META-INF/context.xml
文件中,而不是保存在java文件中

你知道怎么做吗

这是我当前的代码:

public class db {

    private String userName = null;
    private String password = null;
    private String dbName = null;
    private String db_connect_string = null;

    public db() {
        this.db_connect_string = "jdbc:mysql://localhost/mydb";
        this.dbName = "name";
        this.userName = "uname";
        this.password = "pass";
    }

protected Connection getDBMySQLCon() {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        return DriverManager.getConnection(this.db_connect_string+"?useSSL=false",  this.userName, this.password);
    } catch (ClassNotFoundException | SQLException | InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

您可以使用包含所需信息的属性文件,而不是XML文件。XML文件的问题在于,您必须选择一个XML解析器并使用它

如果您想继续使用属性文件,可以考虑下面的代码段。

public void setProp() throws Exception{  
    FileReader reader=new FileReader("db.properties");  
    Properties p=new Properties();  
    p.load(reader);  
    // you can get values you want as properties using
    this.db_connect_string = p.getProperty("db_connect_string");  
    this.dbName = p.getProperty("dbName");  
}  
您的文件结构应该是

db_connect_string=connection.string  
dbName=name
userName=uname
password=pass

您可以使用包含所需信息的属性文件,而不是XML文件。XML文件的问题在于,您必须选择一个XML解析器并使用它

如果您想继续使用属性文件,可以考虑下面的代码段。

public void setProp() throws Exception{  
    FileReader reader=new FileReader("db.properties");  
    Properties p=new Properties();  
    p.load(reader);  
    // you can get values you want as properties using
    this.db_connect_string = p.getProperty("db_connect_string");  
    this.dbName = p.getProperty("dbName");  
}  
您的文件结构应该是

db_connect_string=connection.string  
dbName=name
userName=uname
password=pass

这是容器环境的一部分

/META-INF/context.xml

xml覆盖tomcate上下文条目

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Specify a JDBC datasource -->
    <Resource name="jdbc/mydatabase" 
              auth="Container"
              type="javax.sql.DataSource" 
              username="YOUR_USERNAME" 
              password="YOUR_PASSWORD"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://mysql.metawerx.net:3306/YOUR_DATABASE_NAME?
              autoReconnect=true"
              validationQuery="select 1"
              maxActive="10" 
              maxIdle="4"/>

</Context>

// Get DataSource
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydatabase");
// Get Connection and Statement
Connection c = ds.getConnection();
Statement s = c.createStatement();

//获取数据源
Context ctx=新的InitialContext();
DataSource ds=(DataSource)ctx.lookup(“java:comp/env/jdbc/mydatabase”);
//获取连接和语句
连接c=ds.getConnection();
语句s=c.createStatement();

这是容器环境的一部分

/META-INF/context.xml

xml覆盖tomcate上下文条目

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Specify a JDBC datasource -->
    <Resource name="jdbc/mydatabase" 
              auth="Container"
              type="javax.sql.DataSource" 
              username="YOUR_USERNAME" 
              password="YOUR_PASSWORD"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://mysql.metawerx.net:3306/YOUR_DATABASE_NAME?
              autoReconnect=true"
              validationQuery="select 1"
              maxActive="10" 
              maxIdle="4"/>

</Context>

// Get DataSource
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydatabase");
// Get Connection and Statement
Connection c = ds.getConnection();
Statement s = c.createStatement();

//获取数据源
Context ctx=新的InitialContext();
DataSource ds=(DataSource)ctx.lookup(“java:comp/env/jdbc/mydatabase”);
//获取连接和语句
连接c=ds.getConnection();
语句s=c.createStatement();

web.xml
@Satya你能给我一个例子吗?你在使用哪些技术。@Satya我在使用
java
apache tomcat 7.0
你在使用哪些服务器端技术。在
web.xml
@Satya你能给我一个例子吗您正在使用的技术。@Satya我正在使用
java
apachetomcat7.0
您正在使用哪些服务器端技术。