0% found this document useful (0 votes)
8 views3 pages

Prepare ST

The Java program connects to a MySQL database to perform CRUD operations on an 'animal' table. Users can insert, update, delete, display, and search for records through a command-line interface. The program utilizes prepared statements for database interactions and outputs results to a text file.

Uploaded by

meprojects.gg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Prepare ST

The Java program connects to a MySQL database to perform CRUD operations on an 'animal' table. Users can insert, update, delete, display, and search for records through a command-line interface. The program utilizes prepared statements for database interactions and outputs results to a text file.

Uploaded by

meprojects.gg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.sql.

*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Driver;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.*;

public class prepStatementEx


{
public static void main(String[] args){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/javajdbctest","root","");
// Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int choice;
if(!con.isClosed()){
do{
System.out.println("\t1) Insert records");
System.out.println("\t2) Update records");
System.out.println("\t3) Delete records");
System.out.println("\t4) Display records");
System.out.println("\t5) Search Record");
System.out.println("\t6) Exit");

System.out.println("Select operation :");


choice = Integer.parseInt(br.readLine());
PreparedStatement ps;
switch(choice){
case 1:
System.out.println("Enter animal name:");
String name = br.readLine();
System.out.println("Enter category:");
String catego = br.readLine();

ps = con.prepareStatement("INSERT INTO animal(aname,


category)VALUES(?,?)");
ps.setString(1,name);
ps.setString(2,catego);
int insStatus = ps.executeUpdate();
System.out.println(insStatus + " row(s) inserted.");

break;
case 2:
System.out.println("Enter id:");
int id = Integer.parseInt(br.readLine());
System.out.println("Enter animal name:");
String newname = br.readLine();
System.out.println("Enter category:");
String newcatego = br.readLine();

ps = con.prepareStatement("UPDATE animal SET aname=?,


category=? WHERE aid=?");
ps.setString(1,newname);
ps.setString(2,newcatego);
ps.setInt(3,id);
int updStatus = ps.executeUpdate();
System.out.println(updStatus + " row(s) updated.");
break;
case 3:
System.out.println("Enter id for delete:");
int delId = Integer.parseInt(br.readLine());

ps = con.prepareStatement("DELETE FROM animal WHERE aid


= ?");
ps.setInt(1,delId);
int delStatus = ps.executeUpdate();
System.out.println(delStatus + " row(s) deleted");
break;
case 4:

Statement st = con.createStatement();
st.executeQuery("SELECT * FROM animal");
ResultSet rs = st.getResultSet();
System.out.println("ID \t\t Name \t\t Category");
FileOutputStream fw = new
FileOutputStream("./Files/output.txt");
fw.write(("ID \t\t Name \t\t Category").getBytes());
while(rs.next()){
int retId = rs.getInt("aid");
String retName = rs.getString("aname");
String retCate = rs.getString("category");
System.out.println(retId + "\t\t" + retName +"\t\
t"+ retCate);
String outputString = "\n"+retId + "\t\t" + retName
+"\t\t"+ retCate;
fw.write(outputString.getBytes());
}
System.out.println("Output from file");
FileReader rd = new FileReader("./Files/output.txt");
int d;
while((d = rd.read()) != -1){
System.out.print((char)d);
}
break;
case 6:
System.out.println("Exiting...");
break;
case 5:
System.out.println("Enter Name of the animal you wanna
search:");
String sbn = br.readLine();
ps = con.prepareStatement("SELECT * FROM animal WHERE
aname=?");
ps.setString(1,sbn);
ps.executeQuery();
ResultSet result = ps.getResultSet();
System.out.println("ID \t\t Name \t\t Category");
while(result.next()){
int retId = result.getInt("aid");
String retName = result.getString("aname");
String retCate = result.getString("category");
System.out.println(retId + "\t\t" + retName +"\t\
t"+ retCate);
}
break;
default:
System.out.println("Invalid operation !");break;
}
}while(choice<6);
}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
if(con!=null){
con.close();
System.out.println("Exited from code Successfully.");
}

}
catch(SQLException e){
e.printStackTrace();
}
}
}
}

You might also like