Java Help ekak

lilman

Well-known member
  • May 10, 2009
    40,272
    53,321
    113
    Colombo
    machan netbeans wala indala GUI application ekakin mysql database ekakata connect wena code eka hoyanala denna puluwwanda ? :sorry:
     

    sajithswa

    Active member
  • Dec 24, 2007
    120
    46
    28
    package data;

    import data.Support.Msg;
    import java.sql.*;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Database {

    Connection con;
    Statement stm;
    Connection con2;
    Statement stm2;
    ResultSet rs;
    PreparedStatement pstm;

    public Database() {
    con = null;
    try {
    String[] s = new Reg().getValue();
    String ip = s[0];
    String un = s[1];
    String pw = s[2];

    // String ip = "localhost";
    // String un = "root";
    // String pw = "123";
    if (ip == null) {
    Msg.errorMsg("Please set data base ip address");
    FormLoad.loadDBSetting();
    }

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/pos", un, pw);
    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    con2 = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/pos", un, pw);
    stm2 = con2.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    } catch (Exception e) {
    Msg.errorMsg("Please set database settings");
    FormLoad.loadDBSetting();
    }
    }

    public int update(String query) {
    int r = 0;
    if (con != null) {
    try {
    r = stm.executeUpdate(query);
    System.out.println(r);
    return r;
    } catch (SQLException ex) {
    //Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    System.out.println(r);
    return r;
    }
    }
    return r;
    }

    public void select(String query) {
    if (con != null) {
    try {
    System.out.println(query);
    rs = stm.executeQuery(query);
    } catch (SQLException ex) {
    Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
    }

    public void endSelect() {
    try {
    if (rs != null) {
    rs.close();
    }
    if (stm != null) {
    stm.close();
    }
    if (stm2 != null) {
    stm2.close();
    }
    if (con != null) {
    con.close();
    }
    if (con2 != null) {
    con2.close();
    }
    } catch (SQLException ex) {
    Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    public void close() {
    if (con != null) {
    try {
    stm.close();
    stm2.close();
    con.close();
    con2.close();
    } catch (SQLException ex) {
    Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
    }
    }
     
    Last edited:

    lilman

    Well-known member
  • May 10, 2009
    40,272
    53,321
    113
    Colombo
    import data.Support.Msg;
    import java.sql.*;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    oya tika form eke udina dala.

    database connect wena code eka form load ekata daapuwama hari da machan ?
     

    sajithswa

    Active member
  • Dec 24, 2007
    120
    46
    28
    oya tika form eke udina dala.

    database connect wena code eka form load ekata daapuwama hari da machan ?

    Machan java wediya danne nedda?

    Use ur form constructor for DB connetion.

    Menna me tika class eka patan gatthata passe danna.

    Connection con;
    Statement stm;
    ResultSet rs;


    Ita passe constructor eka athule me tika danna.

    try {
    String ip = "localhost";
    String un = "root";
    String pw = "123";
    con = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/pos", un, pw);
    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    } catch (Exception e) {
    }



    If ur class is frmGUI code look like this...

    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class frmGUI extends javax.swing.JFrame {

    Connection con;
    Statement stm;
    ResultSet rs;

    public frmGUI() {
    try {
    String ip = "localhost";
    String un = "root";
    String pw = "123";
    con = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/pos", un, pw);
    stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    } catch (Exception e) {
    }
    }
    }


    Then u an use stm variable for transaction like this way.

    public ResultSet select(String query) {
    if (con != null) {
    try {
    System.out.println(query);
    return rs = stm.executeQuery(query);
    } catch (SQLException ex) {
    Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
    return null;
    }

     

    sajithswa

    Active member
  • Dec 24, 2007
    120
    46
    28
    in here
    con = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/pos", un, pw);

    pos is ur database name
     

    ramishka

    Active member
  • Feb 11, 2007
    441
    189
    43


    Then u an use stm variable for transaction like this way.

    public ResultSet select(String query) {
    if (con != null) {
    try {
    System.out.println(query);
    return rs = stm.executeQuery(query);
    } catch (SQLException ex) {
    Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
    return null;
    }


    It's highly recommended to close the statement in a finally clause.

    Code:
    finally {
        stm.close();
    }

    Also, the Connection needs to be closed at some point too.
    In java it's safest to close your ResultSet, Statement, and Connection (in that order) in a finally block when they are done being used.
     
    Last edited:

    sajithswa

    Active member
  • Dec 24, 2007
    120
    46
    28
    It's highly recommended to close the statement in a finally clause.

    Code:
    finally {
        stm.close();
    }

    Also, the Connection needs to be closed at some point too.
    In java it's safest to close your ResultSet, Statement, and Connection (in that order) in a finally block when they are done being used.

    YES
    U can find full method for that it in my full Database class. This gay needs very basic cording... :yes::yes::yes::yes:
     

    lilman

    Well-known member
  • May 10, 2009
    40,272
    53,321
    113
    Colombo
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package cicproject;
    
    /**
     *
     * @author lilman
     */
    public class Scr2 extends javax.swing.JInternalFrame {
    
        /**
         * Creates new form Scr2
         */
        public Scr2() {
            initComponents();
        }
    
        /**
         * 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.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jLabel7 = new javax.swing.JLabel();
            jLabel8 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jTextField2 = new javax.swing.JTextField();
            jTextField3 = new javax.swing.JTextField();
            jTextField4 = new javax.swing.JTextField();
            jTextField5 = new javax.swing.JTextField();
            jTextField6 = new javax.swing.JTextField();
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
    
            setClosable(true);
            setTitle("New supplier");
    
            jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N
            jLabel1.setText("New Supplier");
    
            jLabel2.setText("Supplier ID");
    
            jLabel3.setText("First Name");
    
            jLabel4.setText("Last Name");
    
            jLabel5.setText("Address");
    
            jLabel6.setText("Street");
    
            jLabel7.setText("City");
    
            jLabel8.setText("Telephone No");
    
            jButton1.setText("Submit");
    
            jButton2.setText("Cancel");
    
            jButton3.setText("Exit");
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(34, 34, 34)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2)
                        .addComponent(jButton3)))
            );
    
            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(72, 72, 72)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel8)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(37, 37, 37)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel7)
                                        .addComponent(jLabel6)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel2)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)))
                                        .addGap(99, 99, 99)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                            .addComponent(jTextField2)
                                            .addComponent(jTextField3)
                                            .addComponent(jTextField4)
                                            .addComponent(jTextField5)
                                            .addComponent(jTextField6))))))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(139, 139, 139)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(77, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jLabel5)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel6)
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addComponent(jLabel7))
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(41, 41, 41)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(56, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        private javax.swing.JTextField jTextField5;
        private javax.swing.JTextField jTextField6;
        // End of variables declaration                   
    }
    machan meka tama frm eke souce code eka.add karana place tika kiyanna puluwanda plese.mata echara java be ban :(:sorry:
     
    Last edited:

    ramishka

    Active member
  • Feb 11, 2007
    441
    189
    43
    YES
    U can find full method for that it in my full Database class. This gay needs very basic cording... :yes::yes::yes::yes:

    Closing the database connection is pretty basic and essential. It's the kind of thing that if left unchecked can lead to memory leaks and even database server crashes. If this was done in an assignment or a job interview it will definitely reduce the scores.