Java Code to Connect to MySQL Datatabase Perform Insert and Delete Operation

package mypack;

import java.sql.*;

import java.io.*;

 

public class db

{

Statement smnt;

 Connection con;

 public db()

  {

    

                try

                {

         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

         con=DriverManager.getConnection("jdbc:odbc:data","root", "1111");

                                               

                }

 

                catch(Exception e)

                {

                System.out.println("Exception"+e);

                }

  }

 

   public void insertname(String a)

  {

   System.out.println("called");

   try

                {

                 smnt=con.createStatement();

     smnt.executeUpdate("INSERT INTO name (name) VALUES('" + a + "')" );

                  

    }

    catch(Exception e)

                {

                  System.out.println(e);

                }

  }

  public void deletename(String a)

  {

  try

  {

 

                 smnt=con.createStatement();

     smnt.executeUpdate("delete from name where name like '"+ a +"'");

  }

  catch(Exception e)

  {

                System.out.println(e);

  }

  }

 

}

//main class (which use the above class

 

import mypack.db;

 

class sachins

{

public static void main(String argu[])

 {

   db sachin = new db();

               

   sachin.insertname("great");

   sachin.deletename("amazing");            

  

 }

                                 

}

 

Database used:

Mysql

Connector ODBC connector

Table name = name

Column name(single column table) = name (same as table name)

 

Happy Coding

Love Marshall

 

Comments

Popular posts from this blog

I feel shame for Facebook Groups like "Growing INDIA, Growing Ourselves & making INDIA a better place to live in". They are capitalizing the Delhi Rape Case. Read how dirty is the Indian Politics where They ignore a Rape in Gujrat under Modi rule and Blame ladies like Sheila Dixing and Sonia for rapes. Stay Alert my Friends

Unwanted spam calls from +91-731-xxx-xxx