Kar*_*yan 0 java postgresql servlets
我在将 java servlet 与 postgresql 连接时遇到了一些问题。如果可以,请帮助我
String dbName = "jdbc:postgresql://localhost/schedule_of_holidays";
String dbDriver = "org.postgresql.Driver";
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbName, userName,
password);
System.out.println("Got Connection");
Statement statement = con.createStatement();
String sql = "select id from registration";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getInt("id"));
}
Run Code Online (Sandbox Code Playgroud)
尝试此代码并在运行已添加到postgresql-9.1-901.jdbc4.jarlib 文件夹中以下位置的代码之前记住一件事:tomcat_home/webapps/<project_name>/WEB-INF/lib
如果没有这个 jar 文件,你会得到 ClassNotFoundException
String dbName = "jdbc:postgresql://localhost/struts_new";
String dbDriver = "org.postgresql.Driver";
String userName = "postgres";
String password = "postgres";
try{
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbName, userName, password);
System.out.println("Got Connection");
Statement statement = con.createStatement();
String sql = "select * from login";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("uname"));
}
}catch(SQLException e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
希望此代码对您有所帮助。