java2 solution set pdf
java2 solution set pdf
import java.util.logging.*;
try
{
Thread.sleep(2000);
}
catch (InterruptedException ex)
{
t.start();
}
}
/*
Slip no 2 Write a Java program to accept the details of
Employee (Eno, EName, Designation,Salary) from a user and
store it into the database. (Use Swing)
*/
package com.mycompany.prac1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.*;
import java.util.logging.*;
import javax.swing.*;
class EmpApp {
private JFrame frame;
private JTextField eno, ename, desig, sal;
private JButton clear, insert;
frame.add(new JLabel("Eno."));
frame.add(eno);
frame.add(new JLabel("EName"));
frame.add(ename);
frame.add(new JLabel("Designation"));
frame.add(desig);
frame.add(new JLabel("Salary"));
frame.add(sal);
Connection conn =
DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres",
"postgres", "bhalchandra");
insert.addActionListener((ActionEvent e) -> {
try {
insertEmp(conn, eno, ename, desig, sal);
} catch (IOException | SQLException ex) {
Logger.getLogger(EmpApp.class.getName()).log(Level.SEVERE,
null, ex);
}
});
clear.addActionListener((ActionEvent e) -> {
eno.setText("");
ename.setText("");
desig.setText("");
sal.setText("");
});
frame.add(insert);
frame.add(clear);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/*
Slip no 2 Q1 Write a java program to read ‘N’ names of your
friends, store it into HashSet and
display them in ascending order.
*/
package com.mycompany.practical_slip;
import java.util.*;;
public class slip2_1
{
public static void main(String[] args)
{
HashSet<String> friends = new HashSet<>();
scan.nextLine();
for(int i = 0 ; i<n;i++)
{
System.out.println("Enter name :");
String name = scan.nextLine();
friends.add(name);
}
/*
Slip no 3 Q1. Write a JSP program to display the details of Patient (PNo,
PName, Address, age,
disease) in tabular form on browser*/
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Patient</h1>
<table border="1">
<tr>
<th>PNo</th>
<th>PName</th>
<th>Address</th>
<th>age</th>
<th>disease</th>
</tr>
<tr>
<td>1</td>
<td>John</td>
<td>xyz</td>
<td>45</td>
<td>kovid</td>
</<tr>
<tr>
<td>2</td>
<td>Brock</td>
<td>abc</td>
<td>48</td>
<td>canser</td>
</<tr>
</table>
</body>
</html>
*/
/*
Slip no 3 Q2. Write a Java program to create LinkedList of String objects
and perform the following:
i. Add element at the end of the list
ii. Delete first element of the list
iii. Display the contents of list in reverse order
*/
package com.mycompany.javaslip;
import java.util.*;
int ch;
do {
System.out.println("Menu");
System.out.println("1. Insert at tail");
System.out.println("2. Delete head.");
System.out.println("3. Display in reverse");
System.out.println("4. Exit");
System.out.println("------------------------------");
System.out.println("Enter your choice:");
ch = sc.nextInt();
sc.nextLine();
System.out.println();
switch (ch) {
case 1:
System.out.println("Enter name.");
names.add(sc.nextLine());
break;
case 2:
names.remove();
break;
case 3:System.out.println("Real order");
Iterator itr = names.iterator();
while (it.hasNext())
{
System.out.println(itr.next());
}
Iterator it = names.descendingIterator();
while (it.hasNext())
{
System.out.println(it.next());
break;
default:
System.out.println("Invalid choice.");
}
System.out.println("-------------------------------");
} while (ch != 4);
}
}
/*
Slip no 4 Q1 Write a Java program using Runnable interface to blink Text
on the JFrame (Use
Swing)
*/
package com.mycompany.practical_slip;
import java.awt.Color;
import java.util.Random;
import javax.swing.*;
public BlinkText() {
frame = new JFrame("Blink Light");
frame.setSize(200, 200);
blink = new JLabel("Blink");
frame.add(blink);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void run() {
Random rand = new Random();
while(true) {
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
blink.setForeground(new Color(r, g, b));
}
}
}
import java.util.*;
int ch;
String code, city;
do {
System.out.println("Menu");
System.out.println("1. Add City and std code.(no
duplicates)");
System.out.println("2. Remove City.");
System.out.println("3. Search city name dsiplay std code");
System.out.println("4. Exit");
System.out.println("------------------------------");
System.out.println("Enter your choice:");
ch = sc.nextInt();
sc.nextLine();
System.out.println();
switch(ch) {
case 1: System.out.println("Enter std code.");
code = sc.nextLine();
System.out.println("Enter City.");
city = sc.nextLine();
cityMap.put(code, city);
break;
case 2: System.out.println("Enter std code.");
code = sc.nextLine();
cityMap.remove(code);
break;
case 3: System.out.println("Enter city:");
city = sc.nextLine();
code = null;
for(Map.Entry<String, String> map :
cityMap.entrySet()) {
if(map.getValue().equals(city))
code = map.getKey();
}
if(code != null)
System.out.println("Code is " + code);
else
System.out.println("Not found.");
break;
default: System.out.println("Invalid choice.");
}
System.out.println("-------------------------------");
} while(ch != 4);
}
}
/*
Slip no5 Q1. Write a Java Program to create the hash table that will
maintain the mobile number and
student name. Display the details of student using Enumeration interface
*/
package com.mycompany.javaslip;
import java.util.*;
studentTable.put("1234567890", "john");
studentTable.put("1239874560", "carry");
/*
slip no 6 Q1 Write a Java program to accept ‘n’ integers from the user
and store them in a Collection.
Display them in the sorted order. The collection should not accept
duplicate elements.
(Use a suitable collection). Search for a particular element using
predefined search
method in the Collection framework
*/
package com.mycompany.practical_slip;
import java.util.*;
System.out.println(nums);
/*
slip no 6 q2 Write a java program using multithreading to simulate
traffic signal (Use Swing).
*/
package com.mycompany.practical_slip;
import java.util.logging.*;
@Override
public void run() {
int idx = 0;
while(true) {
System.out.println("Current Signal : " + lights[idx]);
try {
Thread.sleep(getDuration(lights[idx]) * 1000);
} catch (InterruptedException ex) {
Logger.getLogger(TrafficLight.class.getName()).log(Level.SEVERE, null,
ex);
}
idx = (idx + 1) % lights.length;
}
}
/*
slip no 7 Q2 Write a java program that implements a multi-thread
application that has three threads.
First thread generates random integer number after every one second, if
the number is
even; second thread computes the square of that number and prints it. If
the number is
odd, the third thread computes the cube of that number and prints it.
*/
package com.mycompany.practical_slip;
import java.util.Random;
import java.util.logging.*;
@Override
public void run() {
while(true) {
n = rand.nextInt(100);
System.out.println("Generated number: " + n);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(NumGenerator.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
}
SqrGenerator(NumGenerator numGenerator) {
this.numGenerator = numGenerator;
}
@Override
public void run() {
while(true) {
int n = numGenerator.n;
if(n % 2 == 0)
System.out.println("Square of " + n + " is " + n*n);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(SqrGenerator.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
}
CubeGenerator(NumGenerator numGenerator) {
this.numGenerator = numGenerator;
}
@Override
public void run() {
while(true) {
int n = numGenerator.n;
if(n % 2 != 0)
System.out.println("Cube of " + n + " is " + n*n*n);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(CubeGenerator.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
}
/*
slip no 7 q2. Write a java program for the following:
i. To create a Product (Pid, Pname, Price) table.
ii. Insert at least five records into the Product table.
iii. Display all the records from a Product table.
Assume Database is already created
*/
package com.mycompany.practical_slip;
import java.sql.*;
import java.util.Scanner;
int ch;
do {
System.out.println("Menu");
System.out.println("1. Create table Product.");
System.out.println("2. Insert into Product.");
System.out.println("3. Display records of product.");
System.out.println("4. Exit.");
System.out.println("------------------------------");
System.out.println("Enter your choice:");
ch = sc.nextInt();
switch(ch) {
case 1: create(conn);
break;
case 2: insert(conn);
break;
case 3 : select(conn);
break;
default : System.out.println("Invalid choice.");
break;
}
} while(ch != 4);
}
PreparedStatement pt = conn.prepareStatement(sql);
Scanner sc = new Scanner(System.in);
System.out.println("Enter pid:");
int pid = sc.nextInt();
sc.nextLine();
System.out.println("Enter pname:");
String name = sc.nextLine();
System.out.println("Enter price");
float price = sc.nextFloat();
pt.setInt(1, pid);
pt.setString(2, name);
pt.setFloat(3, price);
pt.executeUpdate();
}
while(res.next()) {
System.out.println("Pid = " + res.getInt("pid"));
System.out.println("PName = " + res.getString("pname"));
System.out.println("Price = " + res.getFloat("price"));
System.out.println("----------------------------------------------");
}
}
}
/*
slip no 9 Q1. Write a java program to define a thread for printing text on
output screen for ‘n’
number of times. Create 3 threads and run them. Pass the text ‘n’
parameters to the
thread constructor.
Example:
i. First thread prints “COVID19” 10 times.
ii. Second thread prints “LOCKDOWN2020” 20 times
iii. Third thread prints “VACCINATED2021” 30 times
*/
package com.mycompany.practical_slip;
T1(String msg) {
this.msg = msg;
}
public void run() {
for(int i=0; i<10; i++)
System.out.println(msg);
}
}
T2(String msg) {
this.msg = msg;
}
T3(String msg) {
this.msg = msg;
}
t1.start();
t2.start();
t3.start();
}
}
/*slip no 8 Q2*/
if(n > 1) {
boolean isPrime = true;
for(int i=2; i<n; i++) {
if(n % i == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
%>
<h3 class="prime">Prime number</h3>
<%
} else {
%>
<h3 class="prime">Not a prime number</h3>
<%
}
}
}
%>
</body>
</html>
/*
slip no 9 Q1. 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 (Use
Swing).
*/
package com.mycompany.practical_slip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.logging.*;
import javax.swing.*;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(175, yDelta, 50, 50);
repaint();
}
void setBallPos(int y) {
this.yDelta = y;
}
}
slip9_1()
{
frame = new JFrame("Ball Movement App");
frame.setSize(400, 400);
frame.setLayout(new BorderLayout());
frame.add(ballPanel, BorderLayout.CENTER);
frame.add(start, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
y += 5 * dir;
if(y <= 0)
dir = 1;
ballPanel.setBallPos(y);
}
}
import java.awt.GridLayout;
import java.sql.*;
import java.util.logging.*;
import javax.swing.*;
class StudentRec
{
private JFrame frame;
private JTextField tf1, tf2, tf3;
private JButton display;
Connection conn =
DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres",
"postgres", "postgres");
display.addActionListener((ActionEvent) -> {
try {
select(conn);
} catch (SQLException ex) {
frame.setLayout(new GridLayout(4,1));
frame.add(tf1);
frame.add(tf2);
frame.add(tf3);
frame.add(display);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
while(rs.next()) {
tf1.setText(" " + rs.getInt("rno"));
tf2.setText(" " + rs.getString("sname"));
tf3.setText(" " + rs.getFloat("per") + "");
}
}
}
/*
slip no 11 q2 Write a Java program to display information about all
columns in the DONAR table
using ResultSetMetaData.
*/
package com.mycompany.javaslip;
import java.sql.*;
ResultSet rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
if(n > 1) {
int sum = 0;
for(int i=1; i<=n/2; i++) {
if(n % i == 0) {
sum += i;
}
}
if(sum == n) {
%>
<h3>Perfect number</h3>
<%
} else {
%>
<h3>Not a perfect number</h3>
<%
}
}
}
%>
</body>
</html>
/*
slip no 12 Q2 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).
*/
package com.mycompany.javaslip;
import java.awt.BorderLayout;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
class ProjectTable {
private JFrame frame;
private JTable table;
createTable(conn);
insert(conn);
String[] colNames = {"pid", "pname", "description", "status"};
String[][] data = retriveData(conn);
frame.getContentPane().add(scrPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int noCol = rsmd.getColumnCount();
rs.last();
int noRow = rs.getRow();
rs.beforeFirst();
int rowCnt = 0;
while (rs.next()) {
for (int i = 1; i <= noCol; i++)
data[rowCnt][i - 1] = rs.getString(i);
rowCnt++;
}
return data;
}
/*
Slip no 13 Q1 Write a Java program to display information about the
database and list all the tables in
the database. (Use DatabaseMetaData).
*/
package com.mycompany.javaslip;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
System.out.println("" + md.getDatabaseProductName());
System.out.println("" + md.getDatabaseProductVersion());
System.out.println("" + md.getDriverName());
System.out.println("" + md.getDriverVersion());
/*
Slip no13 Q2 Write a Java program to show lifecycle (creation, sleep, and
dead) of a thread. Program
should print randomly the name of thread and value of sleep time. The name
of the
thread should be hard coded through constructor. The sleep time of a
thread will be a
random integer in the range 0 to 4999.
*/
package com.mycompany.javaslip;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
ThreadLifeCycle(String threadName) {
this.threadName = threadName;
}
Logger.getLogger(ThreadLifeCycle.class.getName()).log(Level.SEVERE, null,
ex);
}
t1.start();
t2.start();
t3.start();
}
}
/*
slip no 14 Q1 Write a Java program using Multithreading for a simple
search engine. Accept a string
to be searched. Search the string in all text files in the current folder.
Use a separate
thread for each file. The result should display the filename and line
number where the
string is found.
*/
package com.mycompany.javaslip;
import java.io.*;
import java.util.Scanner;
if (files != null) {
boolean foundInAnyFile = false;
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".txt")) {
SearchThread t = new SearchThread(file, searchStr);
t.start();
foundInAnyFile = true;
}
}
if (!foundInAnyFile) {
System.out.println("No text files found in the current
directory.");
}
} else {
System.err.println("Error: Unable to access current
directory.");
}
}
}
/* slipno 14 Q2 */
int fDigit = n;
while(fDigit >= 10) {
fDigit /= 10;
}
int lDigit = n % 10;
int sum = fDigit + lDigit;
%>
<h3 class="res">Sum of first and last digit is <%= sum
%></h3>
<%
}
%>
</body>
</html>
/*
slip no 15 q1 Write a java program to display name and priority of a
Thread.
*/
package com.mycompany.javaslip;
t1.start();
t2.start();
}
}
/*
slip no 16 Q1. Write a java program to create a TreeSet, add some colors
(String) and print out the
content of TreeSet in ascending order
*/
package com.mycompany.javaslip;
import java.util.*;
{
public static void main(String[] args) {
Set<String> colors = new TreeSet<>();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add("Yellow");
colors.add("Black");
System.out.println(colors);
}
}
/*
slip no 16 Q2 Write a Java program to accept the details of Teacher (TNo,
TName, Subject). Insert at
least 5 Records into Teacher Table and display the details of Teacher who
is teaching
“JAVA” Subject. (Use PreparedStatement Interface)
*/
package com.mycompany.javaslip;
import java.sql.*;
import java.util.Scanner;
class Teacher {
select(conn);
}
PreparedStatement ps = conn.prepareStatement(sql);
System.out.println("Enter tno:");
ps.setInt(1, sc.nextInt());
sc.nextLine();
System.out.println("Enter tname:");
ps.setString(2, sc.nextLine());
System.out.println("Enter subject:");
ps.setString(3, sc.nextLine());
ps.executeUpdate();
}
/*
Slip no 17 q1Write a java program to accept ‘N’ integers from a user.
Store and display integers in
sorted order having proper collection class. The collection should not
accept duplicate
elements.
*/
package com.mycompany.javaslip;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
System.out.println(set);
}
}
/*
Slip no 17 Q2 Write a java program using Multithreading to display the
number’s between 1 to 100
continuously in a JTextField by clicking on JButton. (Use Runnable
Interface &
Swing).
*/
package com.mycompany.javaslip;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.logging.*;
import javax.swing.*;
slip17_2() {
frame = new JFrame("Integer printing App");
frame.setSize(300, 200);
frame.setLayout(new GridLayout(2,1));
tf = new JTextField();
print = new JButton("Print");
frame.add(tf);
frame.add(print);
print.addActionListener((ActionEvent e) -> {
tf.setText("");
if(intThread == null || !intThread.isAlive()) {
intThread = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
for(int i=1; i<=100; i++) {
tf.setText(String.valueOf(i));
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
import java.util.Scanner;
import java.util.logging.*;
t.start();
}
}
/*
slip no 19 Q1 Write a java program to accept ‘N’ Integers from a user
store them into LinkedList
Collection and display only negative integers.
*/
package com.mycompany.javaslip;
import java.util.*;
<%
String numStr = request.getParameter("num");
t = rev;
rev = 0;
while(t > 0) {
rem = t % 10;
rev = (rev * 10) + rem;
t = t / 10;
switch(rem) {
case 0: out.println("zero");
break;
case 1: out.println("one");
break;
case 2: out.println("two");
break;
case 3: out.println("three");
break;
case 4: out.println("four");
break;
case 5: out.println("five");
break;
case 6: out.println("six");
break;
case 7: out.println("seven");
break;
case 8: out.println("eight");
break;
case 9: out.println("nine");
break;
}
}
}
%>
</body>
</html>
/*
slip no 20 q2Write a java program using Multithreading to demonstrate
drawing temple (Use
Swing)
*/
package com.mycompany.javaslip;
import javax.swing.*;
import java.awt.*;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
drawTemple(g);
}
g.setColor(Color.WHITE);
g.fillRect(130, 150, 40, 50); // Main Door
g.setColor(Color.RED);
int[] xPoints = {100, 150, 200}; // Triangle for roof
int[] yPoints = {100, 50, 100};
g.fillPolygon(xPoints, yPoints, 3);
g.setColor(Color.ORANGE);
g.fillRect(150, 40, 20, 10); // Flag
}
}
/*
slip no 21 Q1. Write a java program to accept ‘N’ Subject Names from a
user store them into
LinkedList Collection and Display them by using Iterator interface.
*/
package com.mycompany.javaslip;
import java.util.*;
/*
slip no 22 Q2 Write a java program using Multithreading to solve producer
consumer problem in
which a producer produces a value and consumer consume the value before
producer
generate the next value. (Hint: use thread synchronization)
*/
package com.mycompany.javaslip;
import java.util.LinkedList;
class SharedResource {
private LinkedList<String> buffer = new LinkedList<>();
private int capacity = 1;
buffer.add(value);
System.out.println("Produced: " + value);
notifyAll();
}
return value;
}
}
@Override
public void run() {
for(int i=0; i<5; i++) {
String value = "Value " + i;
sharedResource.produce(value);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer extends Thread {
private SharedResource sharedResource;
@Override
public void run() {
for(int i=0; i<5; i++) {
String value = "Value " + i;
sharedResource.consume();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
producer.start();
consumer.start();
}
}
/*
slip no 22 Q1 Write a Menu Driven program in Java for the following:
Assume Employee table with
attributes (ENo, EName, Salary) is already created. 1. Insert 2. Update 3.
Display 4.
Exit
*/
package com.mycompany.javaslip;
import java.sql.*;
import java.util.Scanner;
System.out.println("Enter eno:");
ps.setInt(1, sc.nextInt());
sc.nextLine();
System.out.println("Enter ename:");
ps.setString(2, sc.nextLine());
System.out.println("Enter salary:");
ps.setFloat(3, sc.nextFloat());
ps.executeUpdate();
}
String sql = "update emp2 set ename = '" + ename + "', salary = "
+ salary + " where eno = " + eno;
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
int ch;
do {
System.out.println("Menu");
System.out.println("1. Insert");
System.out.println("2. Update");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.println("-------------------------");
*/ slip no 22 Q2 */
<%
String user = request.getParameter("user");
/*
slip no 23 Q1 Write a java program using Multithreading to accept a String
from a user and display
each vowel from a String after every 3 seconds
*/
package com.mycompany.javaslip;
import java.util.Scanner;
import java.util.logging.*;
t.start();
}
}
/*
Slip no 24 Q1 Write a java program using Multithreading to scroll the
text from left to right
continuously (Use Swing).
*/
package com.mycompany.javaslip;
import javax.swing.*;
@Override
public void run() {
try {
while (true) {
String labelText = label.getText();
labelText = labelText.substring(1) + labelText.charAt(0);
label.setText(labelText);
Thread.sleep(200); // Adjust scrolling speed
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/*
SLip no 25 Q2 Write a Java Program for the following: Assume database is
already created.
*/
package com.mycompany.javaslip;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
tf = new JTextField();
p1.setLayout(new GridLayout(1, 2));
p1.add(new JLabel("Type your DDL query:"));
p1.add(tf);
Connection conn =
DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres",
"postgres", "postgres");
b1.addActionListener((ActionEvent e) -> {
try {
create(conn);
} catch (SQLException ex) {
Logger.getLogger(S25Q2.class.getName()).log(Level.SEVERE,
null, ex);
}
});
b2.addActionListener((ActionEvent e) -> {
try {
alter(conn);
} catch (SQLException ex) {
Logger.getLogger(S25Q2.class.getName()).log(Level.SEVERE,
null, ex);
}
});
b3.addActionListener((ActionEvent e) -> {
try {
drop(conn);
} catch (SQLException ex) {
Logger.getLogger(S25Q2.class.getName()).log(Level.SEVERE,
null, ex);
}
});
frame.add(p1, BorderLayout.CENTER);
frame.add(p2, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void create(Connection conn) throws SQLException {
String sql = tf.getText();
Statement stmt = conn.createStatement();
stmt.execute(sql);
}
/*
Slip no 26 Q1 Write a Java program to delete the details of given employee
(ENo EName Salary).
Accept employee ID through command line. (Use PreparedStatement Interface)
*/
package com.mycompany.javaslip;
import java.sql.*;
/*
slip no 27 Q1 Write a Java Program to display the details of College (CID,
CName, address, Year)
database table on JTable.
*/
package com.mycompany.javaslip;
import java.awt.BorderLayout;
import java.sql.*;
import javax.swing.*;
class CollegeTable {
private JFrame frame;
private JTable table;
frame.getContentPane().add(scrPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int rowCnt = 0;
while (rs.next()) {
for (int i = 1; i <= noCol; i++)
data[rowCnt][i - 1] = rs.getString(i);
rowCnt++;
}
return data;
}
/*
Slip no 28 Q2 Write a java program to display name of currently executing
Thread in multithreading
*/
package com.mycompany.javaslip;
t.start();
}
}
/*
Slip no 29 Q1. Write a Java program to display information about all
columns in the DONAR table
using ResultSetMetaData.
*/
package com.mycompany.javaslip;
import java.sql.*;
ResultSet rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
/*
slip no 29 Q2. Write a Java program to create LinkedList of integer
objects and perform the following:
i. Add element at first position
ii. Delete last element
iii. Display the size of link list
*/
package com.mycompany.javaslip;
import java.util.*;
int ch;
do {
System.out.println("Menu");
System.out.println("1. Insert at head");
System.out.println("2. Delete tail.");
System.out.println("3. Display size");
System.out.println("4. Exit");
System.out.println("------------------------------");
System.out.println("Enter your choice:");
ch = sc.nextInt();
System.out.println();
switch(ch) {
case 1: System.out.println("Enter a number:");
l.addFirst(sc.nextInt());
break;
case 2: l.removeLast();
break;
case 3:
System.out.println("Size : " + l.size() + "\n" + l);
break;
default: System.out.println("Invalid choice.");
}
System.out.println("-------------------------------");
} while(ch != 4);
}
}
/*
Slip no 30 Q1. Write a java program using Multithreading to demonstrate
drawing Indian flag (Use
Swing
*/
package com.mycompany.javaslip;
import javax.swing.*;
import java.awt.*;
public IndianFlag() {
setTitle("Simple Temple Drawing");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawFlag(g);
}
g.setColor(Color.WHITE);
g.fillRect(50, 100, 200, 50);
g.setColor(Color.GREEN);
g.fillRect(50, 150, 200, 50);
}
}
public class slip30_1
{
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new IndianFlag();
});
}
}