Codigo
Codigo
Codigo
java
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
Connection conectar=null;
try{
Class.forName("org.gjt.mm.mysql.Driver");
conectar=(Connection)
DriverManager.getConnection("jdbc:mysql://localhost/paises","root","vico243");
}catch(SQLException ex) {
}catch(ClassNotFoundException ex) { }
return conectar;
java.sql.Connection conexion() {
}
tabla.java
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public tabla() {
initComponents();
mostrardatos(" ");
Connection cn=cc.paises();
modelo.addColumn("pais");
modelo.addColumn("codigo");
modelo.addColumn("poblacion");
jTable1.setModel(modelo);
String sql="";
if (valor.equals(""))
else
try
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next())
datos[0]=rs.getString(1);
datos[1]=rs.getString(2);
datos[2]=rs.getString(3);
modelo.addRow(datos);
jTable1.setModel(modelo);
}catch(SQLException ex)
Logger.getLogger(datos.class.getName()).log(Level.SEVERE,null,ex);
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
jMenuItem1.setText("modificar");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
jMenuItem1ActionPerformed(evt);
});
jPopupMenu1.add(jMenuItem1);
jMenuItem2.setText("eliminar");
jMenuItem2.setToolTipText("");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
});
jPopupMenu1.add(jMenuItem2);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("pais");
jLabel2.setText("codigo");
jLabel3.setText("poblacion");
jTextField3.addActionListener(new java.awt.event.ActionListener() {
jTextField3ActionPerformed(evt);
});
jButton1.setText("insertar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
jButton1ActionPerformed(evt);
});
jButton2.setText("mostrar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("buscar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
jButton3ActionPerformed(evt);
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
{},
{},
{},
{}
},
new String [] {
));
jTable1.setComponentPopupMenu(jPopupMenu1);
jScrollPane1.setViewportView(jTable1);
jButton5.setText("modificar");
jButton5.addActionListener(new java.awt.event.ActionListener() {
jButton5ActionPerformed(evt);
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField2)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
135, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2)
.addComponent(jButton1)
.addComponent(jButton5)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5)
.addComponent(jButton2)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
);
pack();
}// </editor-fold>
Connection cn=cc.paises();
try
pst.setString(1,jTextField1.getText());
jTextField1.setText(null);
pst.setString(2,jTextField2.getText());
jTextField2.setText(null);
pst.setString(3,jTextField3.getText());
jTextField3.setText(null);
int a=pst.executeUpdate();
if(a>0)
JOptionPane.showMessageDialog(null,"Registro exitoso");
mostrardatos("");
else
JOptionPane.showMessageDialog(null,"Error al agregar");
mostrardatos("");
mostrardatos(jTextField4.getText());
jTextField4.setText(null);
String pais="";
if(fila>=0)
jTextField1.setText(jTable1.getValueAt(fila,0).toString());
jTextField2.setText(jTable1.getValueAt(fila,1).toString());
jTextField3.setText(jTable1.getValueAt(fila,2).toString());
pais = jTable1.getValueAt(fila,0).toString();
else
Connection cn=cc.paises();
try
pais = jTextField1.getText();
jTextField1.setText(null);
jTextField2.setText(null);
jTextField3.setText(null);
pst.executeUpdate();
mostrardatos("");
}catch(SQLException e)
{
System.out.print(e.getMessage());
Connection cn=cc.paises();
int fila=jTable1.getSelectedRow();
String cod="";
cod=jTable1.getValueAt(fila,0).toString();
try
pst.executeUpdate();
mostrardatos("");
java.awt.EventQueue.invokeLater(() -> {
new tabla().setVisible(true);
});
}
// Variables declaration - do not modify