Connecting a java application with mysql database.

Share:
Connecting a Java Application With MySql DataBase
Hello Readers, In this arcticle i have shown how to connect a java application with MYSQL Database Table and fetch data from that table?
  • First of all you need to download the platform independent mysql connector jar file.
  • After downloading jar file,create a project in Eclipse or in your favorite IDE with main method.
-Now right click on project
1)now click on configure build path,
2)click on Add External Jars,
3)Now find your downloaded mysql connector jar file and click on that and say apply and close.

-Now the Referenced Libraries Directory will be created, in that jar files are present that you added in last step.

-Now in your class main method or create your own local method  perform the following steps.

1)Register the driver
Class.forName("com.mysql.jdbc.Driver");

2)Create the Connection
String url,username,password;
url="jdbc:mysql://localhost:3306/database_name";
username="root";//if it is a localhost
password="";//if it localhost then keep it blank.
Connection  conn=DriverManager.getConnection(url,username,password);

-That's all for creating connection   
-Now you can perform execution of queries according to your requirement.
-Performing SQL SELECT Database Operation In Java Application.

file name : MyClass.java

public class MyClass{
    public static void main(String args[])
try {

String query="select DISTINCT(ClientName) from clientdetail"
PreparedStatement st=conn.prepareStatement(query);
ResultSet rs = st.executeQuery();

   while(rs.next()){
   String cname=rs.getString("ClientName");
   //it depend on your requireme
   System.out.println(cname);
   }
   rs.close();
   st.close();
   conn.close();
   }
   catch(Exception e){
     System.out.println(e.getMessage());
          }
       }
     }

Full Example
Class Name MyClass.java
           import java.sql.*;
    public class MyClass
     {
    public static void main(String args[])throws ClassNotFoundException
           {
           //Registering the driver
           Class.forName("com.mysql.jdbc.Driver");
           
           try {
           String url,username,password;
           url="jdbc:mysql://localhost:3306/mysql@123";

           //note:mysql@123 is a database name.
           username="root";//if it is a localhost
           password="";//if it localhost then keep it blank.

   Connection  conn=DriverManager.getConnection(url,username,password);
   String query="select DISTINCT(ClientName) from clientdetail";
   PreparedStatement st=conn.prepareStatement(query);
   ResultSet rs =st.executeQuery();

           while(rs.next())
           {
           String cname=rs.getString("ClientName");
           //it depend on your requirements
           System.out.println(cname);

           }
              rs.close();
              st.close();
              conn.close();

             }
              catch(Exception e)
               {
               System.out.println(e.getMessage());
                }
              }
           } 
Here database name is mysql@123 and clientdetail is my Table name with ClientName Column, and using SELECT DISTINCT Statement to get Unique ClientName from the table.
Hope It Will Help You.
Thanks

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database