Showing posts with label J2EE. Show all posts
Showing posts with label J2EE. Show all posts

Sunday, April 29, 2018

What are the necessary steps to work with JDBC ?

There are 5 steps to work with JDBC as illustrated with an example below:
  1. import java.sql.DriverManager;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import java.math.BigDecimal;
  6. import java.sql.Blob;

  7. public class JDBCConfiguration {

  8. public static void main(String[] args) {

  9. Connection con = null;
  10. Statement stmt = null;
  11. ResultSet rs = null;
  12. try {

  13. // 1. Load the driver
  14. Class.forName("com.mysql.jdbc.Driver");

  15. // 2. Get the DB connection via driver
  16. String dbURL = "jdbc:mysql://ishaan-pc:3306/mydb?user=root&password=root";
  17. con = DriverManager.getConnection(dbURL); // DriverManager : class
  18. // getConnection() : static

  19. // 3. Execute the SQL query via connection
  20. String query = "SELECT * FROM EMPLOYEE";
  21. stmt = con.createStatement();
  22. rs = stmt.executeQuery(query);

  23. // 4. Process the result
  24. while (rs.next()) {
  25. int id = rs.getInt("empid");
  26. String name = rs.getString("empname");
  27. long age = rs.getLong("age");
  28. double salary = rs.getDouble("salary");
  29. BigDecimal serialNumber = rs.getBigDecimal("serialnum");
  30. boolean status = rs.getBoolean("status");
  31. /*
  32. * BLOB - Binary Large Object that contains a variable amount of
  33. * data. Values are treated as binary strings.We don't need to
  34. * specify length while creating a column
  35. */
  36. Blob picture = rs.getBlob("emppicture");

  37. System.out.println("emp id: " + id);
  38. System.out.println("emp name: " + name);
  39. System.out.println("emp age: " + age);
  40. System.out.println("emp salary: " + salary);
  41. System.out.println("emp serial no: " + serialNumber);
  42. System.out.println("Is emp active?: " + status);
  43. System.out.println("emp photo: " + picture);

  44. }
  45. } catch (Exception e) {
  46. System.out.println("exception occured due to : " + e);
  47. } finally {

  48. // 5. Close all the JDBC objects
  49. try {
  50. if (con != null) {
  51. con.close();
  52. }
  53. if (stmt != null) {
  54. stmt.close();
  55. }
  56. if (rs != null) {
  57. rs.close();
  58. }
  59. } catch (Exception e) {
  60. System.out.println("exception occured in finally block : " + e);
  61. }
  62. }
  63. }
  64. }
Output: 
emp id: 101
emp name: ishaan
emp age: 21
emp salary: 9000
emp serial no: 11229933994439101
Is emp active?: true
emp photo: 👨

java.lang.Class.forName() method features !!

Class.forName(String fullyQualifiedClassName): It loads the class or interface at run time (dynamically) into the memory and returns the Class object associated with the class or interface with the given string name

  1. public class ClassForNameImpl {
  2. public static void main(String[] args) {
  3. try {
  4. Class<?> cls1 = Class.forName("com.ishaan.jdbc.LoadDemoClass");
  5. System.out.println("return value of forName() method: " + cls1);
  6. System.out.println("--------------------------------------");
  7. String qualifiedClassName = cls1.getName();
  8. System.out.println("return value of getName() method: " + qualifiedClassName);

  9. System.out.println("--------------------------------------");
  10. String onlyClassName = cls1.getSimpleName();
  11. System.out.println("return value of getSimpleName() method: " + onlyClassName);

  12. System.out.println("--------------------------------------");
  13. ClassLoader cLoader = cls1.getClassLoader();
  14. System.out.println("return value of getClassLoader() method: " + cLoader);

  15. System.out.println("--------------------------------------");
  16. System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
  17. System.out.println("--------------------------------------");
  18. System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName));

  19. System.out.println("#########################################");

  20. Class<?> cls2 = Class.forName("java.util.ArrayList");
  21. System.out.println("return value of forName() method: " + cls2);

  22. System.out.println("--------------------------------------");
  23. String qualifiedClassName2 = cls2.getName();
  24. System.out.println("return value of getName() method: " + qualifiedClassName2);

  25. System.out.println("--------------------------------------");
  26. String onlyClassName2 = cls2.getSimpleName();
  27. System.out.println("return value of getSimpleName() method: " + onlyClassName2);

  28. System.out.println("--------------------------------------");
  29. ClassLoader cLoader2 = cls2.getClassLoader();
  30. System.out.println("return value of getClassLoader() method: " + cLoader2);

  31. System.out.println("--------------------------------------");
  32. System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
  33. System.out.println("--------------------------------------");
  34. System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName2));

  35. System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

  36. Class<?> cls3 = Class.forName("java.lang.Thread");
  37. System.out.println("return value of forName() method: " + cls3);

  38. System.out.println("--------------------------------------");
  39. String qualifiedClassName3 = cls3.getName();
  40. System.out.println("return value of getName() method: " + qualifiedClassName3);

  41. System.out.println("--------------------------------------");
  42. String onlyClassName3 = cls3.getSimpleName();
  43. System.out.println("return value of getSimpleName() method: " + onlyClassName3);

  44. System.out.println("--------------------------------------");
  45. ClassLoader cLoader3 = cls3.getClassLoader();
  46. System.out.println("return value of getClassLoader() method: " + cLoader3);

  47. System.out.println("--------------------------------------");
  48. System.out.println("return value of getSystemClassLoader() method: " + ClassLoader.getSystemClassLoader());
  49. System.out.println("--------------------------------------");
  50. System.out.println("return value of loadClass() method: " + cLoader.loadClass(qualifiedClassName3));

  51. } catch (Exception e) {
  52. System.out.println("Class not found, please pass the fully qualified class name as an input argument for the method Class.forName() !!");
  53. System.out.println(e.getMessage())
  54. }
  55. }
  56. }
Output: 

return value of forName() method: class com.ishaan.jdbc.LoadDemoClass
--------------------------------------
return value of getName() method: com.ishaan.jdbc.LoadDemoClass
--------------------------------------
return value of getSimpleName() method: LoadDemoClass
--------------------------------------
return value of getClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of getSystemClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of loadClass() method: class com.ishaan.jdbc.LoadDemoClass
#########################################
return value of forName() method: class java.util.ArrayList
--------------------------------------
return value of getName() method: java.util.ArrayList
--------------------------------------
return value of getSimpleName() method: ArrayList
--------------------------------------
return value of getClassLoader() method: null
--------------------------------------
return value of getSystemClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of loadClass() method: class java.util.ArrayList
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
return value of forName() method: class java.lang.Thread
--------------------------------------
return value of getName() method: java.lang.Thread
--------------------------------------
return value of getSimpleName() method: Thread
--------------------------------------
return value of getClassLoader() method: null
--------------------------------------
return value of getSystemClassLoader() method: sun.misc.Launcher$AppClassLoader@73d16e93
--------------------------------------
return value of loadClass() method: class java.lang.Thread


Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...