Java JDBC

manarrow

Well-known member
  • Jan 31, 2015
    4,991
    5,140
    113
    Man baluwa machan eth mata terenne na :(

    me thiyenne mage db class eka.. mekata anuwa thama mama insert, update, search anan manan gahala thiyenne.

    public class DB {

    public static Connection c;
    public static Statement st;
    public static ResultSet rs = null;

    public static Connection myConnection() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    c = DriverManager.getConnection("jdbc:mysql://localhost:3307/inventory_control_system", "root", "123");
    return c;
    }

    public static void save(String para) throws Exception, NullPointerException {
    if (c == null) {
    myConnection();
    }

    c.createStatement().executeUpdate(para);

    }

    public static ResultSet search(String quary) throws Exception, NullPointerException {

    if (c == null) {
    myConnection();
    }

    rs = c.createStatement().executeQuery(quary);
    return rs;
    }
    }


    onna othana ubata oona karana insert update query dekath ekka thawath usefull dewal thiyanawa. meka me mama hadapu small inventory ekaka code ekak. therun aran karpan.


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
    int x = 0;
    try {
    for (int i = 0; i < jTable1.getRowCount(); i++) {
    String pid = jTable1.getValueAt(i, 0).toString();
    String p_name = jTable1.getValueAt(i, 1).toString();
    String quantity = jTable1.getValueAt(i, 2).toString();
    String unit_price = jTable1.getValueAt(i, 3).toString();
    String sub_total = jTable1.getValueAt(i, 4).toString();

    DB.save("insert into invoice (Customer_Customer_ID, Product_Name, Unit_Price, Quntity, Discount, Net_Total, Cash, Balance, Date, Time) values "
    + "('"+txt_CustomerID.getText()+"', '"+p_name+"', '"+unit_price+"', '"+quantity+"', '"+txt_discount.getText()+"', '"+txt_netTotal.getText()+"', '"+txt_cash.getText()+"', '"+txt_balance.getText()+"', '"+Date.getText()+"', '"+Time.getText()+"')");

    DB.search("select Invoice_ID from invoice where Invoice_ID='"+jTextField3.getText()+"'");
    if(DB.rs.next()){
    x = Integer.parseInt(DB.rs.getString("Invoice_ID").toString());
    }

    DB.save("insert into invoice_registry (Invoice_Invoice_ID, Stock_Product_ID, Product_Name, Unit_Price, Quantity, Total, Cash, Balance, Date, Time) values "
    + "('"+x+"', '"+pid+"', '"+p_name+"', '"+unit_price+"', '"+quantity+"', '"+sub_total+"', '"+txt_cash.getText()+"', '"+txt_balance.getText()+"', '"+Date.getText()+"', '"+Time.getText()+"')");

    DB.save("update stock set quantity=quantity-'"+quantity+"' where Product_ID ='"+pid+"'");
    }

    } catch (Exception e) {
    e.printStackTrace();
    }
    JOptionPane.showMessageDialog(rootPane, "INVOICE HAS SAVED!");
    dtm.setRowCount(0);
    clearText();
    Customer_Name.setText("");
    txt_CustomerID.setText("");
    }