Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Advanced java practical file, Study Guides, Projects, Research of Java Programming

Final year btech advanced java practical file

Typology: Study Guides, Projects, Research

2017/2018

Uploaded on 12/16/2018

jyoti-negi-1
jyoti-negi-1 🇮🇳

2 documents

Partial preview of the text

Download Advanced java practical file and more Study Guides, Projects, Research Java Programming in PDF only on Docsity!

INDEX

Sr. No. Program Description

Page

No.

Signature

To create a database through

JDBC.

To create a table in given database

through JDBC.

To insert data in a given table

through JDBC.

To delete data from a given table

through JDBC.

To delete a table from a given

database through JDBC.

To delete a database through

JDBC.

Aim: - To create a database through JDBC.

PRACTICAL – 01

Aim: - To create a database through JDBC.

Program Code:-

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"); } }

Aim: - To create a table in given database through JDBC.

PRACTICAL – 02

Aim: - To create a table in given database through JDBC.

Program Code:-

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"); } }

Aim: - To insert data in a given table through JDBC.

PRACTICAL – 03

Aim: - To insert data in a given table through JDBC.

Program Code:-

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"); } }

Aim: - To delete data from a given table through JDBC.

PRACTICAL – 04

Aim: - To delete data from a given table through JDBC.

Program Code:-

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"); } }

Aim: - To delete a table from a given database through JDBC.

PRACTICAL – 05

Aim: - To delete a table from a given database through JDBC.

Program Code:-

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);