Sunteți pe pagina 1din 6

package testmysql;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Scanner;

public class TestMySql {

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

Scanner scan = new Scanner(System.in);

int count=0;

char nextOP='y';

char op;

while(nextOP =='y'|| nextOP=='Y'){

System.out.print("\n[A] Add records");

System.out.print("\n[U] Update records");

System.out.print("\n[S] Search records");

System.out.print("\n[D] Delete records");

System.out.print("\n[A] Select Operation: ");

String str=scan.next();

op=str.charAt(0);

switch(op){

case 'a': case 'A':

try{

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

String connectionUrl="jdbc:mysql://localhost:3306/test?"+"user=root&password=";
Connection con = DriverManager.getConnection(connectionUrl);

String sql= "INSERT INTO tbl_student_info(fn,`mn`,`ln`, status) VALUES (?,?,?,?)";

PreparedStatement prest = con.prepareStatement(sql);

System.out.print("\nEnter first name: ");

String first_n = scan.next();

prest.setString(1, first_n);

System.out.print("\nEnter Middle name: ");

String mid_i = scan.next();

prest.setString(2, mid_i);

System.out.print("\nEnter Last name: ");

String last_n = scan.next();

prest.setString(3, last_n);

System.out.print("\nEnter status(active/inactive): ");

String status = scan.next();

prest.setString(4, status);

count = prest.executeUpdate();

System.out.println(count + "row(s) affected");

if(count==1){

System.out.println("\nYou are now registered!");

con.close();

count=0;

catch (SQLException e){

System.out.println("SQL Exception" + e.toString());

} catch (ClassNotFoundException cE){


System.out.println("Class not found: "+ cE.toString());

break;

case 'u': case 'U':

String last_n="";

try{

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

String connectionUrl="jdbc:mysql://localhost:3306/test?"+"user=root&password=";

Connection con = DriverManager.getConnection(connectionUrl);

String sql = "SELECT fn, mn, ln FROM tnl_student_info WHERE ln=?";

PreparedStatement prest = con.prepareStatement(sql);

System.out.print("\nEnter last name of the person you want to search: ");

String searchLastName=scan.next();

prest.setString(1, searchLastName);

ResultSet rs = prest.executeQuery();

while (rs.next()){

String first_n = rs.getString(1);

String mid_i = rs.getString(2);

last_n = rs.getString(3);

count++;

System.out.println(first_n + "\t" + mid_i +"\t"+ last_n);

String updateSql = "UPDATE student_info set fn=?, mn=? WHERE ln=?";

PreparedStatement prestUpdate = con.prepareStatement(updateSql);

String newMiddleName="null";

System.out.print("\nEnter the new First name: ");


String newFirstName = scan.next();

prestUpdate.setString(1, newFirstName);

System.out.print("\nEnter new middle name: ");

newMiddleName=scan.next();

prestUpdate.setString(2, newMiddleName);

prestUpdate.setString(3, last_n);

prestUpdate.executeUpdate();

prestUpdate.close();

con.close();

}//end of try

catch (SQLException s){

System.out.println("SQL statement is not executed!");

break;

case 's': case 'S':

try{

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

String connectionUrl="jdbc:mysql://localhost:3306/test?"+"user=root&password=";

Connection con = DriverManager.getConnection(connectionUrl);

try{

String sql = "SELECT fn, mn, ln FROM tbl_student_info WHERE ln=? AND status=?";

PreparedStatement prest = con.prepareStatement(sql);

System.out.print("\nEnter last name of the person you want to search: ");

String searchLastName=scan.next();

prest.setString(1, searchLastName);

prest.setString(2, "active");
ResultSet rs = prest.executeQuery();

while (rs.next()){

String first_n = rs.getString(1);

String mid_i = rs.getString(2);

last_n = rs.getString(3);

count++;

System.out.println(first_n + "\t" + mid_i +"\t"+ last_n);

System.out.println("Number of records: " + count);

prest.close();

con.close();

catch (SQLException s){

System.out.println("SQL statement is not executed!");

catch (Exception e){

e.printStackTrace();

break;

case 'd': case 'D':

try{

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

String connectionUrl="jdbc:mysql://localhost:3306/test?"+"user=root&password=";

Connection con = DriverManager.getConnection(connectionUrl);

String sql = "UPDATE student_info SET status=? WHERE ln=?";


PreparedStatement prestDelete = con.prepareStatement(sql);

prestDelete.setString(1, "inactive");

System.out.print("\nEnter last name of the person you want to delete: ");

String searchLastName=scan.next();

prestDelete.setString(2, searchLastName);

prestDelete.executeUpdate();

prestDelete.close();

con.close();

catch (SQLException s){

System.out.println("SQL statement is not executed!");

break;

default:

System.out.print("\nThe operation selected is not Valid!");

break;

//display

}//end of switch

System.out.print("\nDo you want to do another operation?Y

S-ar putea să vă placă și