+ All Categories
Home > Documents > Siddhartha Institute of Engg. & Tech. Java Programming...

Siddhartha Institute of Engg. & Tech. Java Programming...

Date post: 03-Apr-2018
Category:
Upload: dangthien
View: 223 times
Download: 1 times
Share this document with a friend
27
Siddhartha Institute of Engg. & Tech. Java Programming Record 1 Experiment:11 Program to create 4 buttons names as Red,Green,Yellow,Blue. The background color is changed when a button clicked. Ex: If Red button clicked, background color as Red. import java.awt.*; import java.awt.event.*; public class ButtonDemo extends Frame implements ActionListener { public ButtonDemo() { setVisible(true); setSize(200,300); setLayout(new FlowLayout()); Button b1=new Button("Red"); Button b2=new Button("Green"); Button b3=new Button("Yellow"); Button b4=new Button("Blue"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); add(b1);add(b2);add(b3);add(b4); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(ae.getActionCommand().equals("Red")) setBackground(Color.RED); if(ae.getActionCommand().equals("Green")) setBackground(Color.GREEN); if(ae.getActionCommand().equals("Yellow")) setBackground(Color.YELLOW); if(ae.getActionCommand().equals("Blue")) setBackground(Color.BLUE); } public static void main(String args[]) { ButtonDemo bdemo=new ButtonDemo(); } } OUTPUT:
Transcript

Siddhartha Institute of Engg. & Tech. Java Programming Record

1

Experiment:11 Program to create 4 buttons names as Red,Green,Yellow,Blue. The background color is changed when a button clicked. Ex: If Red button clicked, background color as Red. import java.awt.*; import java.awt.event.*; public class ButtonDemo extends Frame implements ActionListener { public ButtonDemo() { setVisible(true); setSize(200,300); setLayout(new FlowLayout()); Button b1=new Button("Red"); Button b2=new Button("Green"); Button b3=new Button("Yellow"); Button b4=new Button("Blue"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); add(b1);add(b2);add(b3);add(b4); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(ae.getActionCommand().equals("Red")) setBackground(Color.RED); if(ae.getActionCommand().equals("Green")) setBackground(Color.GREEN); if(ae.getActionCommand().equals("Yellow")) setBackground(Color.YELLOW); if(ae.getActionCommand().equals("Blue")) setBackground(Color.BLUE); } public static void main(String args[]) { ButtonDemo bdemo=new ButtonDemo(); } } OUTPUT:

Siddhartha Institute of Engg. & Tech. Java Programming Record

2

Experiment :12

Develop an Applet in Java that displays a Simple Message import java.applet.*; import java.awt.*; /* <applet code=AppletDemo height=800 width=800> </applet> */ public class AppletDemo extends Applet { String str=""; public void init() { str=str+"init() "; } public void start() { str=str+"start() "; } public void paint(Graphics g) { Font f1=new Font("Monospaced",Font.ITALIC+Font.BOLD,19); g.setFont(f1); g.drawString("WelCome to Applet Demo "+str,50,100); } public void stop() { str=str+"stop() "; } public void destroy() { str=str+"destroy() "; } } OUTPUT:

Siddhartha Institute of Engg. & Tech. Java Programming Record

3

Experiment :13

Develop an applet in JAVA that receives an integer in one text field, and computes its factorial value and returns it in another text field, when the button nemed "Compute" is clicked import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code=Factorial width=300 height=300> </applet> */ public class Factorial extends Applet implements ActionListener { Button b; TextField tf1,tf2; Label n1,n2; public void init() { b=new Button("Compute"); n1=new Label("Enter Integer Number"); n2=new Label("Factorail is"); tf1=new TextField(5); tf2=new TextField(10); add(n1);add(tf1); add(n2);add(tf2); add(b); b.addActionListener(this); }

Siddhartha Institute of Engg. & Tech. Java Programming Record

4

public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("Compute")) { String s1=tf1.getText(); int v1=Integer.parseInt(s1); long f=1l; for(int i=1;i<=v1;i++) { f=f*i; } tf2.setText(String.valueOf(f)); } } } OUTPUT:

Experiment:14

Write a Program that creates User Interface to perform Integer Divisions. The user enters two

numbers in text fields, Num1 and Num2.The division of Num1 and Num2 is displayed in the result

field when the divide button clicked. If Num1 or Num2 were not integer, the program would throw a

NumberFormatException, If Num2 is Zero, and the program would throw an ArithmeticException.

