Beginner - how to make .java file run!
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:
Code :
//
// 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());
}
}
}
Re: Beginner - how to make .java file run!
Re: Beginner - how to make .java file run!
Yeah, when I press run in Netbeans, nothing happens. I am not sure how to get the result and also how to run it online.
Re: Beginner - how to make .java file run!
Take smaller steps. Can you get hello world running? What happens when you try running from the command prompt? Add tiny pieces until it stops working, at which point you'll have an SSCCE and it will be easier to diagnose your problem.