Im在网络项目上工作,我最近安装了9.1.1号邮局。
邮政服务器正在运行。 我可以像往常一样通过纸浆连接,从8.5台的垃圾堆放场上装满和适当节省所有物品。
So I also downloaded the JDBC4 driver for 9.1 postgres version here: http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz
我在“java”公路上通过ec片使用项目特性。
这是我用来提供其他班次的干 connection联系的法典(即,它是一个单一州,只有在现有物品已经关闭或无效的情况下,我才获得新的联系,而现在只有某个物体在某个时间关闭。)
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);
}
}
}
}
正如我在管理该项目时在标题中写的那样,一个班子要求与这一类别挂钩,我总是获得一个非专业创始例外,因为它显然能够装上org.postgresql.司机。 驾驶员位于项目~/lib/org的分数位数。postgresql-9.1-901.jdbc4.jar,而且正如我所说的那样,在建筑道路上,通过粗略项目财产。
I m还提供样本查询,以了解我的班子通常接触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;
}