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