Home Java Batch Processing Java JDBC Gui

Batch Processing Java JDBC Gui

2 min read
0
0
650

This tutorial will teach you how to make a Batch Processing Java JDBC Gui application.

Establish the database Connection

   Connection con;
    PreparedStatement pst;
 
    ResultSet rs;
    public void Connect()
    {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/vmproducts","root","");
        }
        catch (ClassNotFoundException ex) 
        {
         
        } 
        catch (SQLException ex) {
           
        }
    }

Select the txtaccountno textfield right click >Key->Event->KeyReleased past the code inside the KeyReleased  event

private void txtpidKeyReleased(java.awt.event.KeyEvent evt) {                                   
        
           String pcode = txtpid.getText();
        try {
            pst = con.prepareStatement("select pname,price from product where id = ?");
             pst.setString(1, pcode);
            rs = pst.executeQuery();

            if(rs.next() == true)
            {
     
                String pname = rs.getString(1);
               String price = rs.getString(2);
                txtpname.setText(pname);
                txtprice.setText(price);
            }
            else
            {
                txtpname.setText("");
                txtprice.setText("");
            }
        
        } catch (SQLException ex) {
            Logger.getLogger(vmproduct.class.getName()).log(Level.SEVERE, null, ex);
        }
           
        
    }                                  

Save Records

past the code inside the Add button

try {
            con.setAutoCommit(false);
            String pcode = txtpid.getText();
            String pname = txtpname.getText();
            String price = txtprice.getText();
            String qty = txtqty.getText();
          
           Statement st1=con.createStatement( );
           
           String SQL1="insert into sales(pname,price,qty)values('" + pname  + "','" + price  + "','" + qty  + "')"; 
           String SQL2="update product set qty=qty- '" + qty  + "'  where id ='" + pcode  + "'";
         
 
           st1.addBatch(SQL1);
           st1.addBatch(SQL2);
                 
         
		int[ ] status= st1.executeBatch( );

		for(int i=0;i<status.length;i++)
		{
		   System.out.println( status[ i ] );
		 }
            con.commit();

            
        } catch (SQLException ex) {
          
        }

 

  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
  • Is Java Hard to Learn?

    Is Java Hard to Learn is it True? Java is one of the most popular programming languages in…
  • How to generate random with weight java

    Generating Random Numbers with Weights in Java Random number generation with weighted prob…
Load More Related Articles
  • Conditional Statements in Python

    Conditional statements in Python allow us to control the flow of execution based on condit…
  • Java Beans

    A Java Bean is a reusable software component that follows a specific set of conventions. J…
  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
Load More By admin
  • Java Beans

    A Java Bean is a reusable software component that follows a specific set of conventions. J…
  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
  • Java Developer Jobs

    Java remains one of the most in-demand programming languages worldwide, powering everythin…
Load More In Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Conditional Statements in Python

Conditional statements in Python allow us to control the flow of execution based on condit…