Академический Документы
Профессиональный Документы
Культура Документы
Create an array
of objects in Java and display objects with cgpa >8.
b. Write a Java program using JDBC, which handles insert and display operations with the student class
mentioned in Question number 3.a.
JDBC Insert:
import java.sql.*;
// Database credentials
try{
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.executeUpdate(sql);
stmt.executeUpdate(sql);
stmt.executeUpdate(sql);
}catch(SQLException se){
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
JDBC Disaplay:
import java.sql.*;
// Database credentials
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Creating statement...");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
//Display values
rs.close();
}catch(SQLException se){
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample