【发布时间】:2014-03-19 10:05:18
【问题描述】:
我正在尝试编写一个简单的 JSF 应用程序,让用户在 Web 浏览器上登录,然后我的应用程序应该验证 MySQL 数据库上的用户凭据。
我无法让我的代码查看 database.properties 文件。我对所有独立的数据库程序都使用了以下代码,但我必须对我正在编写的这个 JSF 应用程序进行一些修改。
下一行大约 3/4 处抛出 ClassNotFoundException,因为我的 url 变量为空,我不知道为什么: FileInputStream in = new FileInputStream(url.getFile()); // 如果在服务器上,请执行此操作
谁能帮帮我?谢谢!有问题的代码在方法 init(String fileName) 的前半部分。
下面是获取数据源的整个类:
package com.gmail.gmjord.datasource;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* A simple data source for getting database connections.
*/
public class SimpleDataSource {
private static String urlString;
private static String username;
private static String password;
/**
* Initializes the data source.
*
* @param fileName
* the name of the property file that contains the database
* driver, URL, username, and password
*/
public static void init(String fileName) throws IOException,
ClassNotFoundException {
// ****************** Added code for getting resource on a server
// *********************
System.out.println("In SimpleDataSource.init()");
**Class cls = Class.forName("com.gmail.gmjord.datasource.SimpleDataSource");**
System.out.println("Made it here 1");
// returns the ClassLoader object associated with this Class
ClassLoader cLoader = cls.getClassLoader();
System.out.println("Made it here 2");
System.out.println(cLoader.getClass());
// finds resource with the given name
URL url = cLoader.getResource(fileName); // This is the original line I copied from the web page
//URL url = cls.getClass().getClassLoader().getResource(fileName); // This is TA's way
System.out.println("Made it here 3");
System.out.println("File: " + fileName);
System.out.println("url Value = " + url);
// ********* End of code for getting resource on a server*******************************************************
Properties props = new Properties();
//System.out.println("File: " + fileName);
//FileInputStream in = new FileInputStream(fileName); // Do this if not on server
FileInputStream in = new FileInputStream(url.getFile()); // Do this if on Server
props.load(in);
String driver = props.getProperty("jdbc.driver");
urlString = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
if (username == null)
username = "";
password = props.getProperty("jdbc.password");
if (password == null)
password = "";
if (driver != null)
Class.forName(driver);
}
/**
* Gets a connection to the database.
*
* @return the database connection
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(urlString, username, password);
}
}
【问题讨论】:
-
fileName的值是多少??请贴出字符串值
-
对不起,字符串名称是“database.properties”
-
文件database.properties在包里面,或者在web应用的文件夹里,你能把文件位置贴出来吗。
-
它不在一个包中。它在我的项目的顶层如下:PizzaProjectWeb
-
忽略前面的评论...它不在一个包中。它位于我项目的顶层。物理位置是 C:\Users\Mitch Jordan\workspace2\PizzaProjectWeb\database.properties,其中 PizzaProjectWeb 是我的项目。
标签: java mysql jsf url classloader