Tener agregado en libraries el conector mysql.
clase coneBD.java
package pktForm12;
import java.sql.*;
import javax.swing.JOptionPane;
public class coneBD {
public static PreparedStatement prest=null;
public static CallableStatement cllst=null;
public static Connection conec=null;
public static Statement st=null;
public static ResultSet rt=null;
public static String usu;
public static String contra;
public static String host;
public static String db;
public void Conectar(){
try{
usu="root"; contra=""; host="localhost"; db="ventas";
String url="jdbc:mysql://"+host+"/"+db;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conec=DriverManager.getConnection(url,usu,contra);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
}
package pktForm12;
import java.sql.*;
import javax.swing.JOptionPane;
public class coneBD {
public static PreparedStatement prest=null;
public static CallableStatement cllst=null;
public static Connection conec=null;
public static Statement st=null;
public static ResultSet rt=null;
public static String usu;
public static String contra;
public static String host;
public static String db;
public void Conectar(){
try{
usu="root"; contra=""; host="localhost"; db="ventas";
String url="jdbc:mysql://"+host+"/"+db;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conec=DriverManager.getConnection(url,usu,contra);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
}
package pktForm12;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class frmListar extends javax.swing.JFrame {
coneBD cn=new coneBD();
DefaultTableModel modelo=new DefaultTableModel();
public frmListar() {
initComponents();
String titu[]={"IDCliente","Nombres","Apellidos","Direccion","Telefono","E-mail"};
modelo.setColumnIdentifiers(titu);
tblClientes.setModel(modelo);
}
private void llenarT() {
String SQL="select * from clientes;";
String datos[]=new String[6];
try{
cn.Conectar();
cn.st=cn.conec.createStatement();
cn.rt=cn.st.executeQuery(SQL);
while (cn.rt.next()){
datos[0]=cn.rt.getString(1);
datos[1]=cn.rt.getString(2);
datos[2]=cn.rt.getString(3);
datos[3]=cn.rt.getString(4);
datos[4]=cn.rt.getString(5);
datos[5]=cn.rt.getString(6);
modelo.addRow(datos);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
private void btnListarActionPerformed(java.awt.event.ActionEvent evt) {
llenarT();
}
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class frmListar extends javax.swing.JFrame {
coneBD cn=new coneBD();
DefaultTableModel modelo=new DefaultTableModel();
public frmListar() {
initComponents();
String titu[]={"IDCliente","Nombres","Apellidos","Direccion","Telefono","E-mail"};
modelo.setColumnIdentifiers(titu);
tblClientes.setModel(modelo);
}
private void llenarT() {
String SQL="select * from clientes;";
String datos[]=new String[6];
try{
cn.Conectar();
cn.st=cn.conec.createStatement();
cn.rt=cn.st.executeQuery(SQL);
while (cn.rt.next()){
datos[0]=cn.rt.getString(1);
datos[1]=cn.rt.getString(2);
datos[2]=cn.rt.getString(3);
datos[3]=cn.rt.getString(4);
datos[4]=cn.rt.getString(5);
datos[5]=cn.rt.getString(6);
modelo.addRow(datos);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
private void btnListarActionPerformed(java.awt.event.ActionEvent evt) {
llenarT();
}
Se programa en Java, PHP, MySql, Visual Basic .Net desde 100 USD - Tec. Miguel Nunez minuvasoft10@gmail.com