HOW TO CONNECT A JAVA APPLICATION WITH MYSQL DATABASE

Data Base Connectivity with Java Applications


So in this post I will explain how to connect Database with a java application.That is, I will create a java application that contains to GUIs,one is login and other is Signup.And also create a database.This simple application Allows a user to create a account i e signup  and the user can login with his/her username and password.(For creating java application i uses Netbeans and for Database I uses the WAMP server).


Step 1:


Open Neatbeans
File > New Project > Java  > Java Application  (Give the project name and press Finish)

Step 2 :

Expand  the project situated in the Project Explorer window . So you can  see "Source Packages",

Right click on source Packages > New > Java package  (Give your package name and press Finish)

Step 3 :

similarly create a JFrame in that package.For that,

Right click on Packages > New > jFrame form(GIve your jframe name and press Finish name and press Finish)

Step 4:

So After doing that you want to create a simple signup GUI. For doing that you can design your own GUI . In Netbeans we need not to worry about codes because you can create a GUI by drag and drop the neccessary items from the palette.

step 5:

Repeat the step 4 and create your own login page.


Step 6 :

So after you doing the above step you created a  user interface. But for communicate with the database we want to add additional things to it.

So we created a GUI to interact with the user. Then we need a database for creating database connection.

Open your browser > Enter "localhost" 
Then you can see WAMP server page(because I uses WAMP server). from that you need to Select   "phpmyadmin"

In your left hand side you can see the database list .In that press "New"
Give your database name and press create button. Then your database has been created.
Then  you can see the database  in the database list in your left hand side. Then click on that ,After doing that you can see the create table window,Give your  table name and number of required columns  then press  "GO" button.Then configure each columns.

Step 7:


So for the database connectivity you need to create a java class .first you create a Package as we created in step 2 and create a java class by doing,
Right click on Packages > New > Java class
(Give your class name and press Finish name and press Finish)


Step 8 :

In this Step we want to write some code for connectivity
before doing that we want to download a mysql connector for doing that just click on the link below


After downloading that you need to extract that .Then come back to Netbeans 
Right click on  Libraries > Add jar/folder >  Then a dialog box appears and then select the Only the Jar file in the mysql Connector.(Do not add  the entire folder to it)


Step 9:


Then you need to code .Write the code given below to your java class show below

package connectivity;   //connectivity is the package name
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Conne        //Conne is the java class name in the connectivity package
{
private Connection connection = null;
public Connection getconnection()
{
return this.connection;
}
public Conne() throws SQLException
{
String connectionURL ="jdbc:mysql://localhost:3306/Fest_Manager"; // fest manger is the database name that you want to connect
try 
{
Class.forName("com.mysql.jdbc.Driver");
connection =(Connection)DriverManager.getConnection(connectionURL,"root",""); //root is the user name  and no password is indecated by " "

System.out.println("Connected");
}catch(ClassNotFoundException | SQLException e)
{
System.out.println("FAILED");
}
}
public static void main(String arg[]) throws Exception
{
Conne obj = new Conne();
}
}
when you run this file then , you see Connected in the console which means the connection is Successful. If you see FAILED then some error in the database Connection.

Step 10:

So you are successfully created  
GUI 
DATABASE
DATABASE CONNECTION CLASS

Then we want to store the signup information when the user press the button SIGNUP.
For doing that Double click on the SignUP button then you will go to the mouse click event of the button and write the following code inside the button event.


        String  username1 = username.getText(); // name is the name of the text field 
        String password1 =password.getText(); // password is the name of the password 
//above two codes  are used to get the value given by the user
        try 
{
                Conne con = new Conne(); //Conne is the connection java class. con is object
                Connection connection = con.getconnection();
               
               
                    String ques="INSERT INTO test(Username,password)values(?,?)"; 
/* In this test is the table name and Username and password are the column names 
this is for insertion of the data to the database */
                
                    PreparedStatement pstatement = null;
                    pstatement =connection.prepareStatement(ques);
                    pstatement.setString(1,username1);
                    pstatement.setString(2,password1); 
                    pstatement.execute();
                
                    infoBox("Your Account is Created Successfuly","MESSAGE");
//infoBox is a method that is used to display the message;
               
        }catch(Exception e)
{
System.out.println(e);
                infoBox("Your Account is not created \n Try Again","ERROR!");
}

For displaying dialog box you must need to give the below code after the closing bracket of the button event


//The below code is the method definition of infoBox
 public static void infoBox(String infoMessage, String titleBar)
    {
        JOptionPane.showMessageDialog(null, infoMessage,titleBar,         JOptionPane.INFORMATION_MESSAGE);
    } 

I fogot say some thing for doing this we need some necessary imports they are,
import connectivity.*;  // this is the package that we create the Database connection java class situated 
import java.lang.*;
import java.sql.*;
import javax.swing.JOptionPane;

So after doing that you can see that , when you run the signup page and give the information and press sign up that values will be in the  database table 


Step 11:

so the final process is user verification using login.Similarly double click on the login button then the event will be created and you first give the above imports and write the code
 
        // to main menu
     String   username1 = username.getText(); 
     String    password1 = password.getText();
        try 
{
            Conne con = new Conne();
            Connection connection = con.getconnection();
            
String ques="SELECT * FROM test where User_Name='"+username1+"'";
                Statement st =connection.createStatement();
                ResultSet rt =st.executeQuery(ques);
               if(rt.next())
               {
                  one =rt.getString("User_Name");
                  two =rt.getString("Password");
               }
            if((username1.equals(one)) && (password1.equals(two)))
            {
 // The next page that we want to go when the password and username is correct
                Main_Page main = new Main_Page(); //Main_Page is the class and main is the object

                main.show();
                this.dispose(); 
            }
            else
            {
                infoBox("Wrong Password or User Name ","ERROR");
            }
}catch(Exception e)
{
System.out.println(e);
}
       
    }
also include the JOptionPane method
public static void infoBox(String infoMessage, String titleBar)
    {
        JOptionPane.showMessageDialog(null, infoMessage,titleBar,              JOptionPane.INFORMATION_MESSAGE);
    }  






Comments

Post a Comment

Popular posts from this blog

ALP Programming 8086 using TASM in ubuntu

Android Oreo 8.0 & Picture in Picture