Hola Les dejo algo q' me sirvio para empezar a ver algo de BD's con Java ...!! >:D
Todo lo q hace la aplicacion es probar el DSN mostrando el nombre y la version del
gestor de BD's :P
Espero q les sirva de plantilla ;-)
Importamos las librerias necesarias:
import java.awt.*;
import java.sql.*;
Declaramos e inicialisamos las variables q´vamos a utlizar:
Asignamos una posicion al JFrame
public FromProbarDSNEjemploBDs()
{
initComponents();
setLocation(350,250); //Localizacion
}
Para cambiar el Icono del cafe de Java, reescribimos este metodo.!
Nota: http://javax0711.blogspot.com/2009/11/como-cambiar-el-icono-un-jframe.html
public Image getIconImage
() {
getImage(ClassLoader.
getSystemResource("resources/hacker.png")); return retValue;
}
Para indicar q tipo de Driver vamos a utilizar, ponemos la sentencia:
//Usa el puente JDBC-ODBC
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
El administrador de Drivres
(DriverManager) del JDBC, para el caso del puente
JDBC-ODBC, nos devuelve un
Objeto de tipo
Connection con el cual se
apertura el acceso a la BD's
La Metadata de la BD's es pedida a traves de un
Objeto de la clase
Connection (cn)
meta=cn.getMetaData();
El objeto meta de la Clase
DatabaseMetaData nos permite obtener el nombre y la
version del gestor de BD's :
txaMostrar.setText("\n Exito en la conexión...!\n");
txaMostrar.append("\n DataBase:\t"+meta.getDatabaseProductName());
txaMostrar.append("\n version:\t"+meta.getDatabaseProductVersion());
Podemos manipular las Excepciones a traves de un
Objeto de la clase
Exception:
{
txaMostrar.setText("La connecion Fracaso...!! por:\n\n");
txaMostrar.append(ex.toString());
}
Les dejo el codigo completo:
import java.awt.*;
import java.sql.*;
public class FromProbarDSNEjemploBDs extends javax.swing.JFrame
{
private Connection cn= null;
private DatabaseMetaData meta= null;
public FromProbarDSNEjemploBDs()
{
initComponents();
setLocation(350,250);
}
public Image getIconImage()
{
Image retValue=Toolkit.getDefaultToolkit().
getImage(ClassLoader.getSystemResource("resources/hacker.png"));
return retValue;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnProbarDSNEjemploBD = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txaMostrar = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ElHacker.net ");
setIconImage(getIconImage());
setResizable(false);
btnProbarDSNEjemploBD.setText("Probar DSNEjemploBD");
btnProbarDSNEjemploBD.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnProbarDSNEjemploBDActionPerformed(evt);
}
});
txaMostrar.setColumns(20);
txaMostrar.setRows(5);
jScrollPane1.setViewportView(txaMostrar);
jLabel1.setText("Horny3");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(103, 103, 103)
.addComponent(btnProbarDSNEjemploBD))
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(30, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnProbarDSNEjemploBD)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnProbarDSNEjemploBDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:DSNEjemploBD");
meta=cn.getMetaData();
txaMostrar.setText("\n Exito en la conexión...!\n");
txaMostrar.append("\n DataBase:\t"+meta.getDatabaseProductName());
txaMostrar.append("\n version:\t"+meta.getDatabaseProductVersion());
cn.close();
}
catch(Exception ex)
{
txaMostrar.setText("La connecion Fracaso...!! por:\n\n");
txaMostrar.append(ex.toString());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FromProbarDSNEjemploBDs().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnProbarDSNEjemploBD;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txaMostrar;
// End of variables declaration
}
SaludosHorny3