Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
trying to get java working with postgres
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
XenoTerraCide
Veteran
Veteran


Joined: 18 Jan 2004
Posts: 1418
Location: MI, USA

PostPosted: Wed Dec 03, 2008 5:12 pm    Post subject: trying to get java working with postgres Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum