Program in Java does not connect correctly with MySQL in Ubuntu

1

I have a problem with my java program, because my program does not find the bd.

this is my class connect here is all fine because sis is connected to My sql but when I make a query just throws me the message I can not actalizar that I have when he throws an error.

package contabilidad_sgc;

import java.sql.Connection;
import com.mysql.cj.jdbc.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class conectar {

     Connection conn=null;
     String bd="sistema_contabilidad";
     String login="root";
     String password="root";
     String url="jdbc:mysql://localhost/sistema_contabilidad_sgc";

     public Connection conexion(){
        try{
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        conn=DriverManager.getConnection(url,login,password);
        }
        catch(ClassNotFoundException e){

                JOptionPane.showMessageDialog(null, "No se pudo establecer la conexión con la base de datos "+bd);
        } catch (SQLException ex) {
            Logger.getLogger(conectar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(conectar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(conectar.class.getName()).log(Level.SEVERE, null, ex);
        }

client class which makes the query:

package contabilidad_sgc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jeredick Escobar
 */
public class cliente extends javax.swing.JFrame {

    conectar cn = new conectar();
    Connection cc = cn.conexion();
    JTable table;
    DefaultTableModel dfm = new DefaultTableModel();
    public cliente() {
        initComponents();

        table = this.jTable1;
        table.setModel(dfm);
        dfm.setColumnIdentifiers(new Object[]{"ID","NIT","NOMBRE","DIRECCION","E-MAIL","TELEFONO","MOVIL"});
        try{
            Statement st = cc.createStatement();
            ResultSet rs= st.executeQuery("SELECT * FROM CLIENTE");

            while(rs.next()){
                dfm.addRow(new Object[]{rs.getInt("ID"),rs.getString("NIT"), rs.getString("NOMBRE"),rs.getString("DIRECCION1"),rs.getString("DIRECCION2"),rs.getInt("TELEFONO1"),rs.getInt("TELEFONO2")});
            }
            dfm.addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                if(e.getType()==TableModelEvent.UPDATE){
                int columna = e.getColumn();
                int fila = e.getFirstRow();
                if(columna==1){
                    String sql = "UPDATE CLIENTE SET NIT = '"+jTable1.getValueAt(fila, columna)+"' WHERE ID ="+jTable1.getValueAt(fila, 0);
                    actualizar_data(sql);
                }
                }
            }
            });  
        }
        catch(SQLException e){
            JOptionPane.showMessageDialog(null, "No se pudo actualizar");
        }
    }
    void actualizar_data(String act){
            try{
                PreparedStatement ps = cc.prepareStatement(act);
                ps.execute();

            }
            catch(SQLException e){
            JOptionPane.showMessageDialog(null, "No se pudo actualizar");
            }
    }

thanks for your help

    
asked by Jeredick Escobar 15.05.2018 в 04:49
source

2 answers

0

You should check the name of the database because I notice that you have a variable called bd and you have it assigned as sistema_contabilidad , while in the url you have:

String url="jdbc:mysql://localhost/sistema_contabilidad_sgc";

You have different names in bd and url database.

You should also check the class, because you have:

Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

And I've seen it is:

Class.forName("com.mysql.jdbc.Driver").newInstance();

Without the cj .

    
answered by 15.05.2018 в 06:39
0

Are you sure that a connection is returned here?

connect cn = new connect (); Connection cc = cn.connection ();

I would think that when you create the statment you do not have the connection.

    
answered by 15.05.2018 в 12:11