Java Coding
Java Coding
Q1. Write a java code to create Thread and show the working of yield() and sleep() methods?
Output:
Name-Abhay Kumar Course-MCA Roll No-03
class Counter {
private int count = 0;
3.Write a java program to wait main thread till child thread completes its execution using join
and sleep methods?
try {
childThread.join(); // Wait for the child thread to complete
System.out.println("Child thread has completed.");
} catch (InterruptedException e) {
e.printStackTrace();
}
Output :
Name-Abhay Kumar Course-MCA Roll No-03
4. Write a java program to wait child thread till main thread completes its execution using join
and sleep methods?
try {
// Sleep for some time in the main thread
Thread.sleep(3000);
System.out.println("Main thread has completed its execution.");
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
childThread.join(); // Wait for the child thread to complete
System.out.println("Child thread has completed.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Output:
Name-Abhay Kumar Course-MCA Roll No-03
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// Close resources
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Output:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
// Close resources
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
InsertStudent insertStudent = new InsertStudent();
Thread thread = new Thread(insertStudent);
thread.start();
}
}
Output:
Name-Abhay Kumar Course-MCA Roll No-03
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
// Close resources
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Output :
Name-Abhay Kumar Course-MCA Roll No-03
6. Write a JDBC program to insert data into Student table using Statement interface ?
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a statement
stmt = conn.createStatement();
se.printStackTrace();
}
}
}
}
Output:
Name-Abhay Kumar Course-MCA Roll No-03
7. Write a JDBC program to fetch all the rows from Student table using Statement interface?
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a statement
stmt = conn.createStatement();
Output:
Name-Abhay Kumar Course-MCA Roll No-03
8. Write a JDBC program to fetch specific row Student table using Statement interface and
absolute() method?
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a statement
stmt = conn.createStatement();
Output:
Name-Abhay Kumar Course-MCA Roll No-03
9. Write a JDBC program to insert multiple records in Student table using PreparedStatement
interface and Scanner class ?
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a statement
stmt = conn.createStatement();
Output:
Name-Abhay Kumar Course-MCA Roll No-03
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a statement
stmt = conn.createStatement();
// Insert operation
String insertSql = "INSERT INTO Student (id, name, age) VALUES (?, ?, ?)";
PreparedStatement insertStmt = conn.prepareStatement(insertSql);
insertStmt.setInt(1, 1);
insertStmt.setString(2, "John Doe");
insertStmt.setInt(3, 20);
insertStmt.addBatch();
// Update operation
String updateSql = "UPDATE Student SET age = ? WHERE id = ?";
PreparedStatement updateStmt = conn.prepareStatement(updateSql);
updateStmt.setInt(1, 25);
updateStmt.setInt(2, 1);
updateStmt.addBatch();
// Delete operation
String deleteSql = "DELETE FROM Student WHERE id = ?";
Name-Abhay Kumar Course-MCA Roll No-03
}
}
}
}
Output:
Name-Abhay Kumar Course-MCA Roll No-03
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a statement
stmt = conn.createStatement();
// Update operation
String updateSql = "UPDATE Student SET age = 25 WHERE id = 1";
stmt.executeUpdate(updateSql);
// Delete operation
String deleteSql = "DELETE FROM Student WHERE id = 1";
stmt.executeUpdate(deleteSql);
Output:
Name-Abhay Kumar Course-MCA Roll No-03
12. Create a stored procedure getName(IN,OUT) invoke the procedure using Callable
Statement?
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a CallableStatement
String sql = "{CALL getName(?, ?)}";
cstmt = conn.prepareCall(sql);
} finally {
// Close resources
try {
if (cstmt != null)
cstmt.close();
} catch (SQLException se2) {
} // Nothing we can do
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
Output :
Name-Abhay Kumar Course-MCA Roll No-03
13. Create a stored procedure updateMarks(IN,IN) invoke the procedure using Callable
Statement?
import java.sql.*;
// Database credentials
static final String USER = "your_username";
static final String PASS = "your_password";
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a CallableStatement
String sql = "{CALL updateMarks(?, ?)}";
cstmt = conn.prepareCall(sql);
cstmt.close();
} catch (SQLException se2) {
} // Nothing we can do
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
Output :
Name-Abhay Kumar Course-MCA Roll No-03
14. Create a GUI application to calculate addition and subtraction using swing application?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public CalculatorApp() {
// Set up the JFrame
setTitle("Calculator");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4, 2));
}
}
private void calculateResult(Operation operation) {
String number1Text = number1Field.getText();
String number2Text = number2Field.getText();
try {
int number1 = Integer.parseInt(number1Text);
int number2 = Integer.parseInt(number2Text);
int result = 0;
switch (operation) {
case ADD:
result = number1 + number2;
break;
case SUBTRACT:
result = number1 - number2;
break;
}
resultField.setText(Integer.toString(result));
Output:
Name-Abhay Kumar Course-MCA Roll No-03
15. Create a GUI application to calculate the number of words and character in JTextArea?
import javax.swing.*;
public class TextAreaExample
{
TextAreaExample(){
JFrame f= new JFrame();
JTextArea area=new JTextArea("Welcome to javatpoint");
area.setBounds(10,30, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}}
Output:
import javax.swing.*;
import java.awt.event.*;
public class TextAreaExample implements ActionListener{
JLabel l1,l2;
JTextArea area;
JButton b;
TextAreaExample() {
JFrame f= new JFrame();
l1=new JLabel();
l1.setBounds(50,25,100,30);
Name-Abhay Kumar Course-MCA Roll No-03
l2=new JLabel();
l2.setBounds(160,25,100,30);
area=new JTextArea();
area.setBounds(20,75,250,200);
b=new JButton("Count Words");
b.setBounds(100,300,120,30);
b.addActionListener(this);
f.add(l1);f.add(l2);f.add(area);f.add(b);
f.setSize(450,450);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String text=area.getText();
String words[]=text.split("\\s");
l1.setText("Words: "+words.length);
l2.setText("Characters: "+text.length());
}
public static void main(String[] args) {
new TextAreaExample();
}
}
Output :
Name-Abhay Kumar Course-MCA Roll No-03
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new RadioButtonExample();
}
}
Output:
Name-Abhay Kumar Course-MCA Roll No-03
import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
Output:
Name-Abhay Kumar Course-MCA Roll No-03
18.Assume login table having username and password fields in mysql / sql. Create a Login
application using swing components. On click login button data from textfield must be matched
from login table and if data matched then print valid user in Jlabel otherwise print invalid user?
Name-Abhay Kumar Course-MCA Roll No-03
19. Write a java program that will return the first half of the string, if the length of the string is
even and return null if the length is odd?
Output:
Name-Abhay Kumar Course-MCA Roll No-03
20. Write a java program that will concatenate 2 String and return the result. The result should
be in lowercase?
Note: If the concatenation creates a double-char, then one of the character need to be omitted.
return result.toLowerCase();
}
Output :
Name-Abhay Kumar Course-MCA Roll No-03
21. Given a string and return a string made of ‘n’ copies of the first 2 chars of the original string
where ‘n’ is the length of the string?
Example: Demo
o/p DeDeDeDe
return result.toString();
}
Output: