package com.javadb2.mugil.employee; import java.io.BufferedReader; import java.io.InputStreamReader; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.javadb2.mugil.db.display; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; public class update { public static void main(String args[]) { bean objBean = new bean(); objBean.setEmpId(Integer.parseInt(getInput("Enter Employee Id :"))); objBean.setEmpName((String)getInput("Enter Employee Name:")); objBean.setEmpSalary(Integer.parseInt(getInput("Salary"))); updateValues(objBean); } public static void updateValues(bean objBean) { System.out.println(objBean.getEmpName()); System.out.println(objBean.getEmpSalary()); String strSQL = "UPDATE employeelist" + " SET empname = ? ," + " salary = ?" + " WHERE empid = ?"; try { Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "pass"); PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(strSQL); stmt.setString(1, objBean.getEmpName()); stmt.setDouble(2, objBean.getEmpSalary()); stmt.setInt(3, objBean.getEmpId()); int affected = stmt.executeUpdate(); if(affected == 1) { System.out.println("Updated Successfully"); } else { System.err.println("Unable to Update Employee"); } } catch (SQLException e) { e.printStackTrace(); } } public static String getInput(String prompt) { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print(prompt); System.out.flush(); try { return stdin.readLine(); } catch (Exception e) { // TODO: handle exception return e.getMessage(); } } }
You must be logged in to post a comment.