Adv Java Assignment 4
Adv Java Assignment 4
SOURCE CODE:
package jdbc;
import java.sql.*;
public class jdbcExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/notes";
// Database credentials
static final String USER = "root";
static final String PASS = "";
try{ Class.forName("com.mysql.jdbc.Driver");
System.out.println("**********DDL COMMANDS*********");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
System.out.println("Connected database successfully...");
System.out.println("Creating database...");
String sql1 = "CREATE TABLE REGISTRATION " + "(id INTEGER not NULL, " +
" first VARCHAR(255), " + " last VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql1);
System.out.println("Created table in given database...");
System.out.println("Deleting database...");
String sql2 = "DROP DATABASE STUDENTS_WORLD";
stmt.executeUpdate(sql2);
System.out.println("Database deleted successfully...");
System.out.println("******DML COMMANDS******");
System.out.println("Inserting records into the table...");
stmt.executeUpdate(sqlinsert);
sqlinsert = "INSERT INTO Registration " + "VALUES (102, 'ISHANI', 'BORA', 22)";
stmt.executeUpdate(sqlinsert);
sqlinsert = "INSERT INTO Registration " + "VALUES(103, 'ROHAN', 'SINGH', 22)";
stmt.executeUpdate(sqlinsert); System.out.println("Inserted records into the
table...");
String sqlupdate = "UPDATE Registration " + "SET age = 23 WHERE id in (100, 101)";
stmt.executeUpdate(sqlupdate);
sqlupdate = "SELECT id, first, last, age FROM Registration"; ResultSet rs1 =
stmt.executeQuery(sqlupdate); while(rs1.next()){
//Retrieve by column name int id = rs1.getInt("id");
int age = rs1.getInt("age");
String first = rs1.getString("first"); String last = rs1.getString("last");
//Display values System.out.print("ID: " + id); System.out.print(", Age: " + age);
System.out.print(", First: " + first); System.out.println(", Last: " + last);
System.out.println("Records Updated");
}
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
‘
OUTPUT:
import java.sql.*;
public class JDBCE {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/notes";
// Database credentials
static final String USER = "root";
static final String PASS = "";
ResultSet rs = stmt.executeQuery(sql);
//Display values
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
OUTPUT:
SOURCE CODE:
package jdbc;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
// getting connection
con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/notes", "root",
"");
System.out.println("Connection established successfully!");
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(fin!=null) fin.close(); //close file
if(prepStmt!=null) prepStmt.close(); //close PreparedStatement
if(con!=null) con.close(); // close connection
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OUTPUT:
CLOB:
package jdbc;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
// getting connection
con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/notes", "root",
"");
System.out.println("Connection established successfully!");
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(fr!=null)
fr.close(); //close reader
if(prepStmt!=null)
prepStmt.close(); //close PreparedStatement
if(con!=null) con.close(); // close connection
} catch (SQLException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
}
}
}
OUTPUT:
import java.sql.*;
public class batchUpdate {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/notes";
// Database credentials
static final String USER = "root";
static final String PASS = "";
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// Create preparedStatemen
System.out.println("Creating statement...");
stmt = conn.prepareStatement(SQL);
stmt.setInt( 1, 400 );
stmt.setString( 2, "Provu" );
stmt.setString( 3, "Singh" );
stmt.setInt( 4, 33 );
// Add it to the batch stmt.addBatch();
stmt.addBatch();
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
‘
}
System.out.println();
rs.close();
}//end printRows()
}//end JDBCExample
OUTPUT: