java servlet connect with postgresql -
i have problems connect java servlet postgresql . me if can ,please
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 registration"; resultset rs = statement.executequery(sql); while (rs.next()) { system.out.println(rs.getint("id")); }
try code , remember 1 thing before running code have added postgresql-9.1-901.jdbc4.jar
in lib folder @ following location: tomcat_home/webapps/<project_name>/web-inf/lib
without jar file, got 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 * login"; resultset rs = statement.executequery(sql); while (rs.next()) { system.out.println(rs.getstring("uname")); } }catch(sqlexception e){ e.printstacktrace(); }
hope code helps you.
Comments
Post a Comment