Deep Dive into Java Beans and JDBC SQL Execution
🕒 2025-04-23 02:55:45.673149Java'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?
- Introduction to Java Beans
- Java Beans code example that uses all the properties.
- Property design pattern
- Bean writing process
- JDBC (Java Database Connectivity) | Executing SQL statements in JDBC
Introduction to Java Beans
Java Beans code example that uses all the properties:
//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 propertypackage 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:
Bean writing process:
JDBC (Java Database Connectivity) | Executing SQL statements in JDBC
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:
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.
Comments
Loading comments...
Leave a Comment