tutorial netbeans-insert data from input value into mysql database(vol.11)






Hello guys !!
What’s up
Welcome back to masunduh2,last post,I’ve shared tutorial about about how to display dynamic time.now I gonna show you how to insert from input jtextfield into mysql database with netbeans java swing part 11.

How do that ??
Ok lets get started.

Last post, I has been shared articles how to create database and tables in phpmyadmin right??
If you don’t know how to installed xampp,create database,create tables, and attributes, you can search or read last post in my blog or open sitemap in my blog.


recommended : your pc has been installed xampp software, or mysql,and
database,tables,attributes name must be same on my all article from vol.1 until now.
Please visit,read and follow my all tutorials right now.

If you do that, you can follow on step by step for this tutorials.

Ok next,If you have create databases,and tables recruitments.

Open now your netbeans IDE software.
Then, create new project,file > new project or press Ctrl+shift+N. and click finish.

Create Java Class In your Java source package,
right click > New > Java Class. > rename to “connectDB” > and click OK.



Open your java class (connectDB.java)
Next, Type or copy all syntax below for create connection to mysql database with netbeans
 
/*
 * www.masunduh2.blogspot.com
 * subscribe Masunduh2 on YouTube Channel
 */
package jcombodata;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
 *
 * @author Administrator
 */
public class connectDB {
    
    Connection conn=null;
    
    public static Connection connDB()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/alldata","root","");
            //alldata > database name
            // root > username mysql
            // " " > password mysql
            
            return conn;
        } catch (ClassNotFoundException | SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        return null;
    }
}



After that, next step
create new Jframe Form In your Java source package,
right click > New > Jframe Form. > rename to “table_form” > and click OK.


Drag and drop palette component what do you want for insert data programs into mysql database
Like : 1 jbutton,jtextfield component.
ex : to insert data use jtextfield, : id,username,password,age,email, and others. and jbutton for action perform or execute save data into mysql database .

then, in table_form.java, go to source tab.
In source tab table_form.java

Type or copy all syntax below into public class table_form extends javax.swing.JFrame {}

 
Connection connect = null;
ResultSet rst=null;
PreparedStatement pst=null;

like this :
public class table_form extends javax.swing.JFrame {
Connection connect = null;
ResultSet rst=null;
PreparedStatement pst=null;

And type syntax below in public table_form(){}
connect = connectDB.connDB();

like this :
public table_form() {
        initComponents();
        connect = connectDB.connDB();       
    }


next,go to design tab,and  right click jbutton for execute or insert data performe.
Events > action > actionPerformed.

in jbuttonActionPerformed block
Type or copy all syntax below :
 
try {
String sql = "insert into dataop (id,username,password,age,email) values(?,?,?,?,?)";  
// dataop > table name in your database
// id,username,password,age,email > field name in your tables.
pst = connect.prepareStatement(sql);
pst.setString(1, id_text.getText());
pst.setString(2, user_text.getText());
pst.setString(3, pass_text.getText());
pst.setString(4, age_text.getText());
pst.setString(5, email_text.getText());
// id_text,user_text,pass_text,age_text,email_text > jtextfields name


pst.execute();

JOptionPane.showMessageDialog(null, "Success !");
        } catch (SQLException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, e);
        }



Now, if no error in your java swing project you can run that programs

Click Run > choose Clean and build project.
Then, click run again > and select Run File.

Like image below

Screenshot

Watch video tutorial how to insert from input jtextfield into mysql database with netbeans java swing part 11.


Please write comment,share it, and subscribed !
Don’t forget join on my fanspage now,ok  guys !

See you the next awesome and simple tutorials. Bye !

 

2 Responses to "tutorial netbeans-insert data from input value into mysql database(vol.11)"

Ads Inside Post