Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.62 KB | None | 0 0
  1. 1. Program to demonstrate Command Line Argument.
  2.  
  3. public class cla
  4. {
  5. public static void main(String args[])
  6. {
  7. int a,b,c;
  8. a=Integer.parseInt(args[0]);
  9. b=Integer.parseInt(args[1]);
  10. c=a+b;
  11. System.out.println("the sum is "+c);
  12. }
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. 2.Program to demonstrate BufferedReader Class.
  27.  
  28. import java.io.*;
  29. public class io_test
  30. {
  31. public static void main(String args[]) throws IOException
  32. {
  33. InputStreamReader ip=new InputStreamReader(System.in);
  34. BufferedReader br=new BufferedReader(ip);
  35. System.out.println("enter your name ");
  36. String s=br.readLine();
  37. System.out.println("your name is "+s);
  38. }
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. 3.Program to demonstrate Parameterized Constructor.
  51.  
  52. class Box{
  53. double width;
  54. double height;
  55. double depth;
  56.  
  57. Box(double w,double h,double d)
  58. {
  59. width=w;
  60. height=h;
  61. depth=d;
  62. }
  63. double volume(){
  64. return width*height*depth;
  65. }
  66. }
  67. class demo
  68. {
  69. public static void main(String args[])
  70. {
  71. Box ob1=new Box(5,10,15);
  72. double vol;
  73. vol=ob1.volume();
  74. System.out.println("the volume of the box is "+vol);
  75. }
  76. }
  77.  
  78.  
  79.  
  80. 4.Program to demonstrate Function overloading.
  81.  
  82. class overloading
  83. {
  84. void test()
  85. { System.out.println("no parameters");
  86. }
  87. void test(int a)
  88. {System.out.println("single integer passed:a= "+a);
  89. }
  90. void test(int a,int b)
  91. {System.out.println("two integers passed:a= "+a+" b= "+b);
  92. }
  93. void test(double a)
  94. {
  95. System.out.println("double value passed:a= "+a);
  96. }
  97. }
  98.  
  99. class overload
  100. {
  101. public static void main(String args[])
  102. {
  103. overloading ob= new overloading();
  104.  
  105. ob.test();
  106. ob.test(4);
  107. ob.test(7,9);
  108. ob.test(3.75);
  109. }
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. 5.Program to demonstrate Inheritance.
  117.  
  118. class Room
  119. {
  120. int length;
  121. int breadth;
  122. Room(int x,int y)
  123. { length=x;
  124. breadth=y;
  125. }
  126. int area()
  127. {
  128. return(length*breadth);
  129. }
  130. }
  131. class Bedroom extends Room
  132. {
  133. int height;
  134. Bedroom(int x,int y,int z)
  135. { super(x,y);
  136. height=z;
  137. }
  138. int volume()
  139. { return(length*breadth*height);
  140. }
  141. }
  142. class inherit_demo
  143. {
  144. public static void main(String args[])
  145. {
  146. Bedroom room1=new Bedroom(14,12,10);
  147. int area=room1.area();
  148. int volume=room1.volume();
  149. System.out.println("Area ="+area);
  150. System.out.println("volume ="+volume);
  151. }
  152. }
  153.  
  154.  
  155.  
  156. 6.Program to demonstrate Exception Handling.
  157.  
  158. class excep
  159. {
  160. public static void main(String args[])
  161. {
  162. int a=10,b=5,c=5;
  163. int x,y;
  164. try
  165. {
  166. x=a/(b-c);
  167. }
  168. catch(ArithmeticException e)
  169. { System.out.println("division by zero not allowed ");
  170. }
  171. y=a/(b+c);
  172. System.out.println("y= "+y);
  173. }
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. 7.Program to demonstrate Applets and Event Handling.
  188.  
  189. import java.awt.*;
  190. import java.awt.event.*;
  191. import java.applet.*;
  192. /* <applet code="add" width=250 height=200>
  193. </applet>
  194. */
  195. public class add extends Applet implements ActionListener
  196. {
  197. TextField t1,t2,t3;
  198. Button b1;
  199. public void init()
  200. {
  201. t1=new TextField(5);
  202. t2=new TextField(5);
  203. t3=new TextField(5);
  204. b1=new Button("add");
  205. Label l1=new Label("enter first number ",Label.LEFT);
  206. Label l2=new Label(" enter second number ",Label.LEFT);
  207. Label l3=new Label(" the sum is ",Label.LEFT);
  208. add(l1); add(t1);
  209. add(l2); add(t2);
  210. add(b1);
  211. add(l3); add(t3);
  212.  
  213. b1.addActionListener(this);
  214. }
  215. public void actionPerformed(ActionEvent ae)
  216. {
  217. if(ae.getSource()==b1)
  218. {
  219. int a=Integer.parseInt(t1.getText());
  220. int b=Integer.parseInt(t2.getText());
  221. t3.setText(String.valueOf(a+b));
  222. }
  223. }
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240. 8.Program to demonstrate MouseEvents class.
  241.  
  242. import java.awt.*;
  243. import java.awt.event.*;
  244. import java.applet.*;
  245.  
  246. /*<applet code="MouseEvents" width=400 height=400>
  247. </applet>
  248. */
  249. public class MouseEvents extends Applet implements MouseListener,MouseMotionListener{
  250. String msg=" ";
  251. int mousex=0,mousey=0;
  252.  
  253. public void init()
  254. {
  255. addMouseListener(this);
  256. addMouseMotionListener(this);
  257. }
  258. public void mouseClicked(MouseEvent me){
  259. mousex=0;
  260. mousey=20;
  261. msg="mouse clicked";
  262. repaint();
  263. }
  264. public void mouseEntered(MouseEvent me){
  265. mousex=0;
  266. mousey=20;
  267. msg="mouse entered";
  268. repaint();
  269. }
  270. public void mouseExited(MouseEvent me){
  271. mousex=0;
  272. mousey=20;
  273. msg="mouse exited";
  274. repaint();
  275. }
  276. public void mousePressed(MouseEvent me){
  277. mousex=me.getX();
  278. mousey=me.getY();
  279. msg="down";
  280. repaint();
  281. }
  282. public void mouseReleased(MouseEvent me){
  283. mousex=me.getX();
  284. mousey=me.getY();
  285. msg="up";
  286. repaint();
  287. }
  288. public void mouseDragged(MouseEvent me){
  289. mousex=me.getX();
  290. mousey=me.getY();
  291. msg="*";
  292. showStatus("dragging mouse at"+mousex+","+mousey);
  293. repaint();
  294. }
  295. public void mouseMoved(MouseEvent me){
  296. showStatus("moving mouse at "+me.getX()+","+me.getY());
  297. repaint();
  298. }
  299. public void paint(Graphics g){
  300. g.drawString(msg,mousex,mousey);
  301. }
  302. }
  303.  
  304. 9.Program to demonstrate tabbedpane using swings.
  305.  
  306. import javax.swing.*;
  307. /* <applet code="tabbedpane" width=400 height=200>
  308. </applet>
  309. */
  310. public class tabbedpane extends JApplet {
  311. public void init()
  312. { JTabbedPane jtp=new JTabbedPane();
  313. jtp.addTab("semester",new semesterpanel());
  314. jtp.addTab("course",new coursepanel());
  315. jtp.addTab("hobbies",new hobbiespanel());
  316. getContentPane().add(jtp);
  317. }
  318. }
  319. class semesterpanel extends JPanel{
  320. public semesterpanel(){
  321. JButton b1=new JButton ("1st");
  322. add(b1);
  323. JButton b2=new JButton ("2st");
  324. add(b2);
  325. JButton b3=new JButton ("3rd");
  326. add(b3);
  327. JButton b4=new JButton ("4th");
  328. add(b4);
  329. }
  330. }
  331. class coursepanel extends JPanel{
  332. public coursepanel(){
  333. JCheckBox ch1=new JCheckBox("b.tech");
  334. add(ch1);
  335.  
  336. JCheckBox ch2=new JCheckBox("bba");
  337. add(ch2);
  338. JCheckBox ch3=new JCheckBox("bca");
  339. add(ch3);
  340. JCheckBox ch4=new JCheckBox("bcom");
  341. add(ch4);
  342.  
  343. }
  344. }
  345.  
  346. class hobbiespanel extends JPanel{
  347. public hobbiespanel(){
  348. JComboBox cb=new JComboBox();
  349.  
  350.  
  351. cb.addItem("cricket");
  352. cb.addItem("music");
  353. cb.addItem("coin collection");
  354. add(cb);
  355. }
  356. }
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370. 10.Program to create a demo application form using swings.
  371.  
  372. import java.awt.*;
  373. import javax.swing.*;
  374. import java.awt.event.*;
  375. /* <applet code="appform" height=300 width=300>
  376. </applet>
  377. */
  378. public class appform extends JApplet implements ActionListener
  379. {
  380. JLabel l1,l2,l3,l4;
  381. JButton b1,b2;
  382. JTextField t1,t2,t3;
  383. JTextArea ta1;
  384. JRadioButton rb1,rb2;
  385. JComboBox cb;
  386.  
  387. public void init()
  388. {
  389. try
  390. { SwingUtilities.invokeAndWait(new Runnable()
  391. {public void run()
  392. { createGUI();
  393. }
  394. });
  395. }
  396. catch(Exception e)
  397. { System.err.println("createGUI was not successfull");
  398. }
  399. }
  400. private void createGUI()
  401. {
  402. setLayout(new FlowLayout());
  403. l1=new JLabel("name");
  404. add(l1);
  405. t1=new JTextField(15);
  406. add(t1);
  407.  
  408. l2=new JLabel("enter your address",JLabel.LEFT);
  409. add(l2);
  410. ta1=new JTextArea(10,20);
  411. add(ta1);
  412.  
  413. l3=new JLabel(" select gender",JLabel.LEFT);
  414. add(l3);
  415. rb1=new JRadioButton("M");
  416. rb2=new JRadioButton("F");
  417. add(rb1);
  418. add(rb2);
  419. ButtonGroup bg=new ButtonGroup();
  420. bg.add(rb1);
  421. bg.add(rb2);
  422. l4=new JLabel(" choose your nation");
  423. add(l4);
  424. cb=new JComboBox();
  425. cb.addItem("india");
  426. cb.addItem("america");
  427. cb.addItem("italy");
  428. cb.addItem("australia");
  429. cb.addItem("russia");
  430. cb.addItem("germany");
  431. cb.addItem("others");
  432. add(cb);
  433. System.out.println(" ");
  434. b1=new JButton("submit");
  435. add(b1);
  436. b1.addActionListener(this);
  437. }
  438. public void actionPerformed(ActionEvent ae)
  439. {
  440. if(ae.getSource()==b1)
  441. { JOptionPane.showMessageDialog(this,"your form has been submitted","successfull",JOptionPane.INFORMATION_MESSAGE);
  442. }
  443. }
  444. }
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479. 11.Program to implement JTree.
  480. import java.awt.*;
  481. import java.awt.event.*;
  482. import javax.swing.*;
  483. import javax.swing.tree.*;
  484. /*<applet code="jtree" height=200 width=300>
  485. </applet>
  486. */
  487. public class jtree extends JApplet{
  488. JTree tr;
  489. public void init()
  490. {
  491. Container contentpane=getContentPane();
  492. contentpane.setLayout(new BorderLayout());
  493. DefaultMutableTreeNode top=new DefaultMutableTreeNode("folder 1");
  494.  
  495. DefaultMutableTreeNode a=new DefaultMutableTreeNode("games");
  496. top.add(a);
  497. DefaultMutableTreeNode a1=new DefaultMutableTreeNode("cricket");
  498. a.add(a1);
  499. DefaultMutableTreeNode a2=new DefaultMutableTreeNode("football");
  500. a.add(a2);
  501.  
  502. DefaultMutableTreeNode b=new DefaultMutableTreeNode("books");
  503. top.add(b);
  504. DefaultMutableTreeNode b1=new DefaultMutableTreeNode("allamaraju");
  505. b.add(b1);
  506. DefaultMutableTreeNode b2=new DefaultMutableTreeNode("e balaguruswamy");
  507. b.add(b2);
  508. DefaultMutableTreeNode b3=new DefaultMutableTreeNode("herbert schildt");
  509. b.add(b3);
  510.  
  511. tr =new JTree(top);
  512. contentpane.add(tr);
  513. }
  514. }
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525. 12. Program to implement Remote Method Invocation(RMI)
  526. Product.java
  527. import java.rmi.*;
  528. public interface product extends Remote{
  529. public String getname() throws RemoteException;
  530. }
  531.  
  532. Productimpl.java
  533. import java.rmi.*;
  534. import java.rmi.server.*;
  535. public class productimpl extends UnicastRemoteObject implements product
  536. {
  537. public String name1;
  538. productimpl(String name) throws RemoteException
  539. {
  540. name1=name;
  541. }
  542. public String getname()
  543. {
  544. return name1;
  545. }
  546. }
  547. Productclient.java
  548. import java.rmi.*;
  549. import java.rmi.registry.*;
  550. public class productclient
  551. {
  552. public static void main(String s[])
  553. {
  554. try
  555. {
  556. product p=(product)Naming.lookup("rmi://127.0.0.1:1099/java");
  557. System.out.println("productname="+p.getname());
  558. }
  559. catch(Exception e)
  560. {
  561. System.out.println(e);
  562. }
  563. }
  564. }
  565. Productserver.java
  566. import java.rmi.*;
  567. import java.rmi.registry.*;
  568. public class productserver
  569. {
  570. public static void main(String s[])
  571. {
  572. try{
  573. LocateRegistry.getRegistry(1099);
  574. productimpl p1 =new productimpl("java");
  575. Naming.bind("java",p1);
  576. }
  577. catch(java.lang.Exception e)
  578. {System.out.println(e);
  579. }
  580. }
  581. }
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605. 13.Program to add two numbers using Remote Method Invocation.
  606.  
  607. Productadd.java
  608. import java.rmi.*;
  609. public interface productadd extends Remote{
  610. public int add() throws RemoteException;
  611. }
  612. Productimpladd.java
  613. import java.rmi.*;
  614. import java.rmi.server.*;
  615. public class productimpladd extends UnicastRemoteObject implements productadd
  616. {
  617. public int x,y;
  618. productimpladd(int x1,int y1) throws RemoteException
  619. {
  620. x=x1;
  621. y=y1;
  622. }
  623. public int add()
  624. {
  625. return (x+y);
  626. }}
  627.  
  628. Productclientadd.java
  629. import java.rmi.*;
  630. import java.rmi.registry.*;
  631. public class productclientadd
  632. {
  633. public static void main(String s[])
  634. {
  635. try
  636. {
  637. productadd p=(productadd)Naming.lookup("rmi://127.0.0.1:1099/java");
  638. System.out.println("output = "+p.add());
  639. }
  640. catch(Exception e)
  641. {
  642. System.out.println(e);
  643. }
  644. }
  645. }
  646.  
  647. Productserveradd.java
  648. import java.rmi.*;
  649. import java.rmi.registry.*;
  650. public class productserveradd
  651. {
  652. public static void main(String s[])
  653. {
  654. try{
  655. LocateRegistry.getRegistry(1099);
  656. productimpladd p1 =new productimpladd(6,5);
  657. Naming.bind("java",p1);
  658. }
  659. catch(java.lang.Exception e)
  660. {System.out.println(e);
  661. }
  662. }
  663. }
  664.  
  665.  
  666.  
  667. 13. Program to show the contents of a database table using JDBC.
  668.  
  669. import java.sql.*;
  670. public class jdbcex
  671. { public static void main(String args[])
  672. {
  673. try
  674. {
  675. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  676. Connection con=DriverManager.getConnection("jdbc:odbc:g45","","");
  677. Statement st=con.createStatement();
  678. ResultSet rs=st.executeQuery("select * from g45");
  679. while(rs.next())
  680. { System.out.println(rs.getString(1)+"\t"+rs.getString(2));
  681. }
  682. }
  683. catch(Exception e)
  684. { System.out.println("the error has occurred: "+e);
  685. }
  686. }
  687. }
  688.  
  689. 14. Program to enter a row into the database table.
  690.  
  691. import java.sql.*;
  692. public class jdbcex2
  693. { public static void main(String args[])
  694. {
  695. try
  696. {
  697. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  698. Connection con=DriverManager.getConnection("jdbc:odbc:g45","","");
  699. Statement st=con.createStatement();
  700. int n=st.executeUpdate("insert into g45 (Name,Roll_no) values('Gaurav Kalra',43)");
  701. ResultSet rs1=st.executeQuery("select * from g45");
  702.  
  703. while(rs1.next())
  704. { System.out.println(rs1.getString(1)+"\t"+rs1.getString(2));
  705. }
  706. }
  707. catch(Exception e)
  708. { System.out.println("the error has occurred: "+e);
  709. }
  710. }
  711. }
  712.  
  713.  
  714. 16. Program to update a database table in JDBC.
  715.  
  716. employee table
  717.  
  718. employee
  719. emp_id name salary address
  720. e01 rajesh kumar 22000 delhi
  721. e02 shekar kapur 25000 mumbai
  722. e03 reena rai 20000 kolkata
  723. e04 ahmed qureshi 18000 allahabad
  724. e05 manish pandey 28000 delhi
  725. e06 Gaurav Mishra 40000 noida
  726.  
  727.  
  728. import java.sql.*;
  729. public class jdbcup
  730. { public static void main(String args[])
  731. {
  732. try
  733. {
  734. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  735. String str="update employee set salary=29800 where emp_id='e01'";
  736. Connection con=DriverManager.getConnection("jdbc:odbc:g44","","");
  737. Statement st=con.createStatement();
  738. int n=st.executeUpdate(str);
  739.  
  740. ResultSet rs1=st.executeQuery("select * from employee");
  741.  
  742. while(rs1.next())
  743. { System.out.println(rs1.getString(1)+"\t"+rs1.getString(2)+"\t"+rs1.getString(3)+"\t"+rs1.getString(4));
  744. }
  745. }
  746. catch(Exception e)
  747. { System.out.println("the error has occurred: "+e);
  748. }
  749. }
  750. }
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767. 17.Program to implement Prepared Statement in JDBC.
  768. import java.sql.*;
  769. public class pst
  770. { public static void main(String args[])
  771. {
  772. try
  773. {
  774. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  775. String query="select * from employee where salary>20000";
  776. Connection con=DriverManager.getConnection("jdbc:odbc:g44","","");
  777. PreparedStatement pst=con.prepareStatement(query);
  778. ResultSet rs1=pst.executeQuery();
  779.  
  780. while(rs1.next())
  781. { System.out.println(rs1.getString(1)+"\t"+rs1.getString(2)+"\t"+rs1.getString(3)+"\t"+rs1.getString(4));
  782. }
  783. }
  784. catch(Exception e)
  785. { System.out.println("the error has occurred: "+e);
  786. }
  787. }
  788. }
  789.  
  790.  
  791. 18. Program to update a database table in JDBC using Prepared Statement.
  792.  
  793. import java.sql.*;
  794. public class psup
  795. {
  796. public static void main(String args[])
  797. {
  798. try
  799. {
  800. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  801. Connection con1=DriverManager.getConnection("jdbc:odbc:g44","","");
  802. Statement st1=con1.createStatement();
  803.  
  804. String str="Select * from employee";
  805. String str1="update employee set salary=? where emp_id=? ";
  806. //int n=st1.executeUpdate(str1);
  807.  
  808. PreparedStatement pst=con1.prepareStatement(str1);
  809. pst.setString(1,"35000");
  810. pst.setString(2,"e04");
  811. pst.executeUpdate();
  812. pst.close();
  813. ResultSet rs=st1.executeQuery(str);
  814.  
  815.  
  816. // System.out.println("Rows updated:= "+n);
  817. while(rs.next())
  818. { System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4));
  819.  
  820. }
  821.  
  822. con1.commit();
  823. con1.close();
  824.  
  825. }
  826. catch(Exception e)
  827. { System.out.println("Error has occured :"+e);
  828. }
  829. }
  830. }
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852. 19.Program to implement scrollable Result set in JDBC.
  853. import java.sql.*;
  854. public class scrl
  855. {
  856. public static void main(String args[])
  857. {
  858.  
  859. try {
  860. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  861. Connection con=DriverManager.getConnection("jdbc:odbc:g44","","");
  862. String s1,s2,s3;
  863. Statement stmt = con.createStatement(
  864. ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  865. ResultSet rs = stmt.executeQuery("SELECT * FROM employee");
  866. System.out.println("Next()");
  867. while (rs.next()) {
  868. s1 = rs.getString(1);
  869. s2 = rs.getString(2);
  870. s3 = rs.getString(3);
  871. System.out.println(s1+"\t"+s2+"\t"+s3);
  872. }
  873. System.out.println("Previous()");
  874. while (rs.previous()) {
  875. s1 = rs.getString(1);
  876. s2 = rs.getString(2);
  877. s3 = rs.getString(3);
  878. System.out.println(s1+"\t"+s2+"\t"+s3);
  879. }
  880.  
  881. System.out.println("First()");
  882. rs.first();
  883. s1 = rs.getString(1);
  884. s2 = rs.getString(2);
  885. s3 = rs.getString(3);
  886. System.out.println(s1+"\t"+s2+"\t"+s3);
  887.  
  888. System.out.println("Last()");
  889. rs.last();
  890. s1 = rs.getString(1);
  891. s2 = rs.getString(2);
  892. s3 = rs.getString(3);
  893. System.out.println(s1+"\t"+s2+"\t"+s3);
  894.  
  895. System.out.println("Absolute(2)");
  896. rs.absolute(2);
  897. s1 = rs.getString(1);
  898. s2 = rs.getString(2);
  899. s3 = rs.getString(3);
  900. System.out.println(s1+"\t"+s2+"\t"+s3);
  901.  
  902. System.out.println("Relative(2)");
  903. rs.relative(2);
  904. s1 = rs.getString(1);
  905. s2 = rs.getString(2);
  906. s3 = rs.getString(3);
  907. System.out.println(s1+"\t"+s2+"\t"+s3);
  908.  
  909. System.out.println("Relative(-1)");
  910. rs.relative(-1);
  911. s1 = rs.getString(1);
  912. s2 = rs.getString(2);
  913. s3 = rs.getString(3);
  914. System.out.println(s1+"\t"+s2+"\t"+s3);
  915.  
  916. System.out.println("BeforeFirst()");
  917. rs.beforeFirst();
  918. rs.next();
  919. s1 = rs.getString(1);
  920. s2 = rs.getString(2);
  921. s3 = rs.getString(3);
  922. System.out.println(s1+"\t"+s2+"\t"+s3);
  923. System.out.println("AfterLast()");
  924. rs.afterLast();
  925. rs.previous();
  926. s1 = rs.getString(1);
  927. s2 = rs.getString(2);
  928. s3 = rs.getString(3);
  929. System.out.println(s1+"\t"+s2+"\t"+s3);
  930.  
  931.  
  932. }
  933. catch (Exception e)
  934. {
  935. System.out.println(e);
  936. }
  937. }
  938. }
  939.  
  940.  
  941.  
  942. 20.Program to implement Updatable Result set in JDBC.
  943.  
  944. import java.sql.*;
  945. public class update
  946. {
  947. public static void main(String args[])
  948. {
  949. try
  950. {
  951. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  952. Connection con1=DriverManager.getConnection("jdbc:odbc:g44","","");
  953. Statement st1=con1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  954.  
  955. String str="Select * from employee";
  956. ResultSet rs=st1.executeQuery(str);
  957. String s1,s2,s3,s4;
  958. int i=0;
  959. System.out.println("Before Update:-");
  960.  
  961. while(rs.next())
  962. {
  963. s1=rs.getString(1);
  964. s2=rs.getString(2);
  965. s3=rs.getString(3);
  966. s4=rs.getString(4);
  967. if(s2.equals("reena rai"))
  968. { rs.updateString(2,"reena sharma");
  969. rs.updateRow();
  970. }
  971.  
  972. System.out.println(++i+" "+s1+"\t"+s2+"\t"+s3+"\t"+s4);
  973. }
  974. // go to last row to update
  975. rs.last();
  976.  
  977. rs.updateString(1,"e06");
  978. rs.updateString(2,"vinay katiyar");
  979. rs.updateRow();
  980. System.out.println("\nAfter update:-");
  981. i=0;
  982. rs.beforeFirst();
  983. while(rs.next())
  984. { s1=rs.getString(1);
  985. s2=rs.getString(2);
  986. s3=rs.getString(3);
  987. s4=rs.getString(4);
  988. System.out.println(++i+" "+s1+"\t"+s2+"\t"+s3+"\t"+s4);
  989. }
  990. rs.close();
  991. con1.commit();
  992. con1.close();
  993. }
  994. catch(Exception e)
  995. { System.out.println("Error has occured :"+e);
  996. }
  997. }
  998. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement