0% found this document useful (0 votes)
32 views33 pages

Correct

The document contains multiple Java code snippets demonstrating various programming concepts such as threading, GUI creation, database connectivity, and data structures. Key examples include a thread that prints letters, a GUI for employee information input, and operations on collections like LinkedLists and HashSets. Each code snippet serves as a practical example of Java programming techniques and functionalities.

Uploaded by

kamleshghodke94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views33 pages

Correct

The document contains multiple Java code snippets demonstrating various programming concepts such as threading, GUI creation, database connectivity, and data structures. Key examples include a thread that prints letters, a GUI for employee information input, and operations on collections like LinkedLists and HashSets. Each code snippet serves as a practical example of Java programming techniques and functionalities.

Uploaded by

kamleshghodke94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Slip -1a

class AlphaA extends Thread


{
public static void main(String[] args){

try {
for (char c = 'A'; c <= 'Z'; c++){
System.out.print(" " + c);
sleep(2000);
}
}

catch (Exception e) {
System.out.println(e);
}
}
}

Slip – 1b
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class EmployeeB extends JFrame implements ActionListener


{
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3,t4;
JButton b1,b2;

Connection con; Statement stm; ResultSet rs;

EmployeeB(){

setSize(150,300);
setVisible(true);
setLayout(new FlowLayout());

l1 = new JLabel("ENo");
l2 = new JLabel("EName");
l3 = new JLabel("Designation");
l4 = new JLabel("Salary");

t1 = new JTextField(20);
t2 = new JTextField(20);
t3 = new JTextField(20);
t4 = new JTextField(20);
b1 = new JButton("insert");
b2 = new JButton("Clear");
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(l4); add(t4);
add(b1); add(b2);

b1.addActionListener(this);
b2.addActionListener(this);

public void actionPerformed(ActionEvent a)


{
if(a.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
if(a.getSource()==b1)
{
jdbc();
}
}

public void jdbc(){


try {

Class.forName("org.postgres.Driver");
con =
DriverManager.getConnection("jdbc:postgres://localhost/DB","postgres","");
PreparedStatement pstm ;

int eno = Integer.parseInt(t1.getText());


String ename = t2.getText();
String des = t3.getText();
int sal = Integer.parseInt(t4.getText());

pstm = con.prepareStatement("insert into Employee values(? ? ? ?)");


pstm.setInt(1,eno);
pstm.setString(2,ename);
pstm.setString(3,des);
pstm.setInt(4,sal);

pstm.executeUpdate();

JOptionPane.showMessageDialog(this,"Submittted");

} catch (Exception e) {
System.out.println(e);
}
}

public static void main(String[] args) {

new EmployeeB();
}
}

Slip – 2a
import java.util.*;

public class FriendsA {

public static void main(String[] args) {

HashSet<String> h = new HashSet<String>();

Scanner sc = new Scanner(System.in);


System.out.print("Enter how many friends : ");
int n = sc.nextInt();
String F;

System.out.print("Enter names : ");


for(int i=0;i<n;i++)
{
F = sc.next();
h.add(F);
}
System.out.println("Hashset is : "+h);

LinkedList<String> l = new LinkedList<>(h);


Collections.sort(l);
System.out.println("Sorted Hashset is : "+l);
sc.close();
}
}
Slip- 4a
/*Write a Java program using Runnable interface to blink Text on the frame.*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class BlinkText extends JFrame implements ActionListener,Runnable


{
Thread t ;
JLabel lbldisp;
JButton btnblink;
BlinkText()
{
t = new Thread(this);
setTitle("Employe Details Form");
setSize(600,400); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

lbldisp = new JLabel("BLINKING TEXT");


lbldisp.setBounds(200,100,200,40);
add(lbldisp);

btnblink = new JButton("Start Blinking");


btnblink.setBounds(200,300,200,40);
add(btnblink);

btnblink.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btnblink)
{

t.start();
}
}
public void run()
{
int i = 0 ;
try
{
do
{
i++;
lbldisp.setText("BLINKING TEXT : - "+i);
t.sleep(2000);
}while(true);
}
catch(Exception e)
{
}
}
}

class Slip4A
{
public static void main(String[] args) throws Exception
{
BlinkText bt = new BlinkText();
try
{
do
{
bt.lbldisp.setText("");
Thread.sleep(2000);
}while(true);
}
catch(Exception e)
{
}

}
}

Slip -7b
import java.sql.*;
import java.util.*;

class slip3A
{
public static void main(String[] args) throws Exception
{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
System.out.println("Got Connection:-"+con);

Statement stmt1 = con.createStatement();


String query0 = "drop table product";
stmt1.executeUpdate(query0);

System.out.println("Table DROPED");
String query1 ="create table product(pid integer, pname text, price integer)";
stmt1.executeUpdate(query1);
System.out.println("Table Created");
stmt1.close();

String query2 ="insert into product values(?,?,?)";


PreparedStatement pstmt = con.prepareStatement(query2);
Scanner sr = new Scanner (System.in);

int n = 1 ,c = 0;
System.out.println("Enter Product Information to Insert ");
for(int i = 0 ;i<n;i++)
{
System.out.println("Enter Pid:-");
int id = sr.nextInt();
System.out.println("Enter Pname:-");
String name = sr.next();
System.out.println("Enter Pprice:-");
int price = sr.nextInt();
pstmt.setInt(1,id);
pstmt.setString(2,name);
pstmt.setInt(3,price);
pstmt.executeUpdate();
c = i+1;
System.out.println(c+" - Record Updated");
}

System.out.println("Display Product Details ");


Statement stmt2 = con.createStatement();
String query3="select * from product";
ResultSet rs = stmt2.executeQuery(query3);
System.out.println(rs);
System.out.println("Pid \t Pname \t Pprice");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getInt(3));
}

rs.close();
con.close();
}
}

Slip- 3b
import java.util.*;

public class LinkOperA {

public static void main(String[] args) {

LinkedList<String> l = new LinkedList<>();


Scanner sc=new Scanner(System.in);
System.out.print("ENTER 3 STRING : ");
String s;
for(int i=0;i<3;i++){
s=sc.next();
l.add(s);
}

System.out.println(l);

while(true){
System.out.println("\n1. add element at the end ");
System.out.println("2. Delete 1st element in the list ");
System.out.println("3. Display the content of list in reverse order ");
System.out.print("Enter Choice : ");
int ch = sc.nextInt();

switch(ch) {
case 1:
System.out.println("Enter String t add at the end ");
s=sc.next();
l.addLast(s);
System.out.println(l);
break;

case 2:
System.out.println("After delete the 1st element : ");
l.remove(0);
System.out.println(l);
break;

case 3:
System.out.println("Content in reverse : ");

Iterator i = l.descendingIterator();
while (i.hasNext()) {
System.out.println(i.next());
}
break;

default: System.exit(0);
}
}
}
}
Slip - 4b
import java.util.*;

public class city {

public static void main(String[] args) {

Hashtable<String, Integer> h = new Hashtable<>();


Scanner sc = new Scanner(System.in);

String c;
int n;

while(true)
{
System.out.println("\n1. Add a city and its STD code ");
System.out.println("2. Remove city ");
System.out.println("3. search city and display its STD code ");
System.out.println("4. show ");
System.out.print("Enter choice : ");
int ch = sc.nextInt();

switch(ch)
{
case 1:
System.out.print("\nEnter City name : ");
c = sc.next();
System.out.print("Enter its STD code : ");
n = sc.nextInt();
h.put(c,n);
break;

case 2:
System.out.print("\nEnter City name : ");
c = sc.next();
if(h.containsKey(c))
{
h.remove(c);
}
else {
System.out.println("City is not present in collection ");
}
break;

case 3:
System.out.print("Enter City name : ");
c = sc.next();
if(h.containsKey(c))
{
System.out.println("\nCITY : "+c+" STD CODE : "+h.get(c));
}
else {
System.out.println("\nCity is not present in collection ");
}
break;

case 4: System.out.println(h);
break;

default: System.exit(0);
break;
}
sc.close();
}
}
}

Slip – 5a
import java.util.*;

public class StudInfo {

public static void main(String[] args) {

Hashtable<Integer, String> h = new Hashtable<>();


Scanner sc = new Scanner(System.in);

System.out.print("Enter How many Students : ");


int n = sc.nextInt();
int mn , i;
String s;
Object key;
for(i=0;i<n;i++)
{
System.out.print("\nMNO "+i+" : ");
mn = sc.nextInt();
System.out.print("StudName : ");
s = sc.next();

h.put(mn, s);
}

System.out.println("Student details are : ");


System.out.println("mno\tS_name");

Enumeration e = h.keys();
while(e.hasMoreElements())
{
key = e.nextElement();
System.out.println(key +"\t"+ h.get(key));
}
}
}
Slip – 29b
import java.util.*;

public class LinkedOper {

public static void main(String[] args) {

LinkedList<Integer> l = new LinkedList<>();

Scanner sc = new Scanner(System.in);


System.out.print("Enter Size of list : ");
int n = sc.nextInt();

int i,p;
System.out.println("Enter Number 1 by 1 : ");
for(i=0;i<n;i++)
{
p = sc.nextInt();
l.add(p);
}

while(true)
{
System.out.println("\n 1. Add element at 1st pos ");
System.out.println("2. Delete last Element ");
System.out.println("3. Display the size of Link list");
System.out.println("4. Display Link list");
System.out.print("Enter your Choice");
int ch = sc.nextInt();

switch(ch)
{
case 1:
System.out.print("Enter No. to add at 1st pos : ");
p = sc.nextInt();
l.addFirst(p);
break;

case 2:
l.removeLast();
System.out.println("last element is deleted ");
break;

case 3:
System.out.println("Size of list is "+ l.size());
break;

case 4:
Iterator m = l.iterator();
while(m.hasNext()){
System.out.print(" "+m.next());
}
break;
default: System.exit(0);
}
}
}
}

Slip – 6a
import java.util.*;

public class CollFrameA {


public static void main(String[] args) {

TreeSet<Integer> t = new TreeSet<>();


Scanner sc = new Scanner(System.in);

System.out.print("How many No. : ");


int n = sc.nextInt();

int num , s_num;


for (int i=0; i<n; i++)
{
num = sc.nextInt();
t.add(num);
}

System.out.println("SORTED ELEMENTS : ");


System.out.println(t);

System.out.println("Enter num to search : ");


s_num = sc.nextInt();

if(t.contains(s_num)){
System.out.println("Number is present. ");
}
else
System.out.println("Number is not present. ");
}
}
Slip- 7a
import java.util.*;

class Square extends Thread{

int num;
Square(int n)
{
num = n;
}

public void run(){


System.out.println("Square is : " + (num*num));
}
}

class Cube extends Thread{

int num;
Cube(int n)
{
num = n;
}

public void run(){


System.out.println("Cube is : " + (num*num*num));
}
}

public class MulT extends Thread {

public void run(){


Random r = new Random();
int n ;

try {
while(true)
{
n = r.nextInt(100);
System.out.println("Num : "+n);
if(n%2==0)
{
Square s = new Square(n);
s.start();
}
else{
Cube c = new Cube(n);
c.start();
}
Thread.sleep(2000);
}
} catch (Exception e) {
System.out.println(e);
}
}

public static void main(String[] args) {

MulT t = new MulT();


t.start();
}
}

Slip – 8a
import java.util.*;

class nthread extends Thread{

public String s;
public int n;

nthread(String s , int n)
{
this.s=s;
this.n=n;
}

public void run()


{
try{
for(int i=0;i<n;i++)
{
System.out.println(s);
//sleep(1000);
}}
catch(Exception e){}
}
}

public class thread1{

public static void main(String[] args)


{
nthread t1 = new nthread("COVID-19",10);
nthread t2 = new nthread("LOCKDOWN2020",20);
nthread t3 = new nthread("VACCINATED2021",30);

t1.start();
t2.start();
t3.start();
}
}
Slip – 16a
import java.util.*;

public class Tre {

public static void main(String[] args) {

TreeSet<String> t = new TreeSet<>();

Scanner sc = new Scanner(System.in);


System.out.print("Enter how many colors : ");
int n = sc.nextInt();

int i;
String c;

System.out.println("Enter Colors 1 by 1 : ");


for(i=0;i<n;i++)
{
c = sc.next();
t.add(c);
}

System.out.println(t);
}
}

Slip -9a
/*
Write a Java program to create a thread for moving a ball inside a panel vertically. The
ball should be created when the user clicks on the start button
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Ball extends JFrame implements Runnable,ActionListener


{
Thread t;
int x = 100,y=100;
JButton btn;
Ball()
{
setTitle("Thread - Moving Ball");
setSize(600,600); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
btn = new JButton("START");
btn.setBounds(250,500,100,40);
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn)
{
t = new Thread(this);
t.start();
}
}
public void run()
{
try
{
while(true)
{
x = x + 20 ; y= y + 20 ;
repaint();
t.sleep(2000);
}
}
catch(Exception e)
{

}
}
public void paint(Graphics g)
{
g.drawOval(x,y,20,20);
}
}
class Slip9A
{
public static void main(String[] args) throws Exception
{
Ball b = new Ball();
}
}
Slip- 17a
import java.util.*;

public class LinkPro {

public static void main(String[] args) {

TreeSet<Integer> t = new TreeSet<>();

Scanner sc = new Scanner(System.in);


System.out.print("Enter Size of list : ");
int n = sc.nextInt();

int i,p;
System.out.println("Enter Number 1 by 1 : ");
for(i=0;i<n;i++)
{
p = sc.nextInt();
t.add(p);
}

System.out.println(t);
}
}

Slip – 15a, 18a


public class slip10A
{
public static void main(String arg[])
{
Thread t=Thread.currentThread();
System.out.println("Current Thread:"+t); //current Thread

t.setName("Slip15A Thread "); //name


System.out.println ("After Changing Name:"+t);
try
{
for(int i=2;i>0;i--) //priority of thread
{
System.out.println(i);
Thread.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Slip-10b
/*
Write a Java program to display first record from student table (RNo, SName, Per) onto the
TextFields by clicking on button. (Assume Student table is already created).
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

class Slip10B extends JFrame implements ActionListener


{
JLabel lblrno,lblname,lblper;
JTextField tfrno,tfname,tfper;
JButton btnclear,btnfind;
Slip10B()
{
setTitle("Studnet Table 1st Record");
setSize(400,400); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4,2));

lblrno = new JLabel("Roll No");


lblname = new JLabel("Name");
lblper = new JLabel("Percentage");

tfrno = new JTextField(20);


tfname = new JTextField(20);
tfper = new JTextField(20);

btnclear = new JButton("Clear");

btnfind = new JButton("Get Record");


btnfind.addActionListener(this);

add(lblrno); add(tfrno);
add(lblname); add(tfname);
add(lblper); add(tfper);
add(btnclear); add(btnfind);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btnfind)
{
jdbc();
}
}
void jdbc()
{
try
{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
JOptionPane.showMessageDialog
(this,"Got Connection:-"+con);

Statement stmt = con.createStatement();


String query = "select * from student";
ResultSet rs = stmt.executeQuery(query);
rs.next();
tfrno.setText(""+rs.getInt(1));
tfname.setText(""+rs.getString(2));
tfper.setText(""+rs.getDouble(3));

}
catch(Exception e)
{
}
}
public static void main(String[] args)
{
new Slip10B();
}
}

Slip -,13b
import java.util.*;

class slip11A extends Thread


{
String name ; int i = 0;
slip11A(String n)
{
name = n;
System.out.println(name+"- Created");
start();
}

public void run()


{
System.out.println(name+"- Started");
try
{
while(true)
{
i++;
System.out.println(name+"- Running for "+i+"th time");
Random r = new Random();
int st = r.nextInt(5000);
System.out.println(name+"- will Sleep for "+st+" mili-seconds");
long t = (long)st;
sleep(t);
}
} catch(Exception e){}
}
public static void main(String[] args)
{
slip11A t = new slip11A("My Slepy Thread");
}
}

Slip – 11b , 29a


import java.sql.*;
class Slip11B
{
public static void main(String[] arg) throws Exception
{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/Slips","postgres","");
Statement stmt = con.createStatement();

String sql = "Select * from doner";


ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println(rsmd);

int cc = rsmd.getColumnCount();
System.out.println("Doner Table Column Count-"+cc);

for(int i =1;i<=cc;i++)
{
System.out.println("Column No:-"+i);
System.out.println("Column Name:-"+rsmd.getColumnName(i));
System.out.println("Column Type:-"+rsmd.getColumnType(i));
}
}
}
Slip -19a
import java.util.*;

public class LinkNega {

public static void main(String[] args) {

LinkedList<Integer> l = new LinkedList<>();

Scanner sc = new Scanner(System.in);


System.out.print("Enter Size of list : ");
int n = sc.nextInt();

int i,p;
System.out.println("Enter Number 1 by 1 : ");
for(i=0;i<n;i++)
{
p = sc.nextInt();
l.add(p);
}

System.out.println(l);

System.out.println("Negative Numbers are : ");


for( int num : l ){
if(num<0)
System.out.print(" "+num);
}
}
}

Slip – 12b
/*
Write a Java Program to create a PROJECT table with field’s project_id, Project_name,
Project_description, Project_Status. Insert values in the table. Display all the details
of the PROJECT table in a tabular format on the screen.(using swing).
*/
import java.awt.*;
import javax.swing.*;
import java.sql.*;

class slip12B extends JFrame


{
JTable tbl; int rc = 0 , cc = 0;
slip12B()
{
setTitle("Studnet Table 1st Record");
setSize(400,400); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tbl = new JTable(10,4);


add(tbl);
tbl.setValueAt("Proj Id",rc,cc);cc++;
tbl.setValueAt("Proj Name",rc,cc);cc++;
tbl.setValueAt("Proj Description",rc,cc);cc++;
tbl.setValueAt("Proj Status",rc,cc);cc++;
jdbc();
}
void jdbc()
{
try
{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
JOptionPane.showMessageDialog
(this,"Got Connection:-"+con);

Statement stmt = con.createStatement();


String query = "select * from project";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
rc++;
cc = 0 ;
tbl.setValueAt(""+rs.getInt(1),rc,cc);cc++;
tbl.setValueAt(""+rs.getString(2),rc,cc);cc++;
tbl.setValueAt(""+rs.getString(3),rc,cc);cc++;
tbl.setValueAt(""+rs.getString(4),rc,cc);cc++;
}
}
catch(Exception e)
{
}
}
public static void main(String[] args)
{
new slip12B();
}
}
Slip – 13a
import java.sql.*;

class DataBaseMetaData
{
public static void main(String args[])
{
Connection con=null;;
Statement stm = null;
ResultSet rs = null;

try{
Class.forName("org.postgresql.Driver");
//System.out.print("\ndriver loaded");

con =
DriverManager.getConnection("jdbc:postgresql://localhost/te021","postgres","postgres");
//System.out.println("\nConnection Establish");
stm=con.createStatement();

DatabaseMetaData db = con.getMetaData(); //database info

System.out.println("DATABASE NAME : "+db.getDriverName());


System.out.println("DATABASE VERSION : "+db.getDriverVersion());
System.out.println("DATABASE PRODUCT NAME : "+db.getDatabaseProductName());
System.out.println("DATABASE PRODUCT VERSION
: "+db.getDatabaseProductVersion());
System.out.println("USERNAME : "+db.getUserName());
System.out.println("CATALOGS : "+db.getCatalogs());

rs = db.getTables(null,null,null,new String[]{"TABLE"}); //to get tables


name
System.out.println("\n TABLES\n---------------");
while(rs.next())
{ // we can also display through index i.e.
System.out.println(rs.getString("TABLE_NAME")); // rs.getString(3)
}

}
catch(Exception e)
{
System.out.println("\nError : "+e);
}

}
}
Slip – 17b
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class slip17B extends JFrame implements ActionListener,Runnable


{
Thread t ;
JTextField tfdisp;
JButton btnstart;
slip17B()
{
t = new Thread(this);
setTitle("Slip17B Thread - TextField Update");
setSize(600,400); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
tfdisp = new JTextField(20);
tfdisp.setBounds(200,100,200,40);
tfdisp.setText("Random No-");
add(tfdisp);

btnstart = new JButton("Start");


btnstart.setBounds(200,300,200,40);
add(btnstart);

btnstart.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)


{
if(ae.getSource()==btnstart)
{

t.start();
}
}

public void run()


{
Random r = new Random();
try
{
do
{
int no = r.nextInt(100);
tfdisp.setText("Random No- "+no);
t.sleep(2000);
}while(true);
}
catch(Exception e)
{
}

public static void main(String[] args) throws Exception


{
slip17B utf = new slip17B();
}
}

Slip – 22a
import java.util.*;
import java.sql.*;

class slip18B
{
public static void main(String[] args) throws Exception
{
int eid,esal; String ename;
Scanner sr = new Scanner(System.in);
Class.forName("org.postgresql.Driver");
System.out.println("Driver Loaded And Registered");

Connection con = DriverManager.getConnection


("jdbc:postgresql://localhost/slips","postgres","");
System.out.println("Got Connection :-"+con);
int op=0;

do
{
System.out.println
("Menu\n1. Insert \n2. Update \n3. Display \n4. Exit \nEnter Choice.");
int ch = sr.nextInt();
switch(ch)
{
case 1 :
String query = "insert into emp1 values(?,?,?)";
PreparedStatement pstmt = con.prepareStatement(query);
System.out.println("Emp id:-");
eid = sr.nextInt();

System.out.println("Emp name:-");
ename = sr.next();

System.out.println("Emp Sal:-");
esal = sr.nextInt();

pstmt.setInt(1,eid);
pstmt.setString(2,ename);
pstmt.setInt(3,esal);
pstmt.executeUpdate();

System.out.println("Record Inserted");
break;

case 2 :
//updating sal from id
String query2 = "update emp1 set esal=? where eid=?";
PreparedStatement pstmt2 = con.prepareStatement(query2);
System.out.println("Emp id to update :-");
eid = sr.nextInt();

System.out.println("Emp updated Sal:-");


esal = sr.nextInt();

pstmt2.setInt(2,eid);
pstmt2.setInt(1,esal);

System.out.println(pstmt2);
pstmt2.executeUpdate();
System.out.println("Record Updated");
break;

case 3 :
String query3 = "Select * from emp1";
PreparedStatement pstmt3 = con.prepareStatement(query3);
ResultSet rs = pstmt3.executeQuery();
System.out.println("Emp-id \t Emp-name \t Emp-sal");

while(rs.next())
{
System.out.println
(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getInt(3));
}
break;

case 4 :
con.close();
System.exit(0);
break;

default : System.out.println
("Enter Choice between 1 to 4 ");
}

System.out.println
("Enter \n0. To Exit \n1. To Continue");
op = sr.nextInt();
}

while(op != 0);
}
}
Slip- 21a
import java.util.*;
class slip21A
{
public static void main(String args[])
{
int i,n;
String name;
LinkedList<String> list = new LinkedList<>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of subjects: ");
n=sc.nextInt();
System.out.println("Enter name of the subject: ");

for(i=0;i<n;i++)
{
name=sc.next();
list.add(name);
}

Iterator t=list.iterator();
System.out.println("sub names: ");
while(t.hasNext())
{
System.out.println(t.next());
}

}
}

Slip
import java.util.*; -23b
public class cmdN {
public static void main(String[] args) {

int i;
LinkedList<String> t = new LinkedList<>();

for(i=0;i<args.length;i++)
{
t.add(args[i]);
}

System.out.println("Using Iterator : ");


Iterator j = t.iterator();
while(j.hasNext())
{
System.out.println(j.next());
}

System.out.println("Using ListIterator : ");


ListIterator l = t.listIterator();
while(l.hasNext())
{
System.out.println(l.next());
}
}
}

Slip–23a
import java.util.*;

public class StrVow {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("ENter String : ");
String s = sc.next();

int n = s.length();
char ch;

try {
System.out.println("Vowels are");
for(int i=0; i < n ; i++)
{
ch = s.charAt(i);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' ||
ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
System.out.println(" "+ch);
Thread.sleep(2000);
}
}
} catch (Exception e) {}
}
}
Slip- 26a
import java.sql.*;
class Slip24B
{
public static void main(String[] args) throws Exception
{
int id = Integer.parseInt(args[0]);
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
System.out.println("Got Connection:-"+con);

String query = "delete from emp1 where eid = ?";


PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1,id);
System.out.println("Query:-"+pstmt);
pstmt.executeUpdate();
System.out.println("Query:- executed");
}
}

