Results 1 to 5 of 5

Thread: Connection with PostgreSQL using JSP

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Connection with PostgreSQL using JSP

    Hello,

    I wish to use PostgreSQL as my backend from the .jsp file. Also let me know where do I put the postgresql-8.4-701.jdbc3.jar file to run. I am using NetBeans 6.7.1. Please help to cope up the situation.

    Thanks a lot in advance.

    Thanks & Regards.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Connection with PostgreSQL using JSP

    Add the jar file anywhere in your classpath (I suggest using a lib folder for 3rd party libraries).
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question Re: Connection with PostgreSQL using JSP

    I am using the database build inside the NetBeans. But I cannot retrieve the data. What is the code to retrieve the data from the table ? Please help.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Connection with PostgreSQL using JSP

    Code:
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Properties;
    import org.postgresql.Driver;
    
    public class Test
    {
    	public static void main(String[] args) throws SQLException
    	{
    		final Driver driver = new Driver();
    		final Properties properties = new Properties();
    		properties.setProperty("schema", "public");
    		properties.setProperty("user", "myuser");
    		properties.setProperty("password", "password");
    		final Connection connection = driver.connect("jdbc:postgresql://db-host:5432/mydb", properties);
    		final PreparedStatement statement = connection.prepareStatement("select * from pg_catalog.pg_amop");
    		final ResultSet results = statement.executeQuery();
    		while (results.next())
    			System.out.println(results.getInt(1));
    		results.close();
    		connection.close();
    	}
    
    	private Test()
    	{
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Connection with PostgreSQL using JSP

    The public schema is installed by default and I have no idea what kind of data is in it.
    I used Netbeans 6.7.1 and the driver came from the PostgreSQL library.

    Of course you know.. user name and password need chaning, so does the hostname, port number and DB name
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width