Java Programming Practical 28

Question 1
Write a program for Implementing Insert Statement.
InsertDemo.java
Java
				import java.sql.*;
public class InsertDemo
{
	
	
	public static void main(String[] args)
		{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Driver loaded");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
			System.out.println("Connection created");
			Statement stmt = con.createStatement();
			String sql = "insert into student values(4343,'PQRS','CO')";
			stmt.executeUpdate(sql);
			System.out.println("Record inserted");
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}
			
Output
Question 2
Write a program for Implementing Update Statement.
UpdateDemo.java
Java
				import java.sql.*;
public class UpdateDemo
{
	
	
	public static void main(String[] args)
		{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Driver loaded");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
			System.out.println("Connection created");
			Statement stmt = con.createStatement();
			String sql = "update student set branch='CO' where roll_no=1212";
			stmt.executeUpdate(sql);
			System.out.println("Record updated");
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}
			
Output
Question 3
Write a program for Implementing Delete Statement.
DeleteDemo.java
Java
				import java.sql.*;
public class DeleteDemo
{
	
	
	public static void main(String[] args)
		{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Driver loaded");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
			System.out.println("Connection created");
			Statement stmt = con.createStatement();
			String sql = "delete from student where roll_no=5656";
			stmt.executeUpdate(sql);
			System.out.println("Record deleted");
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
	}
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *