import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JTable;
/*
* NewJFrame.java
*
* Created on January 5, 2009, 8:58 AM
*/
/**
*
* @author ATAGHER B T ace
*/
public class NewJFrame extends javax.swing.JFrame {
String select = "SELECT * FROM ver;";
String url = "jdbc:mysql://localhost:3306/test";
String driver ="com.mysql.jdbc.Driver";
String user = "root";
String password = "val";
Statement stmt ;
/** Creates new form NewJFrame */
public NewJFrame() {
try{
Class.forName(driver);
}catch(java.lang.ClassNotFoundException ex){
System.err.println("class not found");
System.err.println(ex.getMessage());
}
Connection con;
try{
con=DriverManager.getConnection(url, user, password);
stmt = con.createStatement();
}catch(SQLException ex){ex.printStackTrace();}
initComponents();
reload();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"name", "sex"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jTable1);
jButton1.setText("print");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 354, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(36, Short.MAX_VALUE))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(255, Short.MAX_VALUE)
.add(jButton1)
.add(90, 90, 90))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.add(37, 37, 37)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 181, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(22, 22, 22)
.add(jButton1)
.addContainerGap(37, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
dispose();
}//GEN-LAST:event_jButton1ActionPerformed
private void reload() {
jScrollPane1.getViewport().remove(jTable1);
String [] tablehead ={ "name", "sex"};
String[][] tablebody = new String[getrows()][2];
int j = 0;
try{
ResultSet rs = stmt.executeQuery(select);
while(rs.next()){
tablebody[j][0] = rs.getString("name");
tablebody[j][1] = rs.getString("sex");
j++;
}
}catch(Exception ex){}
jTable1 = new JTable(tablebody, tablehead);
jScrollPane1.getViewport().add(jTable1);
repaint();
}
int getrows(){
int i = 0;
try{
ResultSet rs = stmt.executeQuery(select);
while(rs.next()){
i++;
}
}catch(Exception ex){}
return i;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration//GEN-END:variables
}