Slip – 27a
/*
Write a Java Program to display the details of College (CID, CName, address, Year) on
JTable.
*/
import java.awt.*;
import javax.swing.*;
import java.sql.*;

class Slip25A extends JFrame


{
JTable tbl; int rc = 0 , cc = 0;
Slip25A()
{
setTitle("College Table Record");
setSize(400,400); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tbl = new JTable(10,4);


add(tbl);
tbl.setValueAt("Id",rc,cc);cc++;
tbl.setValueAt("Name",rc,cc);cc++;
tbl.setValueAt("Address",rc,cc);cc++;
tbl.setValueAt("Year",rc,cc);cc++;
jdbc();
}
void jdbc()
{
try
{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
JOptionPane.showMessageDialog
(this,"Got Connection:-"+con);

Statement stmt = con.createStatement();


String query = "select * from college";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
rc++;
cc = 0 ;
tbl.setValueAt(""+rs.getInt(1),rc,cc);cc++;
tbl.setValueAt(""+rs.getString(2),rc,cc);cc++;
tbl.setValueAt(""+rs.getString(3),rc,cc);cc++;
tbl.setValueAt(""+rs.getInt(4),rc,cc);cc++;
}
}
catch(Exception e)
{
}
}
public static void main(String[] args)
{
new Slip27A();
}
}