Display the Exception in message box.

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class A extends JFrame implements ActionListener { JLabel l1, l2, l3; JTextField tf1, tf2, tf3; JButton b1;

Siddhartha Institute of Engg. & Tech. Java Programming Record

5

A() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); l1 = new JLabel("Welcome"); setSize(800, 400); l1 = new JLabel("Enter Number1"); add(l1); tf1 = new JTextField(10); add(tf1); l2 = new JLabel("Enter Number2"); add(l2); tf2 = new JTextField(10); add(tf2); l3 = new JLabel("Result"); add(l3); tf3 = new JTextField(10); add(tf3); b1 = new JButton("Divide"); add(b1); b1.addActionListener(this); setVisible(true); } public void actionPerformed(ActionEvent ae) { try { int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); if(b==0) throw new ArithmeticException(" Divide by Zero Error"); float c = (float) a / b; tf3.setText(String.valueOf(c)); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, ex.getMessage()); } catch (ArithmeticException ex) { JOptionPane.showMessageDialog(this, ex.getMessage()); } } } public class JavaProgram4 { public static void main(String[] args) { A a = new A(); } }

Siddhartha Institute of Engg. & Tech. Java Programming Record

6

OUTPUT:

Siddhartha Institute of Engg. & Tech. Java Programming Record

7

Experiment:15

Write Java Program that implements a multithread application that has three threads. First thread generates Random integer for every second and if the value is even, second thread computes the square of number and prints. If the value is odd, the third thread will print the value of cube of number. import java.util.*; class even implements Runnable { public int x; public even(int x) { this.x = x; } public void run() { System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x); } } class odd implements Runnable { public int x; public odd(int x) { this.x = x; } public void run() { System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x); } } class A extends Thread { public void run() { int num = 0; Random r = new Random(); try { for (int i = 0; i < 5; i++) { num = r.nextInt(100); System.out.println("Main Thread and Generated Number is " + num); if (num % 2 == 0) {

Siddhartha Institute of Engg. & Tech. Java Programming Record

8

Thread t1 = new Thread(new even(num)); t1.start(); } else { Thread t2 = new Thread(new odd(num)); t2.start(); } Thread.sleep(1000); System.out.println("--------------------------------------"); } } catch (Exception ex) { System.out.println(ex.getMessage()); } } } public class JavaProgram5 { public static void main(String[] args) { A a = new A(); a.start(); } } OUTPUT: D:\JP>java JavaProgram5 Main Thread and Generated Number is 10 New Thread 10 is EVEN and Square of 10 is: 100 -------------------------------------- Main Thread and Generated Number is 14 New Thread 14 is EVEN and Square of 14 is: 196 -------------------------------------- Main Thread and Generated Number is 83 New Thread 83 is ODD and Cube of 83 is: 571787 -------------------------------------- Main Thread and Generated Number is 1 New Thread 1 is ODD and Cube of 1 is: 1 -------------------------------------- Main Thread and Generated Number is 20 New Thread 20 is EVEN and Square of 20 is: 400 --------------------------------------

Siddhartha Institute of Engg. & Tech. Java Programming Record

9

Experiment:16 Write a JAVA Program that Connects to database using JDBC and does add, delete, modify and retrieve operations. import java.sql.*; import javax.sql.*; import java.util.*; public class DataBaseRecord { public static void main(String[] args) { Connection cn; Statement st; ResultSet rs; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:siet","siet","siet"); st = cn.createStatement(); System.out.println("SIDDHARTHA Institute of Engineering & Technology"); System.out.println("------MENU------"); System.out.println("1.Insert"); System.out.println("2.EDIT"); System.out.println("3.Delete"); System.out.println("4.Display"); System.out.println("5.Exit"); System.out.println("----------------"); String opt = ""; String htno = "", sname = "", mobile = "", sql = ""; Scanner sc = new Scanner(System.in); while (opt != "5") { System.out.println("Enter Your Option"); opt = sc.next(); switch (opt) { case "1": System.out.println("Enter Htno"); htno = sc.next(); System.out.println("Enter Name"); sname = sc.next(); System.out.println("Enter Mobile"); mobile = sc.next(); sql = "insert into stu values(" + "'" + htno + "'" + "," + "'" + sname + "'" + "," + "'" +mobile + "'" + ")"; if (st.executeUpdate(sql) > 0) { System.out.println("Record Inserted"); } break; case "2":

Siddhartha Institute of Engg. & Tech. Java Programming Record

10

System.out.println("Enter Htno to Update"); htno = sc.next(); System.out.println("Enter Name"); sname = sc.next(); System.out.println("Enter Mobile"); mobile = sc.next(); sql = "update stu set sname=" + "'" + sname + "'" + "," + "mobile=" + "'" + mobile + "'" + " where htno='" + htno + "'"; if (st.executeUpdate(sql) > 0) { System.out.println("Record Updated"); } else { System.out.println("Operation Failed"); } break; case "3": System.out.println("Enter Htno to delete"); htno = sc.next(); sql = "delete from stu where Htno=" + "'" + htno + "'"; if (st.executeUpdate(sql) > 0) { System.out.println("Record deleted"); } else { System.out.println("Operation Failed"); } break; case "4": sql = "select * from stu"; rs = st.executeQuery(sql); System.out.println("Htno\tSname\tMobile"); while (rs.next()) { System.out.println(rs.getString("Htno") + "\t" + rs.getString("SName") + "\t" +rs.getString("mobile")); } rs.close(); break; case "5": opt = "5"; System.out.println("Thank You"); st.close(); cn.close(); break; default: System.out.println("Choose Option between 1 and 5 only"); } } } catch (Exception ex) { System.out.println(ex.getMessage()); } } }

Siddhartha Institute of Engg. & Tech. Java Programming Record

11

Before Program Execution: Input, IN Oracle Database create a table student as

Create table student(htno varchar2(20), stname varchar2(20), mobile varchar2(12)); OUTPUT: D:\JP>javac DataBaseRecord.java D:\JP>java DataBaseRecord SIDDHARTHA Institute of Engineering & Technology ------MENU------ 1.Insert 2.EDIT 3.Delete 4.Display 5.Exit ---------------- Enter Your Option : 1 Enter Htno: 13TP1AO501 Enter Name: SRIVANI Enter Mobile: 9849098490 Record Inserted ---------------- Enter Your Option: 1 Enter Htno: 13TP1AO502 Enter Name: A.MANOJ Enter Mobile : 7890677898 Record Inserted ---------------- Enter Your Option: 2 Enter Htno to Update: 13TP1A0502 Enter Name :A.MANOJ Enter Mobile :9010931781 Record Updated ---------------- Enter Your Option: 4 Htno Sname Mobile 13TP1AO501 SRIVANI 9849098490 13TP1AO502 A.MANOJ 9010931781 ---------------- Enter Your Option: 5 Thank You

Siddhartha Institute of Engg. & Tech. Java Programming Record

12

Experiment:17 Write a Java Program that simulates a Traffic Light. The program lets the use select one of three lights :red, yellow or Green with radiobuttons.On selecting radio button, an appropriate message with “stop” or “Ready” or “GO” should appear above the button in selected color.Intially ,there is no message shown. import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; class A extends JFrame implements ItemListener { public JLabel l1, l2; public JRadioButton r1, r2, r3; public ButtonGroup bg; public JPanel p, p1; CheckboxGroup lngGrp; public A() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(2, 1)); setSize(800, 400); p = new JPanel(new FlowLayout()); p1 = new JPanel(new FlowLayout()); l1 = new JLabel(); Font f = new Font("Verdana", Font.BOLD, 60); l1.setFont(f); add(l1); p.add(l1); add(p); l2 = new JLabel("Select Lights"); p1.add(l2); lngGrp = new CheckboxGroup(); Checkbox r1 = new Checkbox("Red Light", lngGrp, true); r1.setBackground(Color.red); p1.add(r1); r1.addItemListener(this); Checkbox r2 = new Checkbox("Yellow Light", lngGrp, true); r2.setBackground(Color.YELLOW); p1.add(r2); r2.addItemListener(this); Checkbox r3 = new Checkbox("Green Light", lngGrp, true); r3.setBackground(Color.GREEN);

Siddhartha Institute of Engg. & Tech. Java Programming Record

13

p1.add(r3); r3.addItemListener(this); add(p1); setVisible(true); } public void itemStateChanged(ItemEvent i) { Checkbox chk = lngGrp.getSelectedCheckbox(); switch (chk.getLabel()) { case "Red Light": l1.setText("STOP"); l1.setForeground(Color.red); break; case "Yellow Light": l1.setText("Ready"); l1.setForeground(Color.YELLOW); break; case "Green Light": l1.setText("GO"); l1.setForeground(Color.GREEN); break; } } } public class TLights { public static void main(String[] args) { A a = new A(); } } OUTPUT:

Siddhartha Institute of Engg. & Tech. Java Programming Record

14

Experiment: 18

Write a Java Program to create an abstract class named shape that contains two integers and an empty method named printArea. Provide three classes named Rectangle,Triangle and Circle subclass that each one of the classes extends the Class Shape.Each one of the classes contains only the method printArea() that prints the area of Shape. import java.util.*; abstract class shape { public int x,y; public abstract void printArea(); } class Rectangle1 extends shape { public void printArea() { float area; area= x * y; System.out.println("Area of Rectangle is " +area); } }

Siddhartha Institute of Engg. & Tech. Java Programming Record

15

class Triangle extends shape { public void printArea() { float area; area= (x * y) / 2; System.out.println("Area of Triangle is " + area); } } class Circle extends shape { public void printArea() { float area; area=(22 * x * x) / 7; System.out.println("Area of Circle is " + area); } } public class Shapes { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter values : "); int x1=sc.nextInt(); int y1=sc.nextInt(); Rectangle1 r = new Rectangle1(); r.x = x1; r.y = y1; r.printArea(); Triangle t = new Triangle(); t.x = x1; t.y = y1; t.printArea(); Circle c = new Circle(); c.x = x1; c.printArea(); } } OUTPUT: D:\JP>java Shapes Enter values : 7 8 Area of Rectangle is 56.0 Area of Triangle is 28.0 Area of Circle is 154.0

Siddhartha Institute of Engg. & Tech. Java Programming Record

16

Experiment:19 Suppose that a table named Table.txt is stored in a text file. The First line in the file is the header, and the remaining lines correspond rows in table. The elements are separated by commas. Write java program to display the table using Label in Grid Layout. import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class A extends Frame { public A() { setSize(600, 600); GridLayout g = new GridLayout(0, 4); setLayout(g); try { FileInputStream fin = new FileInputStream("D:\\jp\\student.txt"); Scanner sc = new Scanner(fin).useDelimiter(","); String[] arrayList; String a; while (sc.hasNextLine()) { a = sc.nextLine(); arrayList = a.split(","); for (String i : arrayList) { add(new Label(i)); } } } catch (Exception ex) { } pack(); setVisible(true); } } public class TableData { public static void main(String[] args) { A a = new A(); } }

Siddhartha Institute of Engg. & Tech. Java Programming Record

17

Input: create a text file with the name studet.txt as student.txt

OUTPUT: Java Program displays the details of student.txt file, in the Frame using Label in Grid Layout

Experiment:20 Write a Java Program that handles all mouse events and show event name at the center of the window when the mouse event is fired. (Use Adapter Classes) import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; class A extends JFrame implements MouseListener { JLabel l1; public A() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 400); setLayout(new FlowLayout()); l1 = new JLabel(); Font f = new Font("Verdana", Font.BOLD, 20); l1.setFont(f); l1.setForeground(Color.BLUE);

Siddhartha Institute of Engg. & Tech. Java Programming Record

18

l1.setAlignmentX(Component.CENTER_ALIGNMENT); l1.setAlignmentY(Component.CENTER_ALIGNMENT); add(l1); addMouseListener(this); setVisible(true); } public void mouseExited(MouseEvent m) { l1.setText("Mouse Exited"); } public void mouseEntered(MouseEvent m) { l1.setText("Mouse Entered"); } public void mouseReleased(MouseEvent m) { l1.setText("Mouse Released"); } public void mousePressed(MouseEvent m) { l1.setText("Mouse Pressed"); } public void mouseClicked(MouseEvent m) { l1.setText("Mouse Clicked"); } } public class Mevents { public static void main(String[] args) { A a = new A(); } } OUTPUT:

Siddhartha Institute of Engg. & Tech. Java Programming Record

19

