EXCEPTIONS
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package excepdemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.net.*;
/**
*
* @author Admin
*/
public class ExcepDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// Java program to demonstrate ArithmeticException
try {
int a = 30, b = 0;
int c = a/b; // cannot divide by zero
System.out.println ("Result = " + c);
}
catch(ArithmeticException e) {
System.out.println ("Can't divide a number by 0");
}
//Java program to demonstrate NullPointerException
try {
String a = null; //null value
System.out.println(a.charAt(0));
} catch(NullPointerException e) {
System.out.println("NullPointerException..");
}
// Java program to demonstrate StringIndexOutOfBoundsException
try {
String a = "This is like chipping "; // length is 22
char c = a.charAt(24); // accessing 25th element
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println("StringIndexOutOfBoundsException");
}
//Java program to demonstrate FileNotFoundException
try {
// Following file does not exist
File file = new File("E://file.txt");
FileReader fr = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println("File does not exist");
}
// Java program to demonstrate NumberFormatException
try {
// "akki" is not a number
int num = Integer.parseInt ("akki") ;
System.out.println(num);
} catch(NumberFormatException e) {
System.out.println("Number format exception");
}
// Java program to demonstrate ArrayIndexOutOfBoundException
try{
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an array of
// size 5
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
// Java program to demonstrate IOException
Scanner scan = new Scanner("Hello Geek!");
// Print the line
System.out.println("" + scan.nextLine());
// Check if there is an IO exception
System.out.println("Exception Output: "
+ scan.ioException());
scan.close();
// Java program to demonstrate ClassNotFoundException
try{
Class.forName("Class1"); // Class1 is not defined
}
catch(ClassNotFoundException e){
System.out.println(e);
System.out.println("Class Not Found...");
}
String host = "https://gogle.co";
URL url = null;
HttpURLConnection con = null;
try {
url = new URL(host);
con = (HttpURLConnection) url.openConnection();
System.out.println(con.getResponseCode());
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (con != null) {
con.disconnect();
}
}
// TODO code application logic here
}
}
JDBC
Insert:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String sname=jTextField1.getText();
String sid=jTextField2.getText();
String sclass=jTextField3.getText();
String scollege=jTextField4.getText();
String sdept=jTextField5.getText();
String smail=jTextField6.getText();
String sphone=jTextField7.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/coursedb","root","");
Statement stmt=(Statement) con.createStatement();
String INSERT="INSERT INTO student values('"+sname+"','"+sid+"','"+sclass+"','"+scollege+"','"+sdept+"','"+smail+"','"+sphone+"')";
stmt.executeUpdate(INSERT);
JOptionPane.showMessageDialog(null,"Record Inserted");
}
catch (Exception e){
System.out.println(e);
}
}
Delete:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String sid=jTextField2.getText();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/coursedb","root","");
Statement stmt=(Statement) con.createStatement();
String DELETE="DELETE FROM student where id='"+sid+"'";
stmt.execute(DELETE);
JOptionPane.showMessageDialog(null,"Record Deleted");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Update:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String sid=jTextField2.getText();
String scollege=jTextField4.getText();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/coursedb","root","");
Statement stmt=(Statement) con.createStatement();
String UPDATE="UPDATE student SET college='"+scollege+"' where id='"+sid+"'";
stmt.execute(UPDATE);
JOptionPane.showMessageDialog(null,"Record Updated");
}
catch(Exception ex)
{
{
System.out.println(ex);
}
}
}
Select:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String sid=jTextField2.getText();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/coursedb","root","");
Statement stmt=(Statement) con.createStatement();
ResultSet rs;
rs=stmt.executeQuery("SELECT name,id,class,college,department,mail,phone from student");
{
while(rs.next())
{
System.out.println("Student name : " +rs.getString("name"));
System.out.println("Student id : " +rs.getString("id"));
System.out.println("Student class : " +rs.getString("class"));
System.out.println("Student college : " +rs.getString("college"));
System.out.println("Student Department : " +rs.getString("department"));
System.out.println("Student Mail ID : " +rs.getString("mail"));
System.out.println("Student Phone Number : " +rs.getString("phone"));
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Clear:
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
}
HOUSE
import java.applet.Applet;
import java.awt.Graphics;
public class HouseApplet extends Applet {
// Draw the house g.drawRect(100, 200, 200, 150);
// Draw the roof g.drawLine(100, 200, 200, 100); g.drawLine(200, 100, 300, 200);
// Draw the door
g.drawRect(170, 250, 60, 100);
// Draw windows
g.drawRect(120, 220, 50, 50); g.drawRect(230, 220, 50, 50);
}
HUMAN FACE
[7:32 pm, 30/09/2024] Jeyasurya: ava Program to Draw a Human Face using Applet*/
import java.applet.*;
import java.awt.*;
public class Human_Face extends Applet
{
//Initialize the applet
public void init()
{
setBackground(Color.white);
}
//Draw the human face
public void paint(Graphics g)
{
//Change color to cream
Color clr=new Color(255,179,86);
g.setColor(clr);
//Draw and fill the face
g.drawOval (100,100,250,300);
g.fillOval (100,100,250,300);
//Change color to black
g.setColor(Color.black);
//Draw the left eye
[7:32 pm, 30/09/2024] Jeyasurya: g.drawOval (160,185,40,25);
g.fillOval (160,185,40,25);
//Draw the right eye
g.drawOval (250,185,40,25);
g.fillOval (250,185,40,25);
//Draw the Left Eyebrow
g.drawArc(160,170,35,10,0,180);
//Draw the Right Eyebrow
g.drawArc(250,170,35,10,0,180);
//Draw the Nose
g.drawLine(210,265,210,275);
g.drawLine(240,265,240,275);
g.drawArc(210,275,30,10,0,-180);
//Draw the smile
g.drawArc(175,300,100,50,0,-180);
}
}
/*
*/
stack
import java.util.Stack;
class Main {
public static void main(String[] args) { Stack animals= new Stack<>();
// Add elements to Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: " + animals);
// Remove element stacks
String element = animals.pop(); System.out.println("Removed Element:" + element);
// Access element from the top
String element = animals.peek(); System.out.println("Element at top:" + element);
// Search an element int position = animals.search("Horse"); System.out.println("Position of Horse: " + position);
// Check if stack is empty boolean result = animals.empty();
System.out.println("Is the stack empty?" + result)