DATABASE CONNECTIVITY QUERIES: 1 ==> INSERT QUERY: "INSERT INTO tableName(id,name)" + "VALUES(?,?)" 2 ==> DELETE QUERY: "DELETE FROM tableName WHERE id=1" 3 ==> UPDATE QUERY: "UPDATE tableName SET id='2' , name= 'hashmi' WHERE id=1 " 4 ==> PRINT ALL DATA: "SELECT * FROM tableName" USES OF QUERIES IN PROGRAM: import java.sql.*; import java.util.*; /** * * @author usman */ public class SameAssignment { public static void main(String[] args) throws SQLException, ClassNotFoundException { Employee em = new Employee(026,"hashmi",true,"Permanenet"); // Employee em1 = new Employee(146,"ali ",true,"Permanenet"); // Employee em2 = new Employee(040,"usamn",true,"Permanenet");
Scanner input = new Scanner(System.in); mainmenu(); //calling functions System.out.print("Enter your choice: "); switch (input.nextInt()) { case 1: addEmployee(em); //addEmployee(em1); // addEmployee(em2); break; case 2: updateEmployee(); break; case 3: deleteEmployee(); break; case 4: serchEmplpoyee(em); break; case 5: printEmployee(); break; case 0: System.exit(0); default: throw new AssertionError(); } } //insert new data static void addEmployee(Employee employee) throws ClassNotFoundException{ try{
Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/employee","hashmi" ,"470350");} catch (Exception ex) { System.out.println("usman" +ex); } try{ int id = employee.getId(); String name = employee.getName(); boolean gender = employee.isGender(); String type = employee.getType(); Statement st = con.createStatement(); String SQL = "INSERT INTO employee(id, name, gender,type) " + "VALUES( id , name, gender , type )" ;
int num = st.executeUpdate(SQL); System.out.println("Updated"); } catch (SQLException ex) { System.out.println("w:" +ex.getMessage()); } } //Search data static void searchEmplpoyee(Employee employee) throws ClassNotFoundException{ try{
Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/employee","hashmi" ,"470350"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM employee WHERE id = 1");
0 Comments