Recently the company with PostgreSQL this database, see online said this database is still good, I used a bit, I was using Java to connect a database.
In fact, each database is connected in roughly the same way, only with different drivers, with different databases only need to change the database driver package.
Project Structure
Code
DBHelper
PackageCom.xxx.postgrepsql;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;/*** Database Helper class*/ Public classDBHelper {Private StaticString url = "Jdbc:postgresql://localhost:5432/solution"; Private StaticString Driver = "Org.postgresql.Driver"; Private StaticString user = "Postgres"; Private StaticString password = "18731362155"; PrivateConnection Connection =NULL; PublicConnection getconnection () {returnconnection; } PublicPreparedStatement getstatement () {returnstatement; } PrivatePreparedStatement statement =NULL; Publicdbhelper (String sql) {Try{class.forname (driver); Connection=drivermanager.getconnection (URL, user, password); Statement=connection.preparestatement (SQL); } Catch(Exception e) {e.printstacktrace (); } } Public voidClose () {Try { This. Connection.close (); This. Statement.close (); } Catch(Exception e) {e.printstacktrace (); } }}
Jdbcdemo
PackageCom.xxx.postgrepsql;ImportJava.sql.*;/*** Connect Postgres database*/ Public classJdbcdemo { Public Static voidMain (string[] args) {String SQL= "SELECT * FROM T_user"; DBHelper DBHelper=Newdbhelper (SQL); Try{ResultSet ResultSet=dbhelper.getstatement (). ExecuteQuery (); System.out.println ("Id username Password"); while(Resultset.next ()) {System.out.println (resultset.getstring (1) + "" "+resultset.getstring (2) +" "+resultset.getstring (3)); } dbhelper.close (); } Catch(SQLException e) {dbhelper.close (); E.printstacktrace (); } }}
If you have any questions, please contact qq1058179540.
Java Connection PostgreSQL