XenoTerraCide Veteran
Joined: 18 Jan 2004 Posts: 1418 Location: MI, USA
|
Posted: Wed Dec 03, 2008 5:12 pm Post subject: trying to get java working with postgres |
|
|
I was looking at this...
http://jdbc.postgresql.org/documentation/83/tomcat.html
I modified the code to match my settings and addes a main method, I'm trying to see if I can just get a simple query to pull data to the command line. once I have that working I'll turn it into a horribly designed servlet. this code is not production, just for learning.
Code: |
package pgsql;
import java.io.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class DBTest
{
String foo = "Not Connected";
int bar = -1;
public void init()
{
try
{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
// /jdbc/postgres is the name of the resource above
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/postgres");
if (ds != null)
{
Connection conn = ds.getConnection();
if(conn != null)
{
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("select * from etc.passwd");
if(rst.next()) {
foo=rst.getString(2);
bar=rst.getInt(3);
}
conn.close();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String getFoo() { return foo; }
public int getBar() { return bar;}
public static void main(String[] args)
{
System.out.println("test");
// new init();
}
}
|
I've been trying to figure out how to get the query to work but no luck so far. any help is appreciated. |
|