Java's most important concepts include Java beans and JDBC (Java Database Connectivity). We will see a Java Beans code example that uses all the properties.

What will you learn?

  1. Introduction to Java Beans
  2. Java Beans code example that uses all the properties.
  3. Property design pattern
  4. Bean writing process
  5. JDBC (Java Database Connectivity) | Executing SQL statements in JDBC

Introduction to Java Beans

What are java beans? What distinguishes it from other Java classes? Discuss the property design patterns using appropriate examples.

How java beans are different from Java classes? Explain the bean writing process.

A Java Bean is a software component that has been intended to be reusable in several different environments. It is a Java class that should follow the following conventions:

  • It should have a public no-arg constructor.
  • It should be Serializable.
  • It must include getter and setter methods, which are used to set and get the values of properties.

Java Bean can be easily used. It can create libraries of reusable components for plugging into JSP. It follows certain rules and guidelines to enable their use as components which we call design patterns.

Java Beans code example that uses all the properties:

CSIT_students.java:

//java beans follow all three property
package beans;

public class CSIT_students implements java.io.Serializable{ //second property
	private int id;
	private String name;
	
	public CSIT_students() { //first property
		
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
//setter, getter --> third property

new_class.java:

package beans;

public class new_class {

    public static void main(String[] args) {
	CSIT_students stu = new CSIT_students();
	stu.setName("Ishwar");
	System.out.println(stu.getName());
    }
}


Property design pattern:

Java beans follow certain rules and guidelines in order to enable their use as components which we call design patterns. The following are the types of bean properties:

1. Simple properties: Getter and setter methods are included

2. Bound properties

3. Constrained properties

4. Indexed properties

Bean writing process:

1. Creating a simple bean (write a Java bean program):   

 write a program that we write before (CSIT_students.java)

2. Compiling the bean:     

javac CSIT_students.java  

This produces the class file CSIT_students.class

3. Create a manifest file:    

We can use our favorite text editor to create a manifest file (say, manifest.tmp) that contains the following text:    

Name: CSIT_students.clss    

Java-Bean: True

4. Create the jar file:    

jar cf CSIT_students.jar manifest.tmp CSIT_students.class

5. Start the Bean Development Kit

6. Test the JAR file

JDBC (Java Database Connectivity) | Executing SQL statements in JDBC

JDBC stands for Java Database Connectivity. As its name suggests, it is a process of connection between Java and a database using MySQL connector.

JDBC (Java Database Connectivity) is a Java API that provides database-independent communication between the Java programming language and a variety of databases.

package myprogram;

import java.sql.*;

public class JDBC {

    public static void main(String[] args) throws SQLException {
        String url = "jdbc:mysql://localhost:3306/my_database";
        String username = "root";
        String password = "";
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                conn = DriverManager.getConnection(url, username, password);
            } catch (SQLException e) {
                System.out.println(e);
            }
        } catch (ClassNotFoundException e) {
            System.out.println("Class not found exception");
        }
        Statement stmt = conn.createStatement();
        String sql = "select * from student where district='Kathmandu'";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.printf("%d %s %s %d \n",
                rs.getInt("ID"),
                rs.getString("name"),
                rs.getString("district"),
                rs.getInt("age")
            );
        }
        stmt.close();
        conn.close();
    }
}

Executing SQL statements in JDBC:

To execute SQL statement in JDBC, we have to first load our driver.
Class.forName("com.mysql.jdbc.Driver");

Then we need to establish a connection to a database:
String url = "jdbc:mysql://localhost/";
String user = "username";
String passwd = "password";
Connection conn = DriverManager.getConnection(url, user, passwd);

Then we need to create a statement:
The connection interface provides methods named createStatement() to create a Statement, prepareStatement() to prepare a statement, and preparecall() to call a statement. We must create any of these statements using the appropriate method:
conn.createStatement();
conn.preparedStatement(query);
conn.preparecall(query);

Use one of the following techniques to execute the created statement:
stmt.execute(query);
stmt.executeQuery(query);

Finally, we read the result of the query via a ResultSet (rs) object:
while(rs.next()){
}

Conclusion:

This post covers all the important concepts of Java like Java Beans, Bean Properties, Property Design Patterns, Bean writing process, JDBC, and Executing SQL statements in JDBC.

I hope this post is very helpful to you. If you have any questions, you can ask me in the comment section. I will reply as soon as possible. Thanks.