A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 柳雷 中级黑马   /  2012-7-26 14:12  /  2270 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 柳雷 于 2012-7-26 16:24 编辑

public class JDBCUtil {
  
  private static String driver = "";
  private static String url = "";
  private static String dbUser = "";
  private static String dbPassword = "";
  public static void getParam(){
            String filename = "src/database.properties";
            File file = new File(filename);
            try {
              FileInputStream fis = new FileInputStream(file);
              Properties props = new Properties();
              props.load(fis);
              driver = props.getProperty("driver");
              url = props.getProperty("url");
              dbUser = props.getProperty("dbUser");
              dbPassword = props.getProperty("dbPassword");
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
  public static Connection getConnection(){
        getParam();
    Connection conn = null;
    try{
      Class.forName(driver);
      conn = DriverManager.getConnection(
          url,dbUser,dbPassword);
    }catch(Exception e){
      e.printStackTrace();
    }
   
    return conn;
  }
public static void main(String[] args) {
         Connection conn = JDBCUtils.getConnection();
         System.out.println("连接:"+conn);
}
}
模仿别人写了个JDBC的工具类,获取连接。本身这个工具类没有问题,执行main方法结果如下:
连接:com.mysql.jdbc.Connection@1808199
但是我发布服务到web项目上,却出错了
servlet关键代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                UserDao userDao = new UserDao();
                String errorInfo = "";
                String loginName = request.getParameter("loginName");
                String password = request.getParameter("password");
                User user = userDao.selectUserByName(loginName);
dao代码如下:
public User selectUserByName(String name){
                User user = null;
                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                String sql = "select * from user where name = ?";
                try {
                        conn = JDBCUtil.getConnection();
                        System.out.println("#########获取连接:"+conn);
                        ps = conn.prepareStatement(sql);
报错如下:
java.io.FileNotFoundException: src\database.properties (系统找不到指定的路径。)
#########获取连接:null
意思是取不到路径,是不是因为发布到tomcat服务器,所以src/database.properties就取不到了?我应该怎么写呢?怎么才能取到这个配置文件中的内容呢?

评分

参与人数 1技术分 +1 收起 理由
田向向 + 1 赞一个!

查看全部评分

2 个回复

倒序浏览
发布成为WEB项目,一般来说路径有两种:页面上用来连接的basepath和文件的classpath。src是不用写的或者试试../这个前缀
回复 使用道具 举报
本帖最后由 黑马高明辉 于 2012-7-26 16:44 编辑

配置文件一般都放在src目录下,在bin下也有一份,所以通过classloader来读取呗。
做了个实验,效果出来了。先看看代码:
import java.io.InputStream;
import java.util.Properties;
public class LoadSRCFile {
/**
  * @param args
  */
public static void main(String[] args) {
  try {
   System.out.println( getSrcFile() );
  } catch (Exception e) {
   e.printStackTrace();
  }
  
}

public static String getSrcFile() throws Exception{
  InputStream ips = LoadSRCFile.class.getResourceAsStream("/config.properties");
  Properties props = new Properties();
        props.load(ips);
  return props.getProperty("gmh");
}
}
------------------------config.properties文件内容。
gmh=from config
---------------------------------运行结果:
from config




回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马