Forum Stats

  • 3,824,761 Users
  • 2,260,415 Discussions
  • 7,896,308 Comments

Discussions

java rookie needs help compiling source code

3674320
3674320 Member Posts: 3
edited May 4, 2019 7:31PM in New To Java

I am relatively new to Java programming and I have hit a snag I can't seem to fix.

my system is 64 bit windows 10.

I have this rather large bit of java code running under Tomcat 9 which does a lot of mysql database work and now I would like to get a very small part of that code running from the windows 10 command line.

I have a number of small java test programs that compile and run from the command line with no issues.

I first created a small test program which wou7ld just load the database driver and that was successfull.

I used the following command to compile my java code

java -cp C:\Tomcat_9_0_14\lib\mysql-connector-java-5.1.47-bin.jar;. sql2.java

However when I added code to read a database table I now can't even compile the4 java source code. I receive the following error message

Error: Could not find or load main class sql2.java

Here is the sql2.java file

import java.util.*;

import java.io.*;

import java.sql.*;

public class sql2 {

    private static String mysql_username = "my_user";

    private static String mysql_user_password = "my_pass";

    private static String url = "jdbc:mysql://localhost/my_database";

    private static String db_error_message = new String();

    private class division_class {

        public int        division_id;

        public String    division_name;

        public int        conference_id;

    } // end of division_class

    private static Vector division_entries = new Vector();

    private static division_class division_node;

    public static void main (String args[])  {

        Connection conn;

        Statement stmt;

        ResultSet results;

        int    num_div;

        String query = new String();

        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();

        } catch (Exception e) {

            db_error_message = "Can't get MySQL Instance : " + e;

            System.out.println(db_error_message + "\n");

            System.exit(1);

        } // CATCH

        System.out.println("Successfully loaded database driver\n");

        try {

            conn = DriverManager.getConnection(url,mysql_username,mysql_user_password);

            stmt = conn.createStatement();

            results = stmt.executeQuery("SELECT * FROM hockey_divisions");

            num_div = 0;

            while (results.next()) {

                num_div += 1;

                division_node = new division_class();

                division_node.division_id = results.getInt(1);

                division_node.division_name = results.getString(2);

                division_node.conference_id = results.getInt(3);

                division_entries.addElement(division_node);

            } // WHILE displaying SELECT results

        } catch (Exception e) {

            db_error_message = "Database error : " + e;

            System.out.println(db_error_message + "\n");

            System.exit(1);

        } // CATCH

    System.exit(0);

    } // end of main

} // end of sql2

As you can see, there is a "main" class. What have I done that is so horribly wrong ?

Answers

  • 3674320
    3674320 Member Posts: 3
    edited May 4, 2019 4:43PM

    Please forgive a horrible brain cramp. I finally noticed that I was running "java" not "javac" to compile the source code.due to a mixup in the batch files I am using to run and compile the java code.

    Now when I compile my java file sql2.java with the command

    javac sql2.java

    I see the following error

    sql2.java:41: error: non-static variable this cannot be referenced from a static context

                                    division_node = new division_class();

    I do not know how to fix this error. Any suggestions ?

  • 3674320
    3674320 Member Posts: 3
    edited May 4, 2019 7:31PM

    After much google searching and experiments, I finally managed to get the code to work.