Monday, February 22, 2016

Java Program Connect To MySql Database Using Command Prompt

************** Java Program Connect To MySql Database Using Command Prompt************


Step 1 : Register the Driver class using forName() Method.

              Class.forName("com.mysql.jdbc.Driver");

Step 2: Create Connection Object .

             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:port Number/Database Name", "Username", "Password");

Step 3: Create Statement Object.

           Statement stmt = conn.createStatement();

Step 4: Execute the Query and store result in ResultSet Object.

            ResultSet rs = stmt.executeQuery("select * from tablename");

Step 5: Display Result From ResultSet Object.

          while(rs.next())
         {
                   System.out.println(rs.getInt(1)); + "   " + rs.getString(2));
          }

Step 6: Close The ResultSet, Statement and Connection Object.

            rs.close();
            stmt.close();
            conn.close();

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Step 7: To Type Your Java Database Connectivity program in Notepad or Notepad++  editor.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name : DatabaseConnection.java

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.sql.*;

class DatabaseConnection
{
             

               public static void main(String argv[]) throws Exception
               {
                     
                      Class.forName("com.mysql.jdbc.Driver");
     
                      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sunil", "root", "123456");

                     if(conn == null)
                     {
                             System.out.println("\n Connection is Denied \n");
                       }
                   
                     Statement stmt = conn.createStatement();

                     ResultSet rs = stmt.executeQuery("select * from emp");
                 
                     while(rs.next())
                     {
System.out.println(rs.getInt(1));
      }
       rs.close();
      stmt.close();
    conn.close();
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Step 8: Compile that program using javac command 

           javac DatabaseConnection.Java

Step 9: Run That Program using java Command.

        java DatebaseConnection

We have occur ClassNotFoundException at Runtime. We have solve that exception using following steps.

1. Go to your browser and download mysql-connector-java-5.1.14-bin.jar file.

2. Copy that file and paste that file into C:\Program Files\Java\jdk1.7.0_10\jre\lib\ext folder.

3. Restart command prompt and execute again that program. We have to successfully connect java program to mysql database.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Java Servlet Program is comilation error then we have to download servlet-api.jar file and copy paste that file into C:\Program Files\Java\jdk1.7.0_10\jre\lib\ext folder. Recompile that program.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...