PackageTest.postgre;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classSIMPLEJDBC { Public Static voidMain (string[] args) {Connection conn=Getconn (); String SQL= "SELECT * from Calendar"; Statement stmt=NULL; ResultSet RS=NULL; Try{stmt=conn.createstatement (); RS=stmt.executequery (SQL); while(Rs.next ()) {System.out.println (Rs.getint (1)); } } Catch(SQLException e) {e.printstacktrace (); } } /*** Database Line *@return */ Public StaticConnection Getconn () {Connection conn=NULL; Try{class.forname ("Org.postgresql.Driver"); String URL= "Jdbc:postgresql://***.**.**.**/test"; Try{conn= Drivermanager.getconnection (URL, "Postgres", "Postgres"); } Catch(SQLException e) {e.printstacktrace (); } } Catch(ClassNotFoundException e) {e.printstacktrace (); } returnConn; }}
Java Simple database connection