I have a problem with my code, my program fills a JTable form a MySQL DB using a thread, the program works fine but I get this error
//Error, com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" Error java.lang.NullPointerException//
I think I need to close the connection or maybe don't use a Thread for fills the JTable, my idea it's fills the table with out using a JButton event, do you have a suggestion for this?
public class VentanaAdministrador extends javax.swing.JFrame{
public static final String URL = "jdbc:mysql://localhost:3306/floreria?autoReconnet=true&useSSL=false";
public static final String Usuario = "root";
public static final String Contraseña = "";
PreparedStatement ps;
ResultSet rs;
public Connection getConnection(){
Connection conexion = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conexion = (Connection) DriverManager.getConnection(URL,Usuario,Contraseña);
}catch(Exception ex){
System.err.println("Error, "+ex);
}
return conexion;
}
///////////////////////////////////////////////
Thread llenado = new Thread(){
public void run(){
DefaultTableModel modeloTabla = new DefaultTableModel();
TablaVentanaAsignar.setModel(modeloTabla);
PreparedStatement ps = null;
ResultSet rs = null;
try{
VentanaAdministrador con = new VentanaAdministrador();
Connection VentanaAdministrador = con.getConnection();
ps = VentanaAdministrador.prepareStatement("select NumeroEmpleado,Nombre,Punto_de_venta,TiempoTrabajo from empleados where Puesto=?");
ps.setString(1, "Diseñador");
rs = ps.executeQuery();
modeloTabla.addColumn("No. Empleado");
modeloTabla.addColumn("Nombre");
modeloTabla.addColumn("Sucursal");
modeloTabla.addColumn("Tiempo trabajado");
while(rs.next()){
Object fila[] = new Object[4];
fila[0] = rs.getObject(1);
fila[1] = rs.getObject(2);
fila[2] = rs.getObject(3);
fila[3] = rs.getObject(4);
modeloTabla.addRow(fila);
}
}catch(Exception ex){
System.err.println("Error "+ex);
}
}
};
public VentanaAdministrador() {
initComponents();
llenado.start();
}