I NEED JUST THE IMPLEMENTATION FOR THIS TWO PUBLIC CLASSES. Thank you public Arr
ID: 3738875 • Letter: I
Question
I NEED JUST THE IMPLEMENTATION FOR THIS TWO PUBLIC CLASSES. Thank you
public ArrayList<String> query(String query)
Input parameter query contains an SQL query (i.e., selectstatement). The method creates a JDBC Statement object that executes the query. The ResultSet that is created by the JDBC Statement object is stored in an ArrayList of Strings and is returned by this method.
The method returns null if an exception occurs within this method, or if the ResultSet is empty.
public boolean executeDML(String dml)
Input parameter dml contains a SQL Data Modification Language statement (i.e., insert, update, or delete). The method creates a JDBC Statement object that executes the DML statement.
The method return true if the DML statement executes successfully, or false if it does not
Explanation / Answer
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/*
* Please place the required jar file in the classpath
* please change the url connection like hostname and port and user id and password
*/
public class DataBase {
public ArrayList<String> query(String query) {
ArrayList<String> list = new ArrayList<String>();
// Change Accordingly
Connection dbConnection = null;
try {
dbConnection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "uesrid", "password");
} catch (Exception e) {
System.out.println("Problem in Creating Connection ");
return null;
}
Statement statement = null;
try {
// creating statment
statement = dbConnection.createStatement();
} catch (Exception e) {
System.out.println("Problem in Creating Statment");
return null;
}
ResultSet rs = null;
try {
// executing the query which will return the results
rs = statement.executeQuery(query);
} catch (SQLException e) {
System.out.println("Problem in execuitng query");
return null;
}
try {
while (rs.next()) {
// assuming we want fetch the first column so we are passing the 1
list.add(rs.getString(1));
}
} catch (SQLException e) {
}
return list;
}
public boolean executeDML(String dml) {
// Change Accordingly
Connection dbConnection = null;
try {
dbConnection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "password");
} catch (Exception e) {
System.out.println("Problem in Creating Connection ");
return false;
}
Statement statement = null;
try {
statement = dbConnection.createStatement();
} catch (Exception e) {
System.out.println("Problem in Creating Statment");
return false;
}
int res=0;
try {
// if is success than it will return the number of rows got updated/inserted/deleted
res = statement.executeUpdate(dml);
} catch (SQLException e) {
System.out.println("Problem in execuiting query");
return false;
}
return res!=0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.