Tarea de la sesión 6 - JOBP
-
Upload
j-omar-burgos-palacios -
Category
Education
-
view
181 -
download
2
Transcript of Tarea de la sesión 6 - JOBP
PROGRAMACION VISUAL II TAREA DE LA SESION 6
1. Usando una de las tablas de la base de datos de universidad diseña un
formulario que muestre los registros de una tabla en un objeto Table.
En la clase Acceso se hace la conexión de la tabla a llamar en la Base de Datos .
package tarea6;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.*;
import javax.swing.*;
/**
*
* @author Omar
*/
public class Acceso {
static Connection conn=null;
static Statement st=null;
static ResultSet rs=null;
static String bd="universidad";
static String login="root";
static String password="";
static String url="jdbc:mysql://localhost/"+bd;
public static Connection Enlace(Connection conn) throws SQLException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,login,password);
IMPORTAMOS LAS LIBRERIAS
CREAMOS LA CONEXIÓN LA CUAL
CONECTARA CON LA BASE DE
DATOS
HACEMOS LA CONEXIÓN LA
CUAL LLAMARÁ AL USUARIO Y
LA CONTRASEÑA DE LA BASE DE
DATOS
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null, e);
}
return conn;
}
//AQUÍ LA CREACIÓN DE LOS MÉTODOS DE LO QUE SERÁ TODO SOBRE LA
TABLA CURSO
public static Statement sta(Statement st) throws SQLException
{
conn=Enlace(conn);
st=conn.createStatement();
return st;
}
public static ResultSet obtenerCodCur(ResultSet rs, String nom) throws
SQLException
{
st=sta(st);
String sql;
sql="select idcurso from curso where nombre='"+nom+"'";
rs=st.executeQuery(sql);
return rs;
}
public static ResultSet obtenerCursos(ResultSet rs) throws SQLException
{
st=sta(st);
String sql;
sql="select nombre from curso";
rs=st.executeQuery(sql);
return rs;
}
public static ResultSet obtenerCodCurso(ResultSet rs) throws SQLException
{
st=sta(st);
rs=st.executeQuery("Select * from curso");
return rs;
}
}
En el formulario FrmCurso agregamos y llamamos los métodos hechos en la Clase
Acceso.
package tarea6;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import tarea6.Acceso;
/**
*
* @author Omarzinho
*/
public class FrmCurso extends javax.swing.JFrame {
static Connection conn=null;
static Statement st=null;
static ResultSet rs=null;
DefaultTableModel dtm=new DefaultTableModel();
/**
* Creates new form FrmCurso
*/
public FrmCurso() {
initComponents();
String titulos[]={"Código", "Nombre de Curso"};
dtm.setColumnIdentifiers(titulos);
TablaDocente.setModel(dtm);
this.setResizable(false);
setSize(430,350);
setLocation(60,50);
}
private void btnVerActionPerformed(java.awt.event.ActionEvent evt) {
try
{
// setSize(550,480);
int f,i;
conn=Acceso.Enlace(conn);
rs=Acceso.obtenerCodCurso(rs);
String datos[]=new String[2];
f=dtm.getRowCount();
if(f>0)
for(i=0;i<f;i++)
dtm.removeRow(0);
while(rs.next())
IMPORTAMOS LAS LIBRERIAS
CON ELLA LA SQL
LLAMAMOS LA CLASE Connection
creada en la clase Acceso
AQUÍ AGREGAMOS LOS TÍTULOS
A LAS TABLAS
Llamamos el Enlace creado en la
clase Acceso y contando los datos
de las tablas y en la creación de
un blucle damos la señal de
muestra en la tabla .
{
datos[0]=(String)rs.getString(1);
datos[1]=(String)rs.getString(2);
dtm.addRow(datos);
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null, "Error en BD: "+e.toString());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrmCurso().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTable TablaDocente;
private javax.swing.JButton btnVer;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
AQUIE SE AGREGARAN LOS
DATOS SEGÚN LO QUE SE PIDE EN
LA TABLA