2

i guess this is a newbie question...Don't seem to find it anywhere, so i thought i asked, hope you don't get mad :) I am really new to all this jsp and java thing and try to figure out what's going on.

So, I have created 2 classes that create the connection to my postgres database. The thought was to have a class that creates the connection and use it anytime i need it, cause i will need to retrieve data many times in the future.

The first one: DbContract.java

package Database;


public class DbContract 
{
    public static final String HOST = "jdbc:postgresql://localhost:5432/";
    public static final String DB_NAME = "DB_1";
    public static final String USERNAME = "postgres";
    public static final String PASSWORD = "12345";   
}

and the second one: TestConnection.java

package Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class TestConnection 
{
    public static void main(String[] args) 
    {
        try
        {
            Class.forName("org.postgresql.Driver");
            Connection c;

            c = DriverManager.getConnection(
                    DbContract.HOST+DbContract.DB_NAME,
                    DbContract.USERNAME,
                    DbContract.PASSWORD);

            System.out.println("DB connected");
        }
        catch (ClassNotFoundException | SQLException e) 
        {
            e.printStackTrace();
        }
    }    
}

Now, i want to use these, in order to retrieve data from a existing database i have (postgres and it is filled with records.), BUT from a jsp page. Is it possible?

Can anyone, help me step by step to do it, or is it too easy?

thanks in advance

  • 1
    To retrieve data from your tables, I advice you to use the [DAO Pattern](http://stackoverflow.com/questions/19154202/data-access-object-dao-in-java) and make objects so that you will be able to use them with the [Expression Language](http://stackoverflow.com/tags/el/info) within your JSP – Yassin Hajaj Nov 22 '15 at 14:00
  • thanks @YassinHajaj i tried to combine those 2 classes in order to create a simple class, that connects. i also added getters and setters if i need to create an object of that class. But how do i call it to use it in a jsp page? – Alexander F Nov 22 '15 at 14:47

0 Answers0