{"id":676,"date":"2012-12-27T02:17:35","date_gmt":"2012-12-27T02:17:35","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=419"},"modified":"2012-12-27T02:17:35","modified_gmt":"2012-12-27T02:17:35","slug":"how-to-create-java-bean-and-use-for-crud-operation","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-create-java-bean-and-use-for-crud-operation\/","title":{"rendered":"How to create Java bean and use for CRUD Operation"},"content":{"rendered":"<p><strong>Database Table <\/strong><\/p>\n<pre>\n CREATE TABLE employeeList(empid int not null primary key auto_increment,\n                           empname varchar(255),\n                           salary  double);\n<\/pre>\n<p><strong>Bean Class<\/strong><\/p>\n<pre>\npackage com.javadb2.mugil.employee;\n\npublic class bean\n{\n  private int  empId;\n  private String  empName;\n  private double  empSalary;\n\t\n  public int getEmpId() \n  {\n    return empId;\n  }\n\t\n  public void setEmpId(int empId) \n  {\n    this.empId = empId;\n  }\n\t\n  public String getEmpName() \n  {\n    return empName;\n  }\n  \n  public void setEmpName(String empName) \n  {\n    this.empName = empName;\n  }\n  \n  public double getEmpSalary() \n  {\n    return empSalary;\n  }\n\n  public void setEmpSalary(double empSalary) \n  {\n    this.empSalary = empSalary;\n  }\n}\n<\/pre>\n<p><strong>Crud Operation with Bean Class<\/strong><\/p>\n<pre>\n package com.javadb2.mugil.employee;\n\nimport java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\n\nimport com.javadb2.mugil.db.display;\nimport com.mysql.jdbc.PreparedStatement;\nimport com.mysql.jdbc.Statement;\n\npublic class insert\n{\t\n  public static void main(String[] args) \n  {\n    bean objBean = new bean();\n\t\t\n    objBean.setEmpName(display.getInput(\"Enter the Employee Name\"));\n    objBean.setEmpSalary(Integer.parseInt(display.getInput(\"Enter Salary\")));\n\t\t\n    boolean result = getInsertedKey(objBean);\n\t\t\n    if(result)\n    {\n      System.out.println(\"The Last Inserted Primary Key is \"+objBean.getEmpId());\t\t\t \n    }\n   }\n\t\n   public static boolean getInsertedKey(bean objBean)\n   {\t\n     ResultSet keys = null;\n\t\t\n     String strSQL = \"INSERT INTO employeeList(empname, salary)VALUES(?,?)\";\t\t\n\t\t\n     try \n     {\n\tConnection conn \t= getConnection();\n\tPreparedStatement stmt \t= (PreparedStatement) conn.prepareStatement(strSQL, Statement.RETURN_GENERATED_KEYS);\n\t\t\t\n\tstmt.setString(1, objBean.getEmpName());\n\tstmt.setDouble(2, objBean.getEmpSalary());\n\t\t\t\n\tint affected = stmt.executeUpdate();\n\t\t\t\n\tif(affected == 1)\n\t{\n  \t   keys = stmt.getGeneratedKeys();\n\t   keys.next();\n\t   int newkey = keys.getInt(1);\n\t   objBean.setEmpId(newkey);\n\t}\n\telse\n\t{\n\t   System.err.println(\"Unable to Update Values\");\n\t   return false;\n\t}\n      }\n      catch (SQLException e) \n      {\n\t processException(e);\n\t System.err.println(\"Error in Establishing Connection\");\n      }\n\t\t\n      return true;\n  }\n\t\n  public static Connection getConnection() throws SQLException\n  {\n    Connection conn = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost\/test\", \"root\", \"pass\");\n    return conn;\n  }\n\t\n  public static void processException(SQLException e)\n  {\n     System.err.println(\"Error Message \"+e.getMessage());\n     System.err.println(\"Error Code\"+e.getErrorCode());\n     System.err.println(\"SQL State\"+e.getSQLState());\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Database Table CREATE TABLE employeeList(empid int not null primary key auto_increment, empname varchar(255), salary double); Bean Class package com.javadb2.mugil.employee; public class bean { private int empId; private String empName; private double empSalary; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; }&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-create-java-bean-and-use-for-crud-operation\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-676","post","type-post","status-publish","format-standard","hentry","category-database","category-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":0,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}