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
Post a Comment