Experiment: 21 Write a java program that loads names and phone numbers from the text file where data is organized as one line per record and each field in record are separated by a tab(\t).It takes a name or phone number as input and prints corresponding other value from hash table(hint: use Hash Table) import java.util.*; import java.io.*; public class PhoneDictionary { public static void main(String[] args) { try { FileInputStream fs = new FileInputStream("D:\\jp\\ph.txt"); Scanner sc = new Scanner(fs).useDelimiter("\\s+"); Hashtable<String, String> ht = new Hashtable<String, String>();

Siddhartha Institute of Engg. & Tech. Java Programming Record

20

String[] arrayList; String a; System.out.println("Welcome TO Siddhartha Institute of Engg. & Technology"); System.out.println("Student Phone numbers are"); while (sc.hasNext()) { a = sc.nextLine(); arrayList = a.split("\\s+"); ht.put(arrayList[0], arrayList[1]); System.out.println(arrayList[0] + ":" + arrayList[1]); } System.out.println("Welcome TO Siddhartha Institute of Engg. & Technology"); System.out.println("MENU"); System.out.println("1.Search by Name"); System.out.println("2.Search by Mobile"); System.out.println("3.Exit"); String opt = ""; String name, mobile; Scanner s = new Scanner(System.in); while (opt != "3") { System.out.println("Enter Your Option (1,2,3): "); opt = s.next(); switch (opt) { case "1": System.out.println("Enter Name"); name = s.next(); if (ht.containsKey(name)) { System.out.println("Mobile is " + ht.get(name)); } else { System.out.println("Not Found"); } break; case "2": System.out.println("Enter mobile"); mobile = s.next(); if (ht.containsValue(mobile)) { for (Map.Entry e : ht.entrySet()) { if (mobile.equals(e.getValue())) { System.out.println("Name is " + e.getKey()); } } } else {

Siddhartha Institute of Engg. & Tech. Java Programming Record

21

System.out.println("Not Found"); } break; case "3": opt = "3"; System.out.println("Menu Successfully Exited"); break; default: System.out.println("Choose Option betwen 1 and Three"); break; } } } catch (Exception ex) { System.out.println(ex.getMessage()); } } } INPUT loads names and phone numbers from the text file named ph.txt:

OUPUT: D:\JP>java PhoneDictionary Welcome TO Siddhartha Institute of Engg. & Technology Student Phone numbers are P.RaghuNandan:8977332085 P.Ronith:9052277577 Y.Srinish:9849039239 Y.Snikitha:9849098490 P.Rishikesh:9848098480 Welcome TO Siddhartha Institute of Engg. & Technology MENU 1.Search by Name 2.Search by Mobile 3.Exit Enter Your Option( 1,2,3): 1

Siddhartha Institute of Engg. & Tech. Java Programming Record

22

Enter Name P.RaghuNandan Mobile is 8977332085 Enter Your Option( 1,2,3): 2 Enter mobile 9849098490 Name is Y.Snikitha Enter Your Option 1,2,3 3 Menu Successfully Exited

Experiment:22 Implement above program with database instead of a text File import java.sql.*; import java.util.*; public class PhoneDB { public static void main(String[] args) { Connection cn; Statement st; ResultSet rs; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:siet","siet","siet"); System.out.println("Data Base Connected"); st = cn.createStatement(); rs = st.executeQuery("select * from phone"); Hashtable<String, String> ht = new Hashtable<String, String>(); System.out.println("Welcome TO Siddhartha Institute of Engg. & Technology"); System.out.println("Student Phone numbers are"); while (rs.next()) { ht.put(rs.getString(1), rs.getString(2)); System.out.println(rs.getString(1) + ":" + rs.getString(2)); } rs.close(); st.close(); cn.close(); System.out.println("-----------------------------------------------------"); System.out.println("Welcome TO Siddhartha Institute of Engg. & Technology"); System.out.println("Main Menu"); System.out.println("1.Search by Name"); System.out.println("2.Search by Mobile");

Siddhartha Institute of Engg. & Tech. Java Programming Record

23

System.out.println("3.Exit"); String opt = ""; String name, mobile; Scanner s = new Scanner(System.in); while (opt != "3") { System.out.println("-----------------------"); System.out.println("Enter Your Option 1,2,3"); opt = s.next(); switch (opt) { case "1": System.out.println("Enter Name"); name = s.next(); if (ht.containsKey(name)) { System.out.println("Mobile is " + ht.get(name)); } else { System.out.println("Not Found"); } break; case "2": System.out.println("Enter mobile"); mobile = s.next(); if (ht.containsValue(mobile)) { for (Map.Entry e : ht.entrySet()) { if (mobile.equals(e.getValue())) { System.out.println("Name is " + e.getKey()); } } } else { System.out.println("Not Found"); } break; case "3": opt = "3"; System.out.println("Menu Successfully Exited"); break; default: System.out.println("Choose Option betwen 1 and Three"); } } } catch (Exception ex) { System.out.println(ex.getMessage()); }

Siddhartha Institute of Engg. & Tech. Java Programming Record

24

} } INPUT from DataBase Table in Oracle :

