Database Connectivity with Java ?

For those who already know a database connectivity in Java, this post will be boring. But, for people who're yet to know, this will be resourceful and simple to understand.

I use Netbeans as my IDE, So that, I just have to think about, all important coding part.

1 . First, take a new project in Netbeans & create a new Java Frame.


2.  Go to Pallet & Take a button from Swing Controls.



3.  Then, create an event for the Button & write the following code in the event.




 
try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdbname","username","password");
            JOptionPane.showMessageDialog(null,"Succesfully Connected !");
            
        } catch (Exception e) {
            System.out.println(e);
        } 
      

In this example, if your connection is successful a java swing 'JOptionPane' message dialog will be shown. 

But, connecting to a database inside a click event (private method) is unacceptable.Because , the database connectivity will be required in multiple occasions in a program. So, we can create a separate class for this & call it, whenever we need it, using object oriented concepts.

Here's a class for database connectivity.



import java.sql.Connection;
import java.sql.DriverManager;

public class Connect{

 public Connection connect(){

            Connection con = null;
  try
  {
   Class.forName("com.mysql.jdbc.Driver");
   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdbname","username","password");
  } catch(Exception ex){
   ex.printStackTrace();
  }
  return con;
 }

 public void conClose(Connection c){

  try{
   c.close();
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }
}

Here's an example of dynamically calling the connect() & conClose() methods of the Connect class that we've finished writing.

Before the java code make sure you execute following MYSQL script to create database & table.

create database yourdbname;

use yourdbname;

create table test_table(
 name varchar(50) not null
);

insert into test_table values('Harsha Wansooriya');

You have to add a swing text field 'jtxt_test' to your Frame. Then add the following method to the Frame Class & call inside the constructor so that it'd execute.

    public void load_name(){

        Connection con = null;

        try {
            con = new Connect().connect();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT name from yourdbname.test_table WHERE name LIKE 'Harsha Wansooriya'");

            while(rs.next())
            {
                String name = rs.getString(1);
                jtxt_test.setText(name);
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Enjoy ! Create Things & Test !

3 comments:

Anonymous said...

Many Thanks for this !

Anonymous said...

Not that much information but thanks for the blog post bro!

Anonymous said...

note that much information but a good post thanks for this!