// Explore and find out what new can you do with the following JDBC logic. (includes netbeans auto designed code).
import java.sql.*;
import javax.swing.*;
public class JdbcGUI extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
ResultSetMetaData rsmd;
JLabel l[];
JTextField t[];
String dsn,tname;
boolean notnull;
public JdbcGUI() {
initComponents();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dsn = JOptionPane.showInputDialog("Enter your DSN name:");
con = DriverManager.getConnection("jdbc:odbc:"+dsn);
tname = JOptionPane.showInputDialog("Enter your Table name");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("select * from "+tname);
jLabel2.setText("DATABASE FOR "+tname.toUpperCase());
rsmd = rs.getMetaData();
if(rs.next())
notnull=true;
else
notnull=false;
l = new JLabel[rsmd.getColumnCount()];
t = new JTextField[rsmd.getColumnCount()];
for(int i=0;i<rsmd.getColumnCount();i++)
{
l[i] = new JLabel(rsmd.getColumnName(i+1).toUpperCase());
l[i].setBounds(jLabel1.getBounds());
l[i].setLocation(jLabel1.getX(), jLabel1.getY()+(i*25));
this.getContentPane().add(l[i]);
t[i] = new JTextField(20);
t[i].setBounds(jTextField1.getBounds());
t[i].setLocation(jTextField1.getX(), jTextField1.getY()+(i*25));
this.getContentPane().add(t[i]);
if(notnull)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}
jLabel1.setVisible(false);
jTextField1.setVisible(false);
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error: "+cnfe);
}
catch(SQLException sqe)
{
System.out.println("Error: "+sqe);
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jLabel1.setText("getLabelsHere");
jTextField1.setText("getDataValuesHere");
jLabel2.setText("DataBase for Employees");
jButton1.setText("Next");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Previous");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("First");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Last");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(140, 140, 140)
.add(jLabel2)
.add(143, 143, 143))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(19, 19, 19)
.add(jLabel1)
.add(18, 18, 18)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 72, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(63, 63, 63))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(jLabel2)
.add(16, 16, 16)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton1))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton4)
.add(112, 112, 112))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try
{
if(rs.next())
{
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}
else
{
rs.last();
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton1ActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
try
{
con.close();
}
catch(Exception e){}
}//GEN-LAST:event_formWindowClosing
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
try
{
if(rs.previous())
{
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}
else
{
rs.first();
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
try
{
if(rs.first())
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
try
{
if(rs.last())
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton4ActionPerformed
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JdbcGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
import java.sql.*;
import javax.swing.*;
public class JdbcGUI extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
ResultSetMetaData rsmd;
JLabel l[];
JTextField t[];
String dsn,tname;
boolean notnull;
public JdbcGUI() {
initComponents();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dsn = JOptionPane.showInputDialog("Enter your DSN name:");
con = DriverManager.getConnection("jdbc:odbc:"+dsn);
tname = JOptionPane.showInputDialog("Enter your Table name");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("select * from "+tname);
jLabel2.setText("DATABASE FOR "+tname.toUpperCase());
rsmd = rs.getMetaData();
if(rs.next())
notnull=true;
else
notnull=false;
l = new JLabel[rsmd.getColumnCount()];
t = new JTextField[rsmd.getColumnCount()];
for(int i=0;i<rsmd.getColumnCount();i++)
{
l[i] = new JLabel(rsmd.getColumnName(i+1).toUpperCase());
l[i].setBounds(jLabel1.getBounds());
l[i].setLocation(jLabel1.getX(), jLabel1.getY()+(i*25));
this.getContentPane().add(l[i]);
t[i] = new JTextField(20);
t[i].setBounds(jTextField1.getBounds());
t[i].setLocation(jTextField1.getX(), jTextField1.getY()+(i*25));
this.getContentPane().add(t[i]);
if(notnull)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}
jLabel1.setVisible(false);
jTextField1.setVisible(false);
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error: "+cnfe);
}
catch(SQLException sqe)
{
System.out.println("Error: "+sqe);
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jLabel1.setText("getLabelsHere");
jTextField1.setText("getDataValuesHere");
jLabel2.setText("DataBase for Employees");
jButton1.setText("Next");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Previous");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("First");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Last");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(140, 140, 140)
.add(jLabel2)
.add(143, 143, 143))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(19, 19, 19)
.add(jLabel1)
.add(18, 18, 18)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 72, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.add(63, 63, 63))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(jLabel2)
.add(16, 16, 16)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton1))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton4)
.add(112, 112, 112))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try
{
if(rs.next())
{
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}
else
{
rs.last();
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton1ActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
try
{
con.close();
}
catch(Exception e){}
}//GEN-LAST:event_formWindowClosing
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
try
{
if(rs.previous())
{
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}
else
{
rs.first();
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
try
{
if(rs.first())
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
try
{
if(rs.last())
for(int i=0;i<t.length;i++)
{
t[i].setText(String.valueOf(rs.getObject(i+1)).toUpperCase());
}
}catch(SQLException sqe){System.out.println("Error: "+sqe);}
}//GEN-LAST:event_jButton4ActionPerformed
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JdbcGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
No comments:
Post a Comment