Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
java rookie needs help compiling source code

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
-
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 ?
-
After much google searching and experiments, I finally managed to get the code to work.