একবার আপনি স্টেটমেন্ট অবজেক্ট তৈরি করলে আপনি স্টেটমেন্ট ইন্টারফেসের এক্সিকিউট পদ্ধতিগুলির একটি ব্যবহার করে এটি চালাতে পারেন, যেমন execute(), executeUpdate() এবং executeQuery()।
এক্সিকিউট() পদ্ধতি: এই পদ্ধতিটি SQL DDL স্টেটমেন্ট চালানোর জন্য ব্যবহার করা হয়, এটি একটি বুলিয়ান মান প্রদান করে আবহাওয়া নির্দিষ্ট করে যে ResultSet অবজেক্টটি পুনরুদ্ধার করা যেতে পারে।
উদাহরণ
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Example {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Executing the statement
String createTable = "CREATE TABLE Employee( "
+ "Name VARCHAR(255), "
+ "Salary INT NOT NULL, "
+ "Location VARCHAR(255))";
boolean bool = stmt.execute(createTable);
System.out.println(bool);
}
} আউটপুট
Connection established...... false
executeUpdate(): এই পদ্ধতিটি সন্নিবেশ করা, আপডেট করা, মুছে ফেলার মতো বিবৃতি কার্যকর করতে ব্যবহৃত হয়। এটি প্রভাবিত সারি সংখ্যা প্রতিনিধিত্ব করে একটি পূর্ণসংখ্যা মান প্রদান করে।
উদাহরণ
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ExecuteUpdateExample {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
String insertData = "INSERT INTO Employee("
+ "Name, Salary, Location) VALUES "
+ "('Amit', 30000, 'Hyderabad'), "
+ "('Kalyan', 40000, 'Vishakhapatnam'), "
+ "('Renuka', 50000, 'Delhi'), "
+ "('Archana', 15000, 'Mumbai')";
int i = stmt.executeUpdate(insertData);
System.out.println("Rows inserted: "+i);
}
} আউটপুট
Connection established...... Rows inserted: 4
executeQuery(): এই পদ্ধতিটি বিবৃতি কার্যকর করতে ব্যবহৃত হয় যা ট্যাবুলার ডেটা প্রদান করে (উদাহরণ নির্বাচন করুন)। এটি ক্লাস ফলাফল সেটের একটি বস্তু প্রদান করে।
উদাহরণ
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ExecuteQueryExample {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Retrieving data
ResultSet rs = stmt.executeQuery("Select *from Employee");
while(rs.next()) {
System.out.print("Name: "+rs.getString("Name")+", ");
System.out.print("Salary: "+rs.getInt("Salary")+", ");
System.out.print("City: "+rs.getString("Location"));
System.out.println();
}
}
} আউটপুট
Connection established...... Name: Amit, Salary: 30000, City: Hyderabad Name: Kalyan, Salary: 40000, City: Vishakhapatnam Name: Renuka, Salary: 50000, City: Delhi Name: Archana, Salary: 15000, City: Mumbai