Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Erreur sous Eclipse

4 réponses
Avatar
Craps
A la compilation Eclipse me donne comme message d'erreur qu'il ne trouve pas
la fonction main.
Pourtant elle est présente dans le fichier.
--------------
package mysql;

import java.sql.*;

public class Hello
{
Connection connection;

private void displaySQLErrors(SQLException e)
{
System.out.println("SQLException : " + e.getMessage());
System.out.println("SQLState : " + e.getSQLState());
System.out.println("VendorError : " + e.getErrorCode());
}

public Hello()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(SQLException e)
{
System.err.println("Impossible de trouver le driver");
System.exit(1);
}

}

public void connectToDb()
{
try
{
connection =
DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
}
catch (SQLException e)
{
displaySQLErrors(e);
}
}

public void executeSQL()
{
try
{
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * from personnel");
while (rs.next())
{
System.out.println(rs.getString(1));
}
rs.close();
statement.close();
connection.close();
}
catch (SQLException e)
{
displaySQLErrors(e);
}
}

public static void main(String[] args)
{
Hello hello = new Hello();
hello.connectToDB();
hello.executeSQL();
}
}

4 réponses

Avatar
KiLVaiDeN
"Craps" wrote in message
news:4187e29d$0$3531$
A la compilation Eclipse me donne comme message d'erreur qu'il ne trouve
pas

la fonction main.
Pourtant elle est présente dans le fichier.
--------------
public void connectToDb()
declaration


hello.connectToDB();
in main


Try correcting that typo, then see if it works better.

K

Avatar
Craps
thank's

after correct this mistake i receive following message :

java.lang.Error: Unresolved compilation problems:
Unhandled exception type IllegalAccessException
Unhandled exception type InstantiationException
Unhandled exception type ClassNotFoundException

at Hello.<init>(Hello.java:30)
at Hello.main(Hello.java:75)
Exception in thread "main"
Avatar
Craps
sorry the precedent message is wrong

receive following message

java.lang.Error: Unresolved compilation problems:
Unhandled exception type IllegalAccessException
Unhandled exception type InstantiationException
Unhandled exception type ClassNotFoundException
Unreachable catch block for SQLException. This exception is never thrown
from the try statement body

at Hello.<init>(Hello.java:32)
at Hello.main(Hello.java:83)
Exception in thread "main"
Avatar
KiLVaiDeN
"Craps" wrote in message
news:41881c37$0$24574$
sorry the precedent message is wrong

receive following message

java.lang.Error: Unresolved compilation problems:
Unhandled exception type IllegalAccessException
Unhandled exception type InstantiationException
Unhandled exception type ClassNotFoundException
Unreachable catch block for SQLException. This exception is never thrown
from the try statement body

at Hello.<init>(Hello.java:32)
at Hello.main(Hello.java:83)
Exception in thread "main"




Means that in your try and catch you must intercept those exceptions :
IllegalAccessException InstantiationException ClassNotFoundException

and _not_ this one : SQLException