Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the answer I got for the java program. Please provide just the code I need to make the program work. Below this answer

Here is the answer I got for the java program. Please provide just the code I need to make the program work. Below this answer is the requirments. You can also see the full requirements in the answered chegg question. Thank you!

Complex systems often require both online and batch processing. Each kind of processing has very different requirements. Because online processing involves a user waiting on application processing order, the timing and performance of each statement execution in a process is important. Batch processing, on the other hand, occurs when a bunch of distinct transactions need to occur independently of user interaction. A banks ATM machine is an example of a system of online processes. The monthly process that calculates and adds interest to your savings account is an example of a batch process.

JDBC 2.0 introduced new functionality to address the specific issues of batch processing. Using the JDBC 2.0 batch facilities, you can assign a series of SQL statements to a JDBC Statement (or one of its subclasses) to be submitted together for execution by the database. Using the techniques you have learned so far in this book, account interest-calculation processing occurs roughly in the following fashion:

Prepare statement.

Bind parameters.

Execute.

Repeat steps 2 and 3 for each account.

This style of processing requires a lot of back and forth between the Java application and the database. JDBC 2.0 batch processing provides a simpler, more efficient approach to this kind of processing:

Prepare statement.

Bind parameters.

Add to batch.

Repeat steps 2 and 3 until interest has been assigned for each account.

Execute.

Under batch processing, there is no back and forth between the database for each account. Instead, all Java-level processingthe binding of parametersoccurs before you send the statements to the database. Communication with the database occurs in one huge burst; the huge bottleneck of stop and go communication with the database is gone.

Statement and its children all support batch processing through an addBatch( ) method. For Statement , addBatch() accepts a String that is the SQL to be executed as part of the batch. The PreparedStatement andCallableStatement classes, on the other hand, use addBatch() to bundle a set of parameters together as part of a single element in the batch. The following code shows how to use a Statement object to batch process interest calculation:

