-1
import java.sql.Connection;

public static void main(String[] args) throws SQLException, RuntimeException {


    try{

           DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
                       
   String connectionURL="jdbc:mysql://localhost:3306/aaa?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  String login="root";
  String password="aaa";
        
        
        try (Connection conn = DriverManager.getConnection(connectionURL, login, password)) {
            Statement stmt = conn.createStatement();
            
            
            ResultSet rs = stmt.executeQuery("SELECT * FROM Lista1");
            
            
            while(rs.next()) {
                System.out.println(rs.getString("nazwa"));
            }
            rs.close();
            
            
            stmt.close();
        }
    }
    
    catch(SQLException e) {
         System.out.println("Blad polaczenia:");
          e.printStackTrace();
    }
    
    catch(RuntimeException e) {
         System.out.println("Blad polaczenia:");
          e.printStackTrace();
    }

The console log throws:

    Exception in thread "main" java.lang.RuntimeException: 
at cccc.web.Main.main(Main.java:1)

I tried to delete return, but it still threw bad issue. I tried many things but I don't know how to fix it. I don't know how to solve it. And I spent many many many times to fight with it!!!!

  • You can't. You need to find the more specific reason for the exception's being thrown. Where is the rest of the exception's stack trace information? – Hovercraft Full Of Eels Sep 22 '20 at 23:18
  • 1
    You cannot have methods outside of classes. `RuntimeException` in **line 1** likely means that the actual error is a compilation error, because the **source file did not compile**. – Andreas Sep 22 '20 at 23:54

2 Answers2

1

The exception object contains two things that tell you what happened: the specific type of the exception (RuntimeException is a base), and if you're lucky, a text message telling you what the problem is.

Print them in your exception handler.

    System.out.println("Exception: " + e)

And, of course, the stacktrace tells you the exact line of your source file that caused the problem.

This is not really a matter of 'fixing' an exception, it's a matter of understanding what is going on and writing code to handle that situation.

J.Backus
  • 1,441
  • 1
  • 6
  • 6
0

When an error tells you that the exception happened on the first line, that very much looks like a parsing issue. Take a look here:

    try (Connection conn = DriverManager.getConnection(connectionURL, login, password)) {
        Statement stmt = conn.createStatement();
        
        
        ResultSet rs = stmt.executeQuery("SELECT * FROM Lista1");
        
        
        while(rs.next()) {
            System.out.println(rs.getString("nazwa"));
        }
        rs.close();
        
        
        stmt.close();
    }

The code above is a try without a catch. It's inside an outer try. But once you are using a try, you need a catch. Fix this parsing error and let's see whether there are other problems as well.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175