Reputation: 97
Error: Could not find or load main class...
What am I doing wrong when trying to connect Maria DB in java? I'm a novice developer in java so don't be rude...
Am I compiling in a wrong way this file? Also I tried to download mysql and mariaDb drivers but still had no success...I think the driver is not the problem, appreciate all the guidance and help.
This is all my code:
Panaderia.java
package proyecto;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.sql.*;
public class Panaderia extends JFrame implements ActionListener{
JTextArea tablas_ta;
JTextArea registros_ta;
JButton consultarTablas_btn;
JButton consultarRegistros_btn;
JTextField textField;
/*
* Método constructor
**/
public Panaderia(){
super("Ejemplo de Java + MySQL");
//Configurar la operación de salida por defecto para JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,750);
setLayout(new FlowLayout());
consultarTablas_btn = new JButton("Consultar tablas");
consultarTablas_btn.setActionCommand("ConsultarTablas");
add(consultarTablas_btn);
tablas_ta = new JTextArea(10, 70);
add(tablas_ta);
textField = new JTextField(30);
add(textField);
consultarRegistros_btn = new JButton("Consultar registros");
consultarRegistros_btn.setActionCommand("ConsultarRegistros");
add(consultarRegistros_btn);
registros_ta = new JTextArea(10, 70);
add(registros_ta);
// Usar este objeto como Manejador de eventos
consultarTablas_btn.addActionListener(this);
consultarRegistros_btn.addActionListener(this);
textField.addActionListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
Panaderia mjf = new Panaderia();
}
@Override
public void actionPerformed(ActionEvent evento) {
String db, dbuser, pass;
db = "ejemplos";//cambiar por el nombre de la base de datos que desean utilizar
dbuser = "root";
pass = "";
if(evento.getActionCommand()=="ConsultarTablas") {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/"+db, dbuser, pass);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SHOW TABLES");
while( rs.next() ) {
tablas_ta.append( rs.getString(1) + "\n");
}
tablas_ta.append("\n");
con.close();
} catch(Exception e) {
System.out.println(e);
}
}
if(evento.getActionCommand()=="ConsultarRegistros") {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/"+db, dbuser, pass);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM "+textField.getText());
String registro = "";
while( rs.next() ) {
for(int i=1;i<=rs.getMetaData().getColumnCount();i++){
registro = registro + "\t" + rs.getString(i);
}
registros_ta.append( registro +"\n");
registro = "";
}
registros_ta.append("\n");
con.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
}
The error showing in terminal is:
How should you fix it?
Upvotes: 0
Views: 978
Reputation: 405
Hope this will help you.
Upvotes: 1
Reputation: 54
Your code seems correct. I had run on my eclipse. The only thing, you have not supplied a password for the database. I will advise you to move to one of these IDE - Eclipse or Netbeans. Compiling on the command line is time taking and not useful.
Upvotes: 1