Define a datamanipulation application for the books database
Define a data-manipulation application for the books database. The user should be able to edit existing data and add new data to the database. Allow the user to edit the database in the following ways:
Add a new author
Edit the existing information for an author
Add a new title for an author
Add a new entry in the AuthorISBN table to link authors with titles
Solution
// importing required number of packages
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
// main class where program start because main method is in this class
public class DM {
static final String DRIVE = \"sun.jdbc.odbc.JdbcOdbcDriver\"; // getting jdbc driver
static final String DB_URL = \"jdbc:odbc:book\"; // getting database
public static void main( String args[] ) { // starting of the application
Connection connection = null; // it will opens connection
Statement statement = null; // statement related to helo query to be asked
ResultSet resultSet = null; // results are stored
try { // connects to dB book and runs the query in the dB
Class.forName( DRIVE ); // giving driver class
connection = DriverManager.getConnection( DB_URL, \"jhtp7\", \"jhtp7\" ); // connecting to dB
statement = connection.createStatement(); // Statement is created to query in dB
resultSet = statement.executeQuery(\"SELECT authorID, firstName, lastName FROM authors\" ); // asking query using statement nad storing the reult in resultset
ResultSetMetaData metaData = resultSet.getMetaData(); // stores query results
int numOfCol = metaData.getColumnCount(); // getting number of columns to an integer
System.out.println( \"Authors Table of book dB :\ \" );
for ( int i = 1; i <= numOfCol; i++ )
System.out.printf( \"%-8s\\t\", metaData.getColumnName( i ) ); // getting every column name
System.out.println();
while ( resultSet.next() ) {
for ( int i = 1; i <= numOfCol; i++ ) // for every tuple in the result set
System.out.printf( \"%-8s\\t\", resultSet.getObject( i ) ); // print the key value
System.out.println();
} // end of the while
} // end of exception
catch ( SQLException sqlException ) { // when sql occurs
sqlException.printStackTrace();
} // end of the catch
catch ( ClassNotFoundException classNotFound ){ // when class exception occurs
classNotFound.printStackTrace();
} // end of one more catch
finally{ // making sure that resultSet, statement and connection are closed
try{
resultSet.close();
statement.close();
connection.close();
} // end of the try
catch ( Exception exception ) {
exception.printStackTrace(); // if not close/any problem, that will be reported here
} // end of catch
} // end of finally block
} // end of the main
} // end class the public DM (data manuplition)

