Saturday, November 22, 2014

How to data insert in mysql using swing


Data Insert into MySQL database from Java Desktop Application
This tutorial can be taught, how to data insert into the MySQL database using the database connection.
Content:
1. Create MySQL database and table
2.Add Jar file
2. Create Information entry Form
3. Create Information entry pojo
4. Create Connection function
5. Create Insert function
4. Run Project

Pre-requirement: NetBeans 8.x and MySQL Installed your Computer.

The first thing we'll need is an example MySQL database table. To keep it simple, but to also show several different MySQL data types, following MySQL example database and  table:
CREATE DATABASE  :
CREATE DATABASE test_syc  CHARACTER SET utf8 COLLATE utf8_unicode_ci;

CREATE Table:
CREATE TABLE `customer_info` (
  `customer_id` int(11) NOT NULL,
  `customer_name` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
  `cutomer_mobile` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`customer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Add Jar file:
1. Go to project properties by right clicking on project.

2. Then click on Libraries tab, You will see Compile, Run, Compile Tests, Run Tests tabs.

3. Click on Compile tab

4. Click on Add JAR/Folder

5. Then browse and select the jar files or folder which you want to include. Included jar files or libraries will show on  the following box of Compile tab.
6. Click on OK button.
7. Finished.
2. Create Information entry Form
Create Sample Application Form using Swing GUI




Step 3: Create POJO class.
a.    Right click in a java package and select “New”, then “Java Class” and show this windows.


b.    Put the class name as CustomePOJO and Package as (sample.desktop.project.form). Click Finish and show this windows.

c.     CustomePOJO.java:

package semple.desktop.project.from;
/**
*
* @author Samsul
*/
public class CustomerPOJO {
private int custID ;
private String custAddress;
private String custName;
/**
 * @return the custID
 */
public int getCustID() {
    return custID;
}
/**
 * @param custID the custID to set
 */
public void setCustID(int custID) {
    this.custID = custID;
}
/**
 * @return the custAddress
 */
public String getCustAddress() {
    return custAddress;
}
/**
 * @param custAddress the custAddress to set
 */
public void setCustAddress(String custAddress) {
    this.custAddress = custAddress;
}
/**
 * @return the custName
 */
public String getCustName() {
    return custName;
}
/**
 * @param custName the custName to set
 */
public void setCustName(String custName) {
    this.custName = custName;
 }
}


4. Create Connection function
Create DbConnect.java class like as Step-3 .
Write in flowing Code:
public class DbConnect {
        public static Connection establishConnection() throws ClassNotFoundException, SQLException {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String oracleURL = "jdbc:mysql://localhost:3306/test_syc";
            connection = (Connection) DriverManager.getConnection(oracleURL, "root", "root");
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
        return connection;
    }
}

Step 5: Double click the Save button to open it’s ActionPerformed:


Paste the following code:-
  
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: CustomerPOJO cu = new CustomerPOJO(); cu.setCustID(Integer.parseInt(customet_id.getText())); cu.setCustName(customer_name.getText()); cu.setCustAddress(customer_mobile.getText()); String insert = create(cu); //custom title, no icon JOptionPane.showMessageDialog(panel, insert, "Data Insert", JOptionPane.PLAIN_MESSAGE); customet_id.setText(""); customer_name.setText(""); customer_mobile.setText(""); }
  
 

Now create insert function in “CustomeEntry.java” class. Flowing code:
   

 public String create(CustomerPOJO cu) {
        DbConnect db = new DbConnect();
        Connection conn;
        try {
            conn = (Connection) db.establishConnection();
            // the mysql insert statement
            String query = " insert into customer_info (customer_id,customer_name, cutomer_mobile)"
                    + " values (?, ?, ?)";
            // create the mysql insert preparedstatement
            PreparedStatement preparedStmt = conn.prepareStatement(query);
            preparedStmt.setInt(1, cu.getCustID());
            preparedStmt.setString(2, cu.getCustName());
            preparedStmt.setString(3, cu.getCustAddress());
            // execute the preparedstatement
            preparedStmt.execute();
            conn.close();
            return "Successfully Data Insert";
        } catch (Exception ex) {
            System.err.println("Got an exception!");
            System.err.println(ex.getMessage());
            return " Data Insert Fail";
        }
    }

3. Run Project:

Click on () or Right Click on your project and select Run

Show this windows





Fil-up for and click on save button
Show this massage 



Download

0 comments :

Post a Comment

 

Copyright @ 2014 Tech Tutorial .