Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SATTVA INFOTECH

JAVA TRAINING
JDBC CONNECTION PROCEDURE

FOLDER STRUCTURE:

SATTVA INFOTECH TEAM 1


MENU:
package app;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) throws SQLException, IOException {
int usrType = 0;
Scanner sc = new Scanner(System.in);
String defaultStmt = "Please Enter Valid Choice....";
String inputChoice = "Enter Your Choice :";
String tabSpace = "===============================================";

while(usrType !=9) {
System.out.println("=========College
Application===========\n");
System.out.println("\t Choose Menu ");
System.out.println("\t 1. Add Student");
System.out.println("\t 2. View Student ");
System.out.println("\t 3. Update Student ");
System.out.println("\t 4. Delete Student ");
System.out.println("\t 5. Add Image ");
System.out.println("\t 6. View Image ");
System.out.println("\t 7. Add File ");
System.out.println("\t 8. View File ");
System.out.println("\t 9. Exit");
System.out.println(tabSpace);
System.out.println(inputChoice);
usrType = sc.nextInt();
switch(usrType) {
case 1:
System.out.println("Add Student");
Add_Student adst = new Add_Student();
adst.AddStu();

break;
case 2:
System.out.println("View Data");
View_Student vst = new View_Student();
vst.viewStu();
break;
case 3:
System.out.println("Update Data");
Update_Student uds = new Update_Student();
uds.UpdateStu();
break;
case 4:
System.out.println("Delete Data");
Delete_Student ds = new Delete_Student();
ds.delete();
break;
case 5:
System.out.println("Add Image");

break;
SATTVA INFOTECH TEAM 2
case 6:
System.out.println("View Image");

break;
case 7:
System.out.println("Add File");

break;
case 8:
System.out.println("View File");

break;
default:
System.out.println("Thank you for using Application");
}

}
sc.close();

}
}

ADD STUDENT:
package app;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;
// class name
class Add_Student {
// method name
public void AddStu() throws SQLException, IOException {
String cs = "jdbc:mysql://localhost:3306/college";
String query = "insert into student(regno, sname, marks)"
+"values(?,?,?)";
try(
// step 1
Connection conn =
DriverManager.getConnection(cs,"root","Sunil$6246");
// step 2
PreparedStatement pstmt = conn.prepareStatement(query);
){
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
int count = 0;
boolean flag = true;

String regNo;
String sname;
String marks;

String ch;

while(flag) {
// take the input from command prompt
System.out.println("Enter Reg No : ");
SATTVA INFOTECH TEAM 3
regNo = reader.readLine();
System.out.println("Enter Student Name :");
sname = reader.readLine();
System.out.println("Enter Student Marks ");
marks = reader.readLine();
// inserting the values to db
pstmt.setString(1, regNo);
pstmt.setString(2, sname);
pstmt.setNString(3, marks);
//step 3
count +=pstmt.executeUpdate();
System.out.println ("Do you want to continue
Y/N");
ch = reader.readLine();
if(ch.toUpperCase().charAt(0) == 'N')
flag = false;
}
System.out.println (count+" Records Inserted");
//step 4
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}

}
}

VIEW STUDENT:
package app;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class View_Student {

public void viewStu() throws SQLException {


// step 1:
//Class.forName("oracle.jdbc.driver.OracleDriver");
String cs = "jdbc:mysql://localhost:3306/college";
try(
// step 1
Connection conn =
DriverManager.getConnection
(cs,"root","Sunil$6246");

// step 2
Statement stmt = conn.createStatement();
) {
// step 3
ResultSet rs=stmt.executeQuery
("select * from student");
System.out.println("Reg No \t Name \t\t Marks ");
while(rs.next()) {
SATTVA INFOTECH TEAM 4
System.out.println(rs.getInt(1)+" \t
"+rs.getString(2)+" \t "+rs.getInt(3));

}
// step 4
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}

}
}

UPDATE STUDENT:
package app;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Update_Student {
public void UpdateStu() throws SQLException {
Scanner sc = null;
String cs = "jdbc:mysql://localhost:3306/college";
String query = "UPDATE student "
+ "SET marks = ? "
+ "WHERE regno = ?";
try(// step 1
Connection conn =
DriverManager.getConnection(cs,"root","Sunil$6246");
// step2
PreparedStatement pstmt = conn.prepareStatement(query);) {
sc = new Scanner(System.in);
// take input from command prompt
System.out.println("Enter Marks: ");
int Marks = sc.nextInt();
System.out.println("Enter Regno");
int regno = sc.nextInt();
// insert values to db
pstmt.setInt(1, Marks);
pstmt.setInt(2, regno);
// step 3
int del = pstmt.executeUpdate();
System.out.println("Number of records updated : " + del);
// step 4
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}

DELETE STUDENT:

SATTVA INFOTECH TEAM 5


package app;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Delete_Student {
public void delete() throws SQLException {
Scanner sc = null;
String cs = "jdbc:mysql://localhost:3306/college";
String query = "DELETE FROM student where regno = ?";
try(
//step1
Connection conn =
DriverManager.getConnection(cs,"root","Sunil$6246");
// step2
PreparedStatement pstmt = conn.prepareStatement(query);) {

// take input from command prompt


sc = new Scanner(System.in);
System.out.println("Enter Regno to delete the Student!!!!");
int delbyId = sc.nextInt();
// inserting or check value of db
pstmt.setInt(1, delbyId);
// step3
int del = pstmt.executeUpdate();
System.out.println("Number of deleted records: " + del);
// step 4
conn.close();
}catch (SQLException e) {
e.printStackTrace();

}
}

SATTVA INFOTECH TEAM 6

You might also like