C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Number:"); lbl2.setBounds(20, 50, 100, 10); name='Hari',address='Ktm' WHERE sid='1'"; import java.io.

*; public class MyTest {


jframe.add(lbl2); JTextField txt2=new JTextField(); st.execute(sql2); System.out.println("\nData public static void main(String[] args) {
txt2.setBounds(120, 50, 120, 20); jframe.add(txt2); Updated Successfully"); try { FileWriter writer = new
JLabel lbl3=new JLabel("Result: "); FileWriter("MyFile.txt", true);
//lbl3.setText("Result: "); //deleting data String sql3="DELETE FROM student writer.write("Hello World");
WHERE sid='2'"; st.execute(sql3); writer.write("\r\n"); // write new line
lbl3.setBounds(20,80,100,30); jframe.add(lbl3); System.out.println("Data Deleted Successfully\n"); writer.write("Good Bye!");
JButton btn=new JButton("Calculate"); //retrieving data System.out.println("Data After writer.close(); } catch (IOException e) {
btn.setBounds(100, 120, 100, 30); Update and Delete"); String sql5="SELECT * FROM e.printStackTrace(); } } }
jframe.add(btn); btn.addActionListener(new student"; ResultSet rs1=st.executeQuery(sql5); Serialization Interface Serializable is a
ActionListener() { public void System.out.println("Sid\t"+"Name\t"+"address\t" marker interface (has no data member
actionPerformed(ActionEvent ae) { String +"Contact"); while(rs1.next()) { String and method). It is used to "mark" Java
first1=txt1.getText().toString(); String sid=rs1.getString(1); String name=rs1.getString(2); classes so that objects of these classes
second1=txt2.getText().toString(); int a,b,c; String address=rs1.getString(3); String may get the certain capability. The
a=Integer.parseInt(first1); contact=rs1.getString(4); Cloneable and Remote are also marker
b=Integer.parseInt(second1); c=a+b; System.out.println(sid+"\t"+name+"\t"+address+" interfaces.
lbl3.setText("Result: "+c); \t"+contact); } }catch(Exception e) { Serialization Example import java.io.*;
//JOptionPane.showMessageDialog(null, System.out.println(e); } } } class Studentinfo implements
"Addition= "+c); } }); } public static void Serializable { String name; int rid; static
main(String[] args) { Example Program – Program to add two Numbers
String contact; Studentinfo(String n, int
SwingUtilities.invokeLater(new Runnable() { public import java.applet.*; import java.awt.*; import
r, String c) { this.name = n; this.rid = r;
void run() { new SwingDemo(); } }); } } java.awt.event.*; public class Applet2 extends
impot java.swing.*; this.contact = c; } } public class MyTest {
Applet { public void init() { Label lbl1=new
public static void main(String[] args) {
import java.sql.*; public class DB_Test { public Label("First Number: "); add(lbl1); TextField
import java.awt.event.*; try { Studentinfo si = new
static void main(String[] args) { try { txt1=new TextField(10); add(txt1); Label lbl2=new
Studentinfo("Abhi", 104, "110044");
public class KeyListenerExample extends Frame Class.forName("com.mysql.jdbc.Driver"); Label("Second Number: "); add(lbl2); TextField
FileOutputStream fos = new
implements KeyListener{ Label l; TextArea area; Connection conn=DriverManager.getConnection txt2=new TextField(10);
FileOutputStream("student.ser");
KeyListenerExample(){ l=new Label(); ("jdbc:mysql://localhost:3306/bca","root","");
add(txt2); Label lbl3=new Label("Result: "); ObjectOutputStream oos = new
l.setBounds(20,50,100,20); area=new TextArea(); //database="", username=root and password=""
add(lbl3); Button btn=new Button("Submit"); ObjectOutputStream(fos);
area.setBounds(20,80,300, 300); System.out.println("Database connected");
add(btn); btn.addActionListener(new oos.writeObject(si); oos.close();
area.addKeyListener(this); add(l);add(area); Statement st=conn.createStatement(); //clearing
ActionListener() { public void fos.close(); } catch (Exception e) {
setSize(400,400); setLayout(null); setVisible(true); data String sql="DELETE FROM student";
actionPerformed(ActionEvent ae) { int e.printStackTrace(); } } } Object of
} public void keyPressed(KeyEvent e) { st.execute(sql); //inserting data String
first=Integer.parseInt (txt1.getText().toString()); int Studentinfo class is serialized using
l.setText("Key Pressed"); } public void sql1="INSERT INTO
second=Integer.parseInt (txt2.getText().toString()); writeObject() method and written to
keyReleased(KeyEvent e) { l.setText("Key student(sid,name,address,contact) VALUES
int res=first+second; lbl3.setText("Result: "+res); } student.ser file. Deserialization
Released"); } public void keyTyped(KeyEvent e) { ('1','Raaju','Btm','9862612723'),
}); } } Example import java.io * ; class
l.setText("Key Typed"); } public static void ('2','Ram','Ktm','8811111111')"; st.execute(sql1);
DeserializationTest { public static void
main(String[] args) { new KeyListenerExample(); } } System.out.println("Data Inserted
1. Reading a File import java.io.*; public main(String[] args) { studentinfo si=null
Successfully\n"); //retrieving data
class MyTest { public static void ; try { FileInputStream fis = new
import javax.swing.*; import java.awt.event.*; System.out.println("Data Before Update and
main(String[] args) { try { FileReader FileInputStream("student.ser");
public class SwingDemo { SwingDemo(){ JFrame Delete"); String sql4="SELECT * FROM student";
reader = new FileReader("MyFile.txt"); ObjectOutputStream ois = new
jframe=new JFrame("This is a simple JFrame ResultSet rs=st.executeQuery(sql4);
BufferedReader bufferedReader = new ObjectOutputStream(fis); si =
App"); jframe.setSize(400, 300); System.out.println("Sid\t"+"Name\t"+"address\t"
BufferedReader(reader); String line; (studentinfo)ois.readObject(); }
jframe.setLocationRelativeTo(null); +"Contact"); while(rs.next()) { String
while ((line = catch (Exception e) {
jframe.getContentPane().setLayout(null); sid=rs.getString(1); String name=rs.getString(2);
bufferedReader.readLine()) != null) { e.printStackTrace(); }
jframe.setVisible(true); JLabel lbl1=new String address=rs.getString(3); String
System.out.println(line); } System.out.println(si.name);
JLabel("First Number:"); lbl1.setBounds(20, 10, contact=rs.getString(4);
reader.close(); } catch (IOException e) { System.out. println(si.rid);
100, 10); jframe.add(lbl1); JTextField txt1=new System.out.println(sid+"\t"+name+"\t"+address+"
e.printStackTrace(); } } } System.out.println(si.contact); } }
JTextField(); txt1.setBounds(120, 10, 120, 20); \t"+contact); } //updating data String
2. . Writing to a File
jframe.add(txt1); JLabel lbl2=new JLabel("Second sql2="UPDATE student SET

You might also like