You are on page 1of 2

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication50;
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
/**
*
* @author PADMARAMANA
*/
public class JavaApplication50 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException,ClassNotFoundExce
ption, IOException {
Class.forName("com.mysql.jdbc.Driver");
retrieve();
insert(21,"As");
retrieve();
insloop();
delete();
}
static void retrieve(){
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:330
6/Sample","root","Adi89Ani96");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from student");
while(rs.next()){
System.out.print(rs.getInt("id")+" "+rs.getString("name"));
System.out.println(" ");
}
con.close();
}
catch(SQLException e){
System.out.println(e);
}
}
static void insert(int sid, String sname) throws SQLException{
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:330
6/Sample","root","Adi89Ani96");
Statement st = con.createStatement();
int res = st.executeUpdate("insert into student(id,name) values('"+sid+"
','"+sname+"')");
if(res == 1){
System.out.println("SUCCESSFUL");
}
else{
System.out.println("FAIL");
}
con.close();
}

catch(SQLException s){
System.out.println(s);
}
}
static void insloop() throws SQLException, IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
;
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:330
6/Sample","root","Adi89Ani96");
PreparedStatement n = con.prepareStatement("insert into student values(?
,?)");
do{
System.out.println("ENTER ID");
int pid = Integer.parseInt(br.readLine());
System.out.println("ENTER NAME");
String pname = br.readLine();
n.setInt(1, pid);
n.setString(2,pname);
int i = n.executeUpdate();
System.out.println("DO YOU WANT TO CONTINUE??");
String ch = br.readLine();
if(ch.startsWith("n") || ch.startsWith("N")){
break;
}
}while(true);
con.close();
}
static void delete() throws SQLException, IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
;
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:330
6/Sample","root","Adi89Ani96");
PreparedStatement n = con.prepareStatement("delete from student where id
= ?");
do{
try{
System.out.println("Enter ID");
int pid = Integer.parseInt(br.readLine());
n.setInt(1,pid);
int p = n.executeUpdate();
System.out.println("DELETION DONE");
}
catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("DO YOU WANT TO CONTINUE??");
String ch = br.readLine();
if(ch.startsWith("n") || ch.startsWith("N")){
break;
}
}while(true);
con.close();
}
}

You might also like