Statement stmt = conn.createStatement( ); int[] rows; for(int i=0; i 

Example 4.1 provides the full example of a batch program that runs a monthly password-cracking program on peoples passwords. The program sets a flag in the database for each bad password so a system administrator can act appropriately.

import java.sql.*; import java.util.ArrayList; import java.util.Iterator; public class Batch { static public void main(String[] args) { Connection conn = null; try { // we will store the bad UIDs in this list ArrayList breakable = new ArrayList( ); PreparedStatement stmt; Iterator users; ResultSet rs; Class.forName(args[0]).newInstance( ); conn = DriverManager.getConnection(args[1], args[2], args[3]); stmt = conn.prepareStatement("SELECT user_id, password " + "FROM user"); rs = stmt.executeQuery( ); while( rs.next( ) ) { String uid = rs.getString(1); String pw = rs.getString(2); // Assume PasswordCracker is some class that provides // a single static method called crack( ) that attempts // to run password cracking routines on the password if( PasswordCracker.crack(uid, pw) ) { breakable.add(uid); } } stmt.close( ); if( breakable.size( )  

--Oracle DB CREATE TABLE Employee ( empId NUMBER NOT NULL, name varchar2(10) DEFAULT NULL, PRIMARY KEY (empId) ); --MySQL DB CREATE TABLE `Employee` ( `empId` int(10) unsigned NOT NULL, `name` varchar(10) DEFAULT NULL, PRIMARY KEY (`empId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#mysql DB properties DB_DRIVER_CLASS=com.mysql.jdbc.Driver DB_URL=jdbc:mysql://localhost:3306/UserDB #DB_URL=jdbc:mysql://localhost:3306/UserDB?rewriteBatchedStatements=true DB_USERNAME=pankaj DB_PASSWORD=pankaj123 #Oracle DB Properties #DB_DRIVER_CLASS=oracle.jdbc.driver.OracleDriver #DB_URL=jdbc:oracle:thin:@localhost:1871:UserDB #DB_USERNAME=scott #DB_PASSWORD=tiger

DBConnection.java

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

public class DBConnection {

public static Connection getConnection() {

Properties props = new Properties();

FileInputStream fis = null;

Connection con = null;

try {

fis = new FileInputStream("db.properties");

props.load(fis);

// load the Driver Class

Class.forName(props.getProperty("DB_DRIVER_CLASS"));

// create the connection now

con = DriverManager.getConnection(props.getProperty("DB_URL"),

props.getProperty("DB_USERNAME"),

props.getProperty("DB_PASSWORD"));

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return con;

}

}

Use Statement to execute one query at a time.

JDBCStatement.java

package com.journaldev.jdbc.batch;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.Statement;

public class JDBCStatement {

public static void main(String[] args) {

Connection con = null;

Statement stmt = null;

try {

con = DBConnection.getConnection();

stmt = con.createStatement();

long start = System.currentTimeMillis();

for(int i =0; i

String query = "insert into Employee values ("+i+",'Name"+i+"')";

stmt.execute(query);

}

System.out.println("Time Taken="+(System.currentTimeMillis()-start));

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

stmt.close();

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

Use PreparedStatement to execute one query at a time.

JDBCPreparedStatement.java

package com.journaldev.jdbc.batch;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class JDBCPreparedStatement {

public static void main(String[] args) {

Connection con = null;

PreparedStatement ps = null;

String query = "insert into Employee (empId, name) values (?,?)";

try {

con = DBConnection.getConnection();

ps = con.prepareStatement(query);

long start = System.currentTimeMillis();

for(int i =0; i

ps.setInt(1, i);

ps.setString(2, "Name"+i);

ps.executeUpdate();

}

System.out.println("Time Taken="+(System.currentTimeMillis()-start));

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

ps.close();

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

Using Statement Batch API for bulk processing.

JDBCStatementBatch.java

package com.journaldev.jdbc.batch;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.Statement;

public class JDBCStatementBatch {

public static void main(String[] args) {

Connection con = null;

Statement stmt = null;

try {

con = DBConnection.getConnection();

stmt = con.createStatement();

long start = System.currentTimeMillis();

for(int i =0; i

String query = "insert into Employee values ("+i+",'Name"+i+"')";

stmt.addBatch(query);

//execute and commit batch of 1000 queries

if(i%1000 ==0) stmt.executeBatch();

}

//commit remaining queries in the batch

stmt.executeBatch();

System.out.println("Time Taken="+(System.currentTimeMillis()-start));

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

stmt.close();

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

Using PreparedStatement Batch Processing API for bulk queries.

JDBCPreparedStatementBatch.java

package com.journaldev.jdbc.batch;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class JDBCPreparedStatementBatch {

public static void main(String[] args) {

Connection con = null;

PreparedStatement ps = null;

String query = "insert into Employee (empId, name) values (?,?)";

try {

con = DBConnection.getConnection();

ps = con.prepareStatement(query);

long start = System.currentTimeMillis();

for(int i =0; i

ps.setInt(1, i);

ps.setString(2, "Name"+i);

ps.addBatch();

if(i%1000 == 0) ps.executeBatch();

}

ps.executeBatch();

System.out.println("Time Taken="+(System.currentTimeMillis()-start));

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

ps.close();

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

Lets see how our programs work with MySQL database,

I have executed them separately multiple times and below table contains the results.

MYSQL DB STATEMENT PREPAREDSTATEMENT STATEMENT BATCH PREPAREDSTATEMENT BATCH

Time Taken (ms) 8256 8130 7129 7019

When I looked at the response time,

I was not sure whether its right because I was expecting some good response time improvements with Batch Processing.

So I looked online for some explanation and found out that by default MySQL batch processing works in similar way like running without batch.

To get the actual benefits of Batch Processing in MySQL, we need to pass rewriteBatchedStatements as TRUE while creating the DB connection.

Look at the MySQL URL above in db.properties file for this.

With rewriteBatchedStatements as true, below table provides the response time for the same programs.

MYSQL DB STATEMENT PREPAREDSTATEMENT STATEMENT BATCH PREPAREDSTATEMENT BATCH

Time Taken (ms) 5676 5570 3716 394

As you can see that PreparedStatement Batch Processing is very fast when rewriteBatchedStatements is true.

So if you have a lot of batch processing involved, you should use this feature for faster processing.

Here is the requirements for the program. image text in transcribed

peogram lells you what type e a triangle you have ciangle. llesides wrting the uhele program, your tank is so All the ingas are inegers However, they migt b The fileco tanthe proper .sher ofenaies4.maiple Lincs that bgin with a"a coments, and sbould For the iction tha os te triangle pe, dothe Caloulate te cyclomanc complesity, and show how you yous and mine) Expected revals shall be documentiod Tum in the soe ade,an evcuable file the aw gaph gmerate the to cases from the fow graph and the e Not a tirangle Not a trangs Not a tirangke Not atranle Not a tirangle Not a tirangle Not a tiane Poatsneccod Flow graph one niry,one esit 4 Test canes complete peogram lells you what type e a triangle you have ciangle. llesides wrting the uhele program, your tank is so All the ingas are inegers However, they migt b The fileco tanthe proper .sher ofenaies4.maiple Lincs that bgin with a"a coments, and sbould For the iction tha os te triangle pe, dothe Caloulate te cyclomanc complesity, and show how you yous and mine) Expected revals shall be documentiod Tum in the soe ade,an evcuable file the aw gaph gmerate the to cases from the fow graph and the e Not a tirangle Not a trangs Not a tirangke Not atranle Not a tirangle Not a tirangle Not a tiane Poatsneccod Flow graph one niry,one esit 4 Test canes complete

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database And Information Systems Research Advances

Authors: Azer Bestavros ,Victor Fay-Wolfe

1st Edition

1461377803, 978-1461377801

More Books

Students also viewed these Databases questions