1) Create a table name as “phone” create table phone(stname varchar2(20), stmobile varchar2(20));

2) Insert records into DataBase Table “phone” insert into phone values('P.Raghu','8977332085'); insert into phone values('Ramu','9849098490'); insert into phone values('Hari','9877335566'); insert into phone values('Jayadev','9052778877'); insert into phone values('Harika','9849044556');

3) Retrieve the table select * from phone; Table displays as

OUTPUT: D:\JP>javac PhoneDB.java D:\JP>java PhoneDB Data Base Connected Welcome TO Siddhartha Institute of Engg. & Technology Student Phone numbers are P.Raghu:8977332085 Ramu:9849098490 Hari:9877335566 Jayadev:9052778877 Harika:9849044556 ----------------------------------------------------- Welcome TO Siddhartha Institute of Engg. & Technology Main Menu 1.Search by Name 2.Search by Mobile 3.Exit ----------------------- Enter Your Option 1,2,3 1 Enter Name

Siddhartha Institute of Engg. & Tech. Java Programming Record

25

Jayadev Mobile is 9052778877 ----------------------- Enter Your Option 1,2,3 2 Enter mobile 8977332085 Name is P.Raghu ----------------------- Enter Your Option 1,2,3 3 Menu Successfully Exited

Experiment:23 Write a Java Program that takes tab separated data (one record line) from a text file and insert them into a database. import java.sql.*; import java.io.*; import java.util.*; public class Tbltodb { public static void main(String[] args) { Connection cn; Statement st; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn=DriverManager.getConnection("jdbc:odbc:siet","siet","siet"); st=cn.createStatement(); String sql=""; FileInputStream fin=new FileInputStream("D:\\JP\\emp.txt"); Scanner sc=new Scanner(fin); String[] arrayList; String a=""; int i=0; while(sc.hasNext()) { a=sc.nextLine(); arrayList =a.split("\\s+"); sql="insert into employee values("+"'"+arrayList[0]+"','"+arrayList[1]+"','"+arrayList[2]+"')"; st.execute(sql); i++; System.out.println(arrayList[0]+":"+arrayList[1]+":"+arrayList[2]);

Siddhartha Institute of Engg. & Tech. Java Programming Record

26

} System.out.println(i+" Records are inserted"); st.close(); cn.close(); } catch(Exception ex) { System.out.println(ex.getMessage()); } } } Input:

1) Create a table employee in Oracle DataBase create table employee(empno varchar2(10), empname varchar2(20), phone varchar2(15));

2) Create a text file with emp.txt with details EmployeeID, Name and Phone numbers.

OUTPUT: D:\JP>javac Tbltodb.java D:\JP>java Tbltodb CSE1001:P.Raghu:8977332085 CSE1002:G.Uday:9849677889 CSE1003:Kavitha:8949088993 CSE1004:Swetha:7805805805 4 Records are inserted After Inserted records in the Data Base Emplyee table as Sql>select * from employee;

Siddhartha Institute of Engg. & Tech. Java Programming Record

27

Experiment:24 Prg:Write a Java Program that prints the meta data of given table. import java.sql.*; import java.util.*; public class Tblmdata { public static void main(String[] args) { Connection cn; Statement st; ResultSet rs, rs1; ResultSetMetaData rsmd; try { System.out.println("Welcome TO Siddhartha Institute of Engg. & Technology"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:siet","siet","siet"); st = cn.createStatement(); DatabaseMetaData dm = cn.getMetaData(); rs = dm.getTables(cn.getCatalog(), "%", "%", null); String s = ""; String sql = "select * from ", sql1 = ""; System.out.println("Tables are"); while (rs.next()) { sql1 = ""; System.out.println("-------Table Name: " + rs.getString(3) + "---------"); sql1 = sql + rs.getString(3); rs1 = st.executeQuery(sql1); rsmd = rs1.getMetaData(); System.out.println("Columns are "); System.out.println("Column Name\tColumn Type\tSize"); for (int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(rsmd.getColumnLabel(i) + "\t" + rsmd.getColumnTypeName(i) + "\t" +rsmd.getColumnDisplaySize(i)); } System.out.println("----------------------------------"); } rs.close(); cn.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } }


Recommended