Slip -25b

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

class Slip25B extends JFrame implements ActionListener


{
JLabel lbl;
JTextField tfquery;
JButton btnclear,btncreate,btnalter,btndrop;
Connection con ;
Slip25B()
{
setTitle("Database DDL GUI");
setSize(1000,200); setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,3));
lbl = new JLabel("Type your DDL query Here");

tfquery = new JTextField(50);

btnclear = new JButton("Clear");


btncreate = new JButton("CREATE");
btnalter = new JButton("ALTER");
btndrop = new JButton("DROP");

btnclear.addActionListener(this);
btncreate.addActionListener(this);
btnalter.addActionListener(this);
btndrop.addActionListener(this);

add(lbl); add(tfquery); add(btnclear);


add(btncreate); add(btnalter); add(btndrop);

jdbc();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btnclear)
{
tfquery.setText("");
}
else if (ae.getSource()==btncreate)
{
jdbc_create();
}
else if(ae.getSource()==btnalter)
{
jdbc_alter();
}
else if(ae.getSource()==btndrop)
{
jdbc_drop();
}
}
void jdbc()
{
try
{
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
JOptionPane.showMessageDialog
(this,"Got Connection:-"+con);
}
catch(Exception e)
{
}
}
void jdbc_create()
{
try
{
// JOptionPane.showMessageDialog (this,"Created Button Clicked");

Statement stmt1 = con.createStatement();


String query1 = tfquery.getText();
stmt1.executeUpdate(query1);
JOptionPane.showMessageDialog
(this,"Table Created");

}
catch(Exception e)
{
JOptionPane.showMessageDialog
(this,"Create :-"+e);

}
}
void jdbc_alter()
{
try
{
JOptionPane.showMessageDialog
(this,"Alter Button Clicked");
//alter table tbl_name alter column col_name set data type new_datatype;
Statement stmt2 = con.createStatement();
String query2 = tfquery.getText();
stmt2.executeUpdate(query2);
JOptionPane.showMessageDialog
(this,"Table Altered");

}
catch(Exception e)
{
JOptionPane.showMessageDialog
(this,"Alter :-"+e);

}
}
void jdbc_drop()
{
try
{
JOptionPane.showMessageDialog
(this,"Drop Button Clicked");
Statement stmt3 = con.createStatement();
String query3 = tfquery.getText();
stmt3.executeUpdate(query3);
JOptionPane.showMessageDialog
(this,"Table Droped");

}
catch(Exception e)
{
JOptionPane.showMessageDialog
(this,"Alter :-"+e);

}
}

