Hi, I have worked on a java code for a class and have no idea how to make it work. It basically needs to grab info from a database and spit out a table. I have Netbeans, Postgresql, and this java file. What next to work online?
.java file:
// // File: client3.java // import java.sql.*; public class client3 { public static void main( String args[] ) { Class driverClass = loadDriver( "org.postgresql.Driver" ); // String driver = "org.postgresql.Driver"; String url = "jdbc:postgresql://site.com/?user=USERNAME&password=PASSWORD"; if( driverClass == null ) return; // if( args.length != 1 ) // { // System.err.println( "usage: java client3 <url>" ); // return; // } Connection con = connectURL( url ); if( con != null ) { ResultSet result = execQuery( con, "SELECT * FROM kronke.inventory;" ); if( result != null ) printResults( result ); } } static Class loadDriver( String driverName ) { try { return( Class.forName( driverName )); } catch( ClassNotFoundException e ) { System.err.println( "Can't load driver - " + e.getMessage()); return( null ); } } static Connection connectURL( String URL ) { try { return( DriverManager.getConnection( URL )); } catch( SQLException e ) { System.err.println( "Can't connect - " + e.getMessage()); return( null ); } } static ResultSet execQuery( Connection con, String query ) { try { Statement stmt = con.createStatement(); System.out.println( query ); return( stmt.executeQuery( query )); } catch( SQLException e ) { System.err.println( "Query failed - " + e.getMessage()); return( null ); } } static void printResults( ResultSet res ) { System.out.println( " sku | department" ); System.out.println( "---------+------------------------------" ); try { while( res.next()) { System.out.print( res.getString( 1 )); System.out.print( " | "); System.out.print( res.getString( 2 )); System.out.println( "" ); } } catch( SQLException e ) { System.err.println( "Fetch failed: " + e.getMessage()); } } }