我有DB.JAVA package db;import java.io.*;
import java.sql.*;
import java.util.*;
import javax.xml.parsers.*;
import java.net.URL;import org.w3c.dom.*;/**
* 鏡扴: 杅擂踱蟀諉濬 Copyright (c) 2005-2008 Wang Xining
*
* @author 卼浀譴
* @version 1.0
*/public class DB {
static String driver = null; static String server = null; static String dbuser = null; static String dbpassword = null; static int minconn = 0; static int maxconn = 0; static double maxconntime = 0.1D;
static String log_properties = null;
static ConnectionPool p = null;
private static String resourcePath;
private static Properties configProperties = System.getProperties();
static
{
int pos=0;
URL url = DB.class.getResource("DB.class");
String pkgName = DB.class.getPackage().getName();
do
{
pos = pkgName.indexOf(".");
if(pos>0)
{
pkgName = pkgName.substring(0,pos)+"/"+pkgName.substring(pos+1);
}
}
while(pos>0);
String filePath = url.getFile();
String appPath = filePath.substring(0,filePath.indexOf("/classes/"+pkgName));
resourcePath = appPath+"/resources";
try
{
configProperties.load(new FileInputStream(DB.getResourcePath()));
}
catch(FileNotFoundException e)
{
e.printStackTrace(); }
catch(IOException ex)
{
ex.printStackTrace();
}
}
public static void getAtrributes()
{
try
{
driver = getProperty("driver");
server = getProperty("server");
dbuser = getProperty("dbuser");
dbpassword = getProperty("dbpassword");
minconn = Integer.parseInt(getProperty("minconn"));
maxconn = Integer.parseInt(getProperty("maxconn"));
maxconntime = Double.parseDouble(getProperty("maxconntime"));
}
catch(Exception e)
{
System.out.println("Problem parsing the file"+e);
}
}
/**
* Create a connectionpool
*/
public static ConnectionPool getConnPool() {
try {
if (p == null) {
p = new ConnectionPool(driver, server, dbuser, dbpassword,
minconn, maxconn, maxconntime);
}
return p;
} catch (Exception e) {
e.printStackTrace();
} return null;
}
public static String getResourcePath()
{
return resourcePath;
}
public static Properties getConfigProperties()
{
return configProperties;
}
public static String getProperty(String name)
{
return getConfigProperties().getProperty(name);
}}
它讀取WEB-INF/resources/config.properties文件中的相關mysql連接變量,但是運行時報錯,講文件存取被拒!
WHY!
THINKS!
import java.sql.*;
import java.util.*;
import javax.xml.parsers.*;
import java.net.URL;import org.w3c.dom.*;/**
* 鏡扴: 杅擂踱蟀諉濬 Copyright (c) 2005-2008 Wang Xining
*
* @author 卼浀譴
* @version 1.0
*/public class DB {
static String driver = null; static String server = null; static String dbuser = null; static String dbpassword = null; static int minconn = 0; static int maxconn = 0; static double maxconntime = 0.1D;
static String log_properties = null;
static ConnectionPool p = null;
private static String resourcePath;
private static Properties configProperties = System.getProperties();
static
{
int pos=0;
URL url = DB.class.getResource("DB.class");
String pkgName = DB.class.getPackage().getName();
do
{
pos = pkgName.indexOf(".");
if(pos>0)
{
pkgName = pkgName.substring(0,pos)+"/"+pkgName.substring(pos+1);
}
}
while(pos>0);
String filePath = url.getFile();
String appPath = filePath.substring(0,filePath.indexOf("/classes/"+pkgName));
resourcePath = appPath+"/resources";
try
{
configProperties.load(new FileInputStream(DB.getResourcePath()));
}
catch(FileNotFoundException e)
{
e.printStackTrace(); }
catch(IOException ex)
{
ex.printStackTrace();
}
}
public static void getAtrributes()
{
try
{
driver = getProperty("driver");
server = getProperty("server");
dbuser = getProperty("dbuser");
dbpassword = getProperty("dbpassword");
minconn = Integer.parseInt(getProperty("minconn"));
maxconn = Integer.parseInt(getProperty("maxconn"));
maxconntime = Double.parseDouble(getProperty("maxconntime"));
}
catch(Exception e)
{
System.out.println("Problem parsing the file"+e);
}
}
/**
* Create a connectionpool
*/
public static ConnectionPool getConnPool() {
try {
if (p == null) {
p = new ConnectionPool(driver, server, dbuser, dbpassword,
minconn, maxconn, maxconntime);
}
return p;
} catch (Exception e) {
e.printStackTrace();
} return null;
}
public static String getResourcePath()
{
return resourcePath;
}
public static Properties getConfigProperties()
{
return configProperties;
}
public static String getProperty(String name)
{
return getConfigProperties().getProperty(name);
}}
它讀取WEB-INF/resources/config.properties文件中的相關mysql連接變量,但是運行時報錯,講文件存取被拒!
WHY!
THINKS!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货