Untitled
unknown
plain_text
a year ago
2.9 kB
18
Indexable
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import static java.lang.System.exit; public class DbDemo { public static void dbMenus() { System.out.println("\n\t\t\t ********* Welcome to my Database *********"); System.out.println("\t\t\t ----- Please select the DB Operation below -----\n"); System.out.println("\t\t\t1 - Create Data "); System.out.println("\t\t\t2 - Read Data "); System.out.println("\t\t\t3 - Update Data "); System.out.println("\t\t\t4 - Delete Data "); System.out.println("\t\t\t5 - Exit"); System.out.print("\t\t\t\tSelect any option \n"); Scanner sc = new Scanner(System.in); //System.out.println(sc.next()); int userInput = sc.nextInt(); switch (userInput) { case 1: System.out.println("You've selected Menu 1. Let's add some data"); createOperation(); break; case 2: System.out.println("You selected option 2"); break; case 3: System.out.println("You selected option 3"); break; case 4: System.out.println("You selected option 4"); break; case 5: System.out.println(); exit(0); } } private static void createOperation() { final String uname = "root"; final String pass = ""; Scanner sc = new Scanner(System.in); System.out.println("\t\t\t*'Your ID' ...."); System.out.print("\t\t\tEnter student ID number : "); int id = sc.nextInt(); System.out.print("\t\t\tEnter student name : "); String name = sc.next(); System.out.print("\t\t\tEnter student roll : "); String roll = sc.next(); // System.out.print("\t\t\tIs student scholar \t Type 0 for false and 1 for true: "); // String location = sc.next(); // String query="INSERT INTO tbl_students (id, name, roll, is_scholar) values ('"+id+"','"+name+"','"+roll+"')"; String query = "INSERT INTO tbl_students (name, roll) values ('" + name + "','" + roll + "')"; try { Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "root", ""); Statement stmt = con.createStatement(); int rows = stmt.executeUpdate(query); if (rows == 1) { System.out.println("\t\t\t rows affected...."); } else { System.out.println("\t\t\tSomething is Wrong.....!! "); } } catch (SQLException e) { throw new RuntimeException(e); } } public static void main(String[] args) { dbMenus(); } }
Editor is loading...
Leave a Comment