Answer the question
In order to leave comments, you need to log in
Pulling data from a PostgreSQL table?
you need to display data from the database into an HTML table, options with servlets and jsp are suitable.
Here is the db connection class
import java.sql.*;
public class JDBCExample {
public static void main(String args[]) {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "borntorun");
stmt = c.createStatement();
String sql = "CREATE TABLE PHONEBOOK "
+ "(ID INT PRIMARY KEY NOT NULL,"
+ " first_name VARCHAR(20) NOT NULL, "
+ " last_name VARCHAR(20) NOT NULL, "
+ " PHONE VARCHAR(20), "
+ " EMAIL VARCHAR(50))";
stmt.executeUpdate(sql);
sql = "INSERT INTO PHONEBOOK "
+ "VALUES (1, 'asf', 'asf', '123', '[email protected]')";
stmt.executeUpdate(sql);
sql = "INSERT INTO PHONEBOOK "
+ "VALUES (1, 'asf', 'asf', '123', '[email protected]')";
stmt.executeUpdate(sql);
sql = "INSERT INTO PHONEBOOK "
+ "VALUES (1, 'asf', 'asf', '123', '[email protected]')";
stmt.executeUpdate(sql);
sql = "INSERT INTO PHONEBOOK "
+ "VALUES (1, 'asf', 'asf', '123', '[email protected]')";;
stmt.executeUpdate(sql);
sql = "SELECT * FROM PHONEBOOK";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
//Получаем значения
int id = rs.getInt("id");
String email = rs.getString("EMAIL");
String phone = rs.getString("PHONE");
String first = rs.getString("first_name");
String last = rs.getString("last_name");
//Вывод данных
System.out.print("ID: " + id);
System.out.print(", First name: " + first);
System.out.print(", Last name: " + last);
System.out.print(", Phone: " + phone);
System.out.println(", Email: " + email);
}
rs.close();
stmt.close();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
}
Answer the question
In order to leave comments, you need to log in
You can do this with postgresql:
psql -d database_name -H -c "SELECT field1, field2, field3 FROM mytable WHERE field4=true;" -o test.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question