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.
Re: Connection with PostgreSQL using JSP
Add the jar file anywhere in your classpath (I suggest using a lib folder for 3rd party libraries).
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.
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()
{
}
}
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