public static void main(String[] args)


{
new Slip25B();
}
}

Slip – 30b
/*
Write a Java Program for the implementation of scrollable ResultSet. Assume Teacher table
with attributes (TID, TName, Salary) is already created.
*/

import java.sql.*;
class Slip30B
{
public static void main(String[] args) throws Exception
{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
System.out.println("Got Connection:-"+con);

String query = "select * from teacher1";


Statement stmt = con.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(query);
System.out.println("ID\tName\tSALARY");
while(rs.next())
{
System.out.println
(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getInt(3));
}
}
}
Slip – 16b

import java.sql.*;
import java.util.*;

class Slip16B
{
public static void main(String[] args) throws Exception
{
Scanner sr = new Scanner(System.in);
Class.forName("org.postgresql.Driver");
System.out.println("Driver Loaded And Registered");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost/slips","postgres","");
System.out.println("Got Connection :-"+con);

String query = "insert into teacher values(?,?,?)";


PreparedStatement pstmt = con.prepareStatement(query);
System.out.println("Enter total no Techer records to insert:-");
int nor = sr.nextInt() ;
for(int i = 0; i < nor ;i++)
{
System.out.println("Teacher id:-");
int tid = sr.nextInt();
System.out.println("Teacher name:-");
String tname = sr.next();
System.out.println("Teaching subject name:-");
String sname = sr.next();
pstmt.setInt(1,tid);
pstmt.setString(2,tname);
pstmt.setString(3,sname);
pstmt.executeUpdate();
System.out.println("Record Inserted");
}

String query2 = "Select * from teacher where sname=?";


PreparedStatement pstmt2 = con.prepareStatement(query2);
System.out.println("Enter subject name:-");
String subname = sr.next();
pstmt2.setString(1,subname);
ResultSet rs = pstmt2.executeQuery();
System.out.println("Teacher-id \t Teacher-name \t Subject");
while(rs.next())
{
System.out.println
(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}

con.close();
}
}

You might also like