Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Beginner - how to make .java file run!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    //
    //  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());
        }
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Beginner - how to make .java file run!

    Are you just asking how to compile and run a class?

    Recommended reading: "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Beginner Question Here (Trying to make things easier)
    By beer-in-box in forum AWT / Java Swing
    Replies: 2
    Last Post: June 1st, 2011, 10:48 AM
  2. How to make Netbeans know about java.nio.file copyTo?
    By jedast in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2011, 08:38 PM
  3. How to make java search for a file?
    By StarKannon in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2011, 01:36 PM
  4. [SOLVED] i cant make a java jar file which can search data from mysql
    By talha07 in forum JDBC & Databases
    Replies: 6
    Last Post: January 20th, 2011, 06:09 AM
  5. Best beginners project in Java
    By Fendaril in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2009, 08:52 AM