0% found this document useful (0 votes)
1 views

program 1

The document contains two Java programs for managing data in a MySQL database: one for books and another for students. Each program allows for inserting, updating, deleting, and selecting records, with the student program also including a calculation of total scores and percentages. Both programs utilize JDBC for database connectivity and user input is handled through the console.

Uploaded by

arpitchristian00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

program 1

The document contains two Java programs for managing data in a MySQL database: one for books and another for students. Each program allows for inserting, updating, deleting, and selecting records, with the student program also including a calculation of total scores and percentages. Both programs utilize JDBC for database connectivity and user input is handled through the console.

Uploaded by

arpitchristian00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

program 1: Book

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Scanner;

class I1

public void insert_data() throws ClassNotFoundException, SQLException

// TODO Auto-generated method stub

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

String sql = "INSERT INTO Book (Book_id, Tittle, Author_name, price )


VALUES(?, ?, ?, ?)";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, "1001");

stmt.setString(2, "mylife");

stmt.setString(3, "bhumi chavda");

stmt.setString(4, "125");

int rowsInserted = stmt.executeUpdate();

if (rowsInserted > 0) {

System.out.println("A new user was insered successfully!");


}

public void update_data() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

String sql = "UPDATE Book SET Tittle=? where Book_id=?";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, "bhumika");

stmt.setString(2, "1001");

int rowsupdateed = stmt.executeUpdate();

if (rowsupdateed > 0) {

System.out.println("A existing was updated successfully!");

public void delete_data() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

String sql = "DELETE FROM Book WHERE Book_id=?";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, "2");

int rowsDeleted = stmt.executeUpdate();


if (rowsDeleted > 0) {

System.out.println("A new user was deleted successfully!");

public void select_data() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("SELECT * FROM Book where Book_id");

while(rs.next())

System.out.println("Book_id= "+rs.getInt(1)+" Tittle = "+rs.getString(2)+"


Author_name ="+rs.getString(3)+" Price="+rs.getString(4));

public class Book {

public static void main(String[] args) throws ClassNotFoundException, SQLException {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

I1 i=new I1();

while(true)

System.out.println("-Operation-");

System.out.println(": 1. Insert :");

System.out.println(": 2. Update :");


System.out.println(": 3. Delete :");

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

System.out.println(": 5. Exit :");

System.out.println("Enter your choice :");

int ch=sc.nextInt();

switch(ch)

case 1:

i.insert_data();

break;

case 2:

i.update_data();

break;

case 3:

i.delete_data();

break;

case 4:

i.select_data();

break;

case 5:

System.exit(0);

break;

default:

System.out.println("Invalid Choice....");

}
}

program : Student

package DB;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Scanner;

class I1

public void insert_data() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

String sql = "INSERT INTO student (std_no, std_name,


std_sub1, std_sub2, std_sub3) VALUES(?, ?, ?, ?, ?)";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, "10");

stmt.setString(2, "MR");

stmt.setString(3, "80");
stmt.setString(4, "60");

stmt.setString(5, "80");

int rowsInserted = stmt.executeUpdate();

if (rowsInserted > 0) {

System.out.println("A new user was insered successfully!");

public void update_data() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

String sql = "UPDATE student SET std_name=?";

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setString(1, "bbbb");

int rowsupdateed = stmt.executeUpdate();

if (rowsupdateed > 0) {

System.out.println("A existing was updated successfully!");

public void delete_data() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

String sql = "DELETE FROM student WHERE std_no=?";

PreparedStatement stmt = con.prepareStatement(sql);


stmt.setString(1, "8");

int rowsDeleted = stmt.executeUpdate();

if (rowsDeleted > 0) {

System.out.println("A new user was deleted successfully!");

public void select_data() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("SELECT * FROM student;");

while(rs.next())

System.out.println("Roll no = "+rs.getInt(1)+" Name =


"+rs.getString(2)+" s1 ="+rs.getInt(3)+" s2 ="+rs.getInt(4)+" s3 ="+rs.getInt(5));

public int calculate() throws ClassNotFoundException, SQLException

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhumi","root","");

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("SELECT * FROM student;");

while(rs.next())

{
int v1 = rs.getInt(3);

int v2 = rs.getInt(4);

int v3 = rs.getInt(5);

int total = v1 + v2 + v3;

float per=total/3;

System.out.println("Roll no = "+rs.getInt(1)+" Name =


"+rs.getString(2)+" s1 ="+rs.getInt(3)+" s2 ="+rs.getInt(4)+" s3 ="+rs.getInt(5)+" Total
= "+total+" per = "+per);

return 0;

public class INSERT {

public static void main(String[] args) throws ClassNotFoundException, SQLException


{

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

I1 i=new I1();

while(true)

System.out.println("-Operation-");

System.out.println(": 1. Insert :");

System.out.println(": 2. Update :");

System.out.println(": 3. Delete :");

System.out.println(": 4. DISPLAY :");

System.out.println(": 5. Exit :");

System.out.println(": 6. Cale :");

System.out.println("Enter your choice :");

int ch=sc.nextInt();
switch(ch)

case 1:

i.insert_data();

break;

case 2:

i.update_data();

break;

case 3:

i.delete_data();

break;

case 4:

i.select_data();

break;

case 5:

System.exit(0);

break;

case 6:

i.calculate();

break;

default:

System.out.println("Invalid Choice....");

You might also like