INSERT,DELETE AND COUNT THE RECORDS IN DBMS WITH JAVA
INSERT THE RECORD
import java.io.DataInputStream;
import java.sql.*;
public class NewClass
{
public static void main(String args[])throws Exception
{
DataInputStream din=new DataInputStream(System.in);
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/kbn","kbn","kbn");
Statement st=con.createStatement();
System.out.println("enter student number");
int no=Integer.parseInt(din.readLine());
System.out.println("enter student name");
String name=din.readLine();
String qry="insert into student values("+no+",'"+name+"')";
int res=st.executeUpdate(qry);
if(res==0)
System.out.println("record not inserted");
else
System.out.println("record inserted");
}
}
COUNT THE RECORDS
import java.io.DataInputStream;
import java.sql.*;
public class NewClass
{
public static void main(String args[])throws Exception
{
DataInputStream din=new DataInputStream(System.in);
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/kbn","kbn","kbn");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select count(*) from student");
if(rs.next())
{
int count=rs.getInt(1);
System.out.println("no of records in the table is :"+count);
}
}
}
DELETE THE RECORDS
import java.io.DataInputStream;
import java.sql.*;
public class NewClass
{
public static void main(String args[])throws Exception
{
DataInputStream din=new DataInputStream(System.in);
System.out.println("enter row to delete");
int no=Integer.parseInt(din.readLine());
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/kbn","kbn","kbn");
Statement st=con.createStatement();
String qry="delete from student where sid="+no;
System.out.println(qry);
int res=st.executeUpdate(qry);
if(res==0)
System.out.println("record not found");
else
System.out.println("record deleted"+res);
}
}
import java.io.DataInputStream;
import java.sql.*;
public class NewClass
{
public static void main(String args[])throws Exception
{
DataInputStream din=new DataInputStream(System.in);
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/kbn","kbn","kbn");
Statement st=con.createStatement();
System.out.println("enter student number");
int no=Integer.parseInt(din.readLine());
System.out.println("enter student name");
String name=din.readLine();
String qry="insert into student values("+no+",'"+name+"')";
int res=st.executeUpdate(qry);
if(res==0)
System.out.println("record not inserted");
else
System.out.println("record inserted");
}
}
COUNT THE RECORDS
import java.io.DataInputStream;
import java.sql.*;
public class NewClass
{
public static void main(String args[])throws Exception
{
DataInputStream din=new DataInputStream(System.in);
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/kbn","kbn","kbn");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select count(*) from student");
if(rs.next())
{
int count=rs.getInt(1);
System.out.println("no of records in the table is :"+count);
}
}
}
DELETE THE RECORDS
import java.io.DataInputStream;
import java.sql.*;
public class NewClass
{
public static void main(String args[])throws Exception
{
DataInputStream din=new DataInputStream(System.in);
System.out.println("enter row to delete");
int no=Integer.parseInt(din.readLine());
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/kbn","kbn","kbn");
Statement st=con.createStatement();
String qry="delete from student where sid="+no;
System.out.println(qry);
int res=st.executeUpdate(qry);
if(res==0)
System.out.println("record not found");
else
System.out.println("record deleted"+res);
}
}
Comments
Post a Comment