Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Final year btech advanced java practical file
Typology: Study Guides, Projects, Research
1 / 35
PRACTICAL – 01
import java.sql.* ; public class CreateDatabaseJDBC { static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/"; static final String USER = "mogambo"; static final String PASS = "Qwerty@1234"; public static void main(String args[]){ Connection conn = null; Statement stmt = null; try{ Class.forName(JDBC_DRIVER); System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Creating database..."); stmt = conn.createStatement();
String sql = "CREATE DATABASE UIIT"; stmt.executeUpdate(sql); System.out.println("Database created successfully..."); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ se2.printStackTrace(); } try{ if(conn!=null) conn.close(); }catch(SQLException se3){ se3.printStackTrace(); } } System.out.println("All resources closed"); } }
PRACTICAL – 02
import java.sql.*; public class CreateTableJDBC { static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/UIIT"; static final String USER = "mogambo"; static final String PASS = "Qwerty@1234"; public static void main(String args[]){ Connection conn = null; Statement stmt = null; try{ Class.forName(JDBC_DRIVER); System.out.println("Connecting to selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully..."); System.out.println("Creating table in given database..."); stmt = conn.createStatement(); String sql = "CREATE TABLE STUDENT "+ "(roll_num INTEGER not NULL, "+ "first_name VARCHAR(255), "+ "last_name VARCHAR(255), "+ "age INTEGER, "+ "PRIMARY KEY (roll_num))"; stmt.executeUpdate(sql); System.out.println("Created table in given database..."); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ se2.printStackTrace(); } try{ if(conn!=null)
conn.close(); }catch(SQLException se3){ se3.printStackTrace(); } } System.out.println("All resources closed"); } }
PRACTICAL – 03
import java.sql.*; public class InsertDataJDBC { static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/UIIT"; static final String USER = "mogambo"; static final String PASS = "Qwerty@1234"; public static void main(String args[]){ Connection conn = null; Statement stmt = null; try{ Class.forName(JDBC_DRIVER); System.out.println("Connecting to selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully..."); System.out.println("Inserting data into the table..."); stmt = conn.createStatement(); String sql = "INSERT INTO STUDENT "+ "VALUES (63001, 'Sidharth', 'Sood', 20)"; stmt.executeUpdate(sql); sql = "INSERT INTO STUDENT "+ "VALUES (63002, 'Manoj', 'Thakur', 22)"; stmt.executeUpdate(sql); sql = "INSERT INTO STUDENT "+ "VALUES (63049, 'Bharat', 'Singh', 21)"; stmt.executeUpdate(sql); System.out.println("Data inserted into the table..."); sql = "SELECT roll_num,first_name,last_name,age FROM STUDENT"; ResultSet rs =stmt.executeQuery(sql); while(rs.next()){ int roll_num = rs.getInt("roll_num"); int age = rs.getInt("age"); String first_name = rs.getString("first_name"); String last_name = rs.getString("last_name");
System.out.print("Roll No.: "+roll_num); System.out.print(", Age: "+age); System.out.print(", First Name: "+first_name); System.out.println(", Last Name: "+last_name); } rs.close(); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ se2.printStackTrace(); } try{ if(conn!=null) conn.close(); }catch(SQLException se3){ se3.printStackTrace(); } } System.out.println("All resources closed"); } }
PRACTICAL – 04
import java.sql.*; public class DeleteDataJDBC { static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/UIIT"; static final String USER = "mogambo"; static final String PASS = "Qwerty@1234"; public static void main(String args[]){ Connection conn = null; Statement stmt = null; try{ Class.forName(JDBC_DRIVER); System.out.println("Connecting to selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully..."); System.out.println("Deleting data from the table..."); stmt = conn.createStatement(); String sql = "DELETE FROM STUDENT "+ "WHERE roll_num = 63049"; stmt.executeUpdate(sql); System.out.println("Data deleted from the table..."); sql = "SELECT roll_num,first_name,last_name,age FROM STUDENT"; ResultSet rs =stmt.executeQuery(sql); while(rs.next()){ int roll_num = rs.getInt("roll_num"); int age = rs.getInt("age"); String first_name = rs.getString("first_name"); String last_name = rs.getString("last_name"); System.out.print("Roll No.: "+roll_num); System.out.print(", Age: "+age); System.out.print(", First Name: "+first_name); System.out.println(", Last Name: "+last_name); } rs.close();
}catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ se2.printStackTrace(); } try{ if(conn!=null) conn.close(); }catch(SQLException se3){ se3.printStackTrace(); } } System.out.println("All resources closed"); } }
PRACTICAL – 05
import java.sql.*; public class DropTableJDBC { static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/UIIT"; static final String USER = "mogambo"; static final String PASS = "Qwerty@1234"; public static void main(String args[]){ Connection conn = null; Statement stmt = null; try{ Class.forName(JDBC_DRIVER); System.out.println("Connecting to selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS);