Reputation: 11
I was trying to connect java with MySQL. I downloaded the connector and added it to classpath of the app on Eclipse IDE and also moved to the "Referenced Libraries" on VSCode. Different projects each ones.
On Eclipse it worked fine, but VSCode display this error:
PS C:\Users\User\Desktop\Programação\Java\J19> c:; cd 'c:\Users\User\Desktop\Programação\Java\J19'; & 'C:\Program Files\Java\jdk-19\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '@C:\Users\User\AppData\Local\Temp\cp_fw615ezm0w8yc6egf85gzwbb.argfile' 'App'
**Error: Could not find or load main class App
Caused by: java.lang.ClassNotFoundException: App**
I realized this error occurs with any jar files I add, not only with the MySQL connector.
I already tried to reinstall VSCode, reinstall java, and change from Java 17 to 19 and nothing fix it.
Some more data:
VSCode Version: 1.72.0 (User Setup)
MySql Version: 8.0.30 Build 2054668 CE
Connector: mysql-connector-java-8.0.30.jar
Eclipse Version: 2022-09 (4.25.0) Build id 20220908-1902
Edit 1:
This is the code:
package banco;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class App {
public static void main(String[] args) {
final String db_url = "jdbc:mysql://localhost:3306/reuniao";
final String db_query = "SELECT * FROM pessoa";
final String db_user = "root";
final String db_password = "";
// Pessoa[]p;
// int reultSetRows = 0;
System.out.println("Iniciando conexão com o DB.");
try (Connection c = DriverManager.getConnection(db_url, db_user, db_password);
Statement statement = c.createStatement();
ResultSet resultSet = statement.executeQuery(db_query)) {
System.out.println("Conectado ao MySQL");
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}
The project was created with Command Pallete (Ctrl+Shift+P) - Create Java Project - No build Tools - Selected my Code Folder and named my project - Created package and start coding.
Upvotes: 0
Views: 233