Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

How To Retrieve Data Into Database (Java)


Source below will show you, how to create simple counter in java.

*********************************************************************
COMPLETE SOURCE CODE FOR : RetriveData.java
*********************************************************************
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author xp
 */
public class RetriveData extends javax.swing.JFrame {

    /** Creates new form RetriveData */
    public RetriveData() throws Exception {
        initComponents();
        ConnectionTest();
    }

    /** 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.
     */
    public static Connection getConnection()throws Exception
    {

            String driver="com.mysql.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/mytable";
            String uname="root";
            String pass="";
            Class.forName(driver);
            java.sql.Connection con=DriverManager.getConnection(url,uname,pass);
            return (Connection) con;

    }
    public void ConnectionTest()throws Exception
    {
        Connection con=null;
        Statement stmt=null;
        ResultSet rs=null;
       ResultSetMetaData rsmd=null;
       Vector columnNames=new Vector();
       Vector data=new Vector();
       try{
           con=getConnection();
           String query="SELECT * FROM retrivedata";
           stmt=(Statement)con.createStatement();
           rs=(ResultSet) stmt.executeQuery(query);
           rsmd=rs.getMetaData();
           int columns=rsmd.getColumnCount();
           //get Columns
           for(int i=1;i<columns;i++)
           {
               columnNames.addElement(i);
           }
           //get Row
           while(rs.next())
           {
               Vector row=new Vector();
                for(int i=1;i<columns;i++)
                   
                {
                    row.addElement(rs.getObject(i));
                }
               data.addElement(row);
           }
           rs.close();
           stmt.close();
       }catch(Exception e)
       {
           e.printStackTrace();
       }
       //jTable1.setModel(new DefaultTableModel(new Object [][] {},new String [] {"id","Name","Address","Age"}));
       JTable jTable1=new JTable(data,columnNames);
        jScrollPane1.setViewportView(jTable1);
   
      //jTable1.setAutoCreateRowSorter(true);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new RetriveData().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(RetriveData.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************