Java JDBC Database Connection and Client Management Class
Classified in Computers
Written on in
English with a size of 3.11 KB
Java Database Client Management Class
Below is the complete implementation of the beanClientes Java class designed to handle database connections, search client records by telephone, generate unique client codes, and insert new records using JDBC and Java Swing.
Source Code Implementation
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class beanClientes {
private static Connection xcn = null;
private Statement xst = null;
private ResultSet xrs = null;
private String xcod, xnom, xdir, xref, xtel;
public void EnlasaBeanClientes(Connection cn) {
try {
xcn = cn;
xst = xcn.createStatement();
} catch (SQLException ex) {
Logger.getLogger(beanClientes.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void buscaXtelefono(String tel) {
try {
String sqlbusca;
sqlbusca = "select * from clientes where telcli ='" + tel + "'";
xrs = xst.executeQuery(sqlbusca);
if (xrs.next()) {
xcod = xrs.getString(1);
xnom = xrs.getString(2);
xdir = xrs.getString(3);
xref = xrs.getString(4);
xtel = xrs.getString(5);
} else {
JOptionPane.showMessageDialog(null, "cliente nuevo, ingresar datos");
xcod = generarCodigo();
xnom = "";
xdir = "";
xref = "";
xtel = tel;
}
} catch (SQLException ex) {
Logger.getLogger(beanClientes.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String getCodigo() {
return xcod;
}
public String getNombre() {
return xnom;
}
public String getDireccion() {
return xdir;
}
public String getreferencia() {
return xref;
}
public String getTelefono() {
return xtel;
}
public String generarCodigo() {
String cod = "";
String sqlbusca;
int numero;
try {
sqlbusca = "select max(codcli) from clientes";
xrs = xst.executeQuery(sqlbusca);
xrs.next();
cod = xrs.getString(1);
numero = Integer.parseInt(cod.substring(3, 6)) + 1;
cod = "000" + numero;
cod = "cli" + cod.substring(cod.length() - 3, cod.length());
} catch (SQLException ex) {
Logger.getLogger(beanClientes.class.getName()).log(Level.SEVERE, null, ex);
}
return cod;
}
public int agregaCliente(String cod, String tel, String nom, String dir, String ref) {
String sqlagrega;
int n = 0;
try {
sqlagrega = "insert into clientes values('" + cod + "','" + nom + "','" + dir + "','" + ref + "','" + tel + "')";
n = xst.executeUpdate(sqlagrega);
} catch (SQLException ex) {
Logger.getLogger(beanClientes.class.getName()).log(Level.SEVERE, null, ex);
}
return n;
}
}