uro*_*obo 18 java postgresql jdbc
我正在开发一个Web项目,最近我安装了postgres 9.1.1
postgresql服务器已启动并正在运行.我可以像往常一样通过psql连接,一切都被加载并从我从8.5制作的db的转储中正确保存.
所以我也在这里下载了9.1 postgres版本的JDBC4驱动程序:http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz
我通过eclipse使用项目属性将它添加到java构建路径中.
这是我用来提供与其他类的数据库连接的代码(即它是一个单例,只有当现有关闭或空时,我才能获得一个新连接,一次只能从一个对象获得)
public abstract class DBConnection {
private static Connection connection = null;
public static void connect() {
try {
if (connection == null) {
String host = "127.0.0.1";
String database = "xxxxx";
String username = "xxxxx";
String password = "xxxxx";
String url = "jdbc:postgresql://" + host + "/" + database;
String driverJDBC = "org.postgresql.Driver";
Class.forName(driverJDBC);
connection = DriverManager.getConnection(url, username,
password); //line firing the class not found exception
} else if (connection.isClosed()) {
connection = null;
connect();
}
} catch (SQLException e) {
e.printStackTrace(System.err);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
public static void disconnect() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
Logger.getLogger(DBConnection.class.getName()).log(
Level.SEVERE, null, e);
}
}
}
public static Connection getConnection() {
try {
if (connection != null && !connection.isClosed()) {
return connection;
} else {
connect();
return connection;
}
} catch (SQLException e) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE,
null, e);
return null;
}
}
@Override
public void finalize() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
Logger.getLogger(DBConnection.class.getName()).log(
Level.SEVERE, null, e);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如我在标题中写的那样,当我运行项目并且一个类要求连接到这个类时,我总是得到一个Class Not Found Exception,因为它显然无法加载org.postgresql.Driver.class驱动程序位于项目的子文件夹〜/ lib/org.postgresql-9.1-901.jdbc4.jar,正如我所说,通过eclipse项目属性添加到构建路径.
我还提供了一个示例查询,以便查看我的类的通常行为来访问DBConnection:
public static final User validateUserCredentials(String id, String pswd) {
Connection connection = DBConnection.getConnection();
Logger.getLogger(Credentials.class.getName()).log(Level.SEVERE, (connection!=null)?"connection not null":"connection null");
Statement stmt = null;
Logger.getLogger(Home.class.getName()).log(Level.SEVERE, "validating credentials for user: username : " + id + " password : " + pswd);
String sql = "Select * from fuser where id = '" + id + "'";
ResultSet resultset = null;
try {
stmt = connection.createStatement();
resultset = stmt.executeQuery(sql);
Logger.getLogger(Credentials.class.getName())
.log(Level.SEVERE, sql);
resultset.next();
String password = resultset.getString("pswd");
if (pswd.equals(password))
return new User(id, pswd);
} catch (SQLException ex) {
Logger.getLogger(Credentials.class.getName()).log(Level.SEVERE,
null, ex);
} finally {
if (stmt != null)
stmt = null;
if (resultset != null)
resultset = null;
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
connection = null;
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 26
我正在开发一个Web项目,最近我安装了postgres 9.1.1
...
我通过eclipse 使用项目属性将它添加到java构建路径中.
这是错误的方式.必须直接将JAR放在/WEB-INF/libWeb项目的文件夹中,而不必在项目属性中摆弄Build Path.该文件夹是webapp的运行时类路径的标准部分.
与具体问题无关:你的DBConnection课堂上存在一个重大的设计缺陷.您已经声明Connection,static这实质上使您的连接不是线程安全的.使用连接池,永远不要将Connection(也不Statement是ResultSet)分配为类/实例变量.应该在与try-finally执行查询的块相同的块中创建和关闭它们.此外,你还有一个SQL注入漏洞.使用PreparedStatement而不是在SQL字符串中连接用户控制的变量.
小智 5
在您的 pom 中添加此依赖项:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1203-jdbc4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)