-1
import java.sql.*;

class TestingDatabase
{
        public static void main(String args[])
        {
            try
            {
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","metalgear");
                java.util.Scanner input = new java.util.Scanner(System.in);
                int i = input.nextInt();
                PreparedStatement ps = conn.prepareStatement("select * from students where sid=?");
                ps.setInt(1,i);
                ResultSet rs = ps.executeQuery();
                rs.next();
                System.out.println(rs.getInt(1));
                System.out.println(rs.getInt(2));

            }
           catch(ClassNotFoundException e){}
           catch(SQLException e){}
       }
}

I saved this code on desktop and compiled it using javac TestingDatabase.java command. It compiled successfully but when I entered the command java TestingDatabase it said Error: could not find or load main class TestingDatabase

I double checked for any spelling mistake but couldn't find one. can anyone suggest any solution.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • 3
    Possible duplicate of [What does "Could not find or load main class" mean?](http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – azurefrog Oct 17 '16 at 19:39
  • Terrible code in every way. Your immediate problem is likely to be an issue with paths. Empty catch blocks? You'll regret those when the code does nothing and you can't figure out why. – duffymo Oct 17 '16 at 19:39
  • @azurefrog i know what it this statement means but i dont understand why i am getting this error. – WePull Washney Oct 17 '16 at 19:40
  • @Max I Tried it. But same results. – WePull Washney Oct 17 '16 at 19:42
  • @duffymo i didn't meant to use this code in large programs. i just wanted to test if my database connection is working properly. – WePull Washney Oct 17 '16 at 19:43
  • try to add `public` to the declaration of your class `public class TestingDatabase` – Nicolas Filotto Oct 17 '16 at 19:43
  • @NicolasFilotto didn't work. – WePull Washney Oct 17 '16 at 19:44
  • i mailed the same code to a friend and he didn't get this error. i dont understand why am i getting this? – WePull Washney Oct 17 '16 at 19:46
  • you need to add your JDBC driver in your classpath by launching it with `java -cp /path/to/my/driver.jar TestingDatabase` – Nicolas Filotto Oct 17 '16 at 19:47
  • @NicolasFilotto i added the driver.jar (ie ojdbc14.jar) file in both of these locations C:\Program Files\Java\jdk1.8.0_77\jre\lib\ext and C:\Program Files\Java\jre1.8.0_101\lib\ext – WePull Washney Oct 17 '16 at 19:53
  • @WePullWashney-bad habits are still bad. Those drivers should not be in those locations. It's not surprising that you are having issues. – duffymo Oct 17 '16 at 20:00
  • -@duffymo these drivers have always been there . And my previous codes have worked fine . this particular code is showing this error. i was just brushing up the syntax before using it in my actual program. – WePull Washney Oct 17 '16 at 20:02
  • 1
    You really need to a **CAREFULL** look at @azurefrog's duplicate page. There are a lot of solutions (**BUNCH SOLUTIONS WITH NOTES AND REASONS**) and reason there. So if you can't find yours..then we are sorry – Young Emil Oct 17 '16 at 20:06
  • @YoungMillie i read the soultion on that page several times before asking this question. – WePull Washney Oct 17 '16 at 20:09
  • I don't trust that...CAREFULLY...because that is absolutely almost all of the possible causes and their suggested corresponding solutions. I strongly recommend you to revisit that page and carefully go through all the solutions **Especially the first one** else I guess you must solve this your own way. – Young Emil Oct 17 '16 at 20:14
  • @YoungMillie i just tried to execute my previous programs and they all showing the same error. I think there are problems in my computer. – WePull Washney Oct 17 '16 at 20:18
  • @WePullWashney If you have already read the canonical answer, you should have mentioned that in your question. It would save us a great deal of time to not have to go over things things you've already tried. – azurefrog Oct 17 '16 at 20:22
  • @azurefrog sorry.......i didn't meant to waste anyone's time. i think the problem is in my environment variables. – WePull Washney Oct 17 '16 at 20:26
  • thnx guys ..the problem is solved. there was a mistake in my environment variable. i really appreciate your comments. – WePull Washney Oct 17 '16 at 20:29

1 Answers1

0

agree with Young Millie on reviewing the list of possible causes. when testing, i found no problem with either the code or executing it. i used command line:

14:31:28$ javac TestingDatabase.java 14:31:33$ java TestingDatabase 14:31:36$

my best guess is that your classpath which might be incorrect; it should include the current directory (.) at some point or not have a classpath defined at all

ryonts
  • 126
  • 6