Posts

Java program to search some data in file using Hash table

import java.io.*; import java.util.*; public class Searching_in_file { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.out.println("Enter name :"); Scanner s = new Scanner(System.in); String n = s.next(); String [] a; FileInputStream fis = new FileInputStream("G://test.txt"); Scanner sf =new Scanner(fis).useDelimiter(","); Hashtable<String,String> ht = new Hashtable<String,String>(); while(sf.hasNext()){ a=sf.nextLine().split(","); ht.put(a[0],a[1]); } if(ht.containsKey(n)){ System.out.println("Phone number is :"+ht.get(n)); } else{ System.out.println("NO RECORD FOUND"); } ht.clear(); } }

Java program to search for a field's data

import java.sql.*; import java.util.*; public class Searching_in_db { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub System.out.println("Enter name :"); Scanner s = new Scanner(System.in); String n = s.next(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/jdbc","root","root"); Statement st= con.createStatement(); ResultSet rs = st.executeQuery("select * from data where name='"+n+"';"); while(rs.next()){ System.out.println("Phone number of "+n+" is :"+rs.getInt(2)); //second coulumn of my table is phone number } } }

Java program to insert data to database from a file

import java.io.*; import java.sql.*; import java.util.*; public class TestFile_to_DB { public static void main(String[] args) throws Exception { Connection con = DriverManager.getConnection("jdbc:mysql://localhost/jdbc","root","root"); Statement s = con.createStatement(); FileInputStream fin = new FileInputStream("G://test.txt"); //you should manully enter data to you file // LIKE SO: //java,lab //ds,lab Scanner sc = new Scanner(fin); while(sc.hasNext()){ StringTokenizer st = new StringTokenizer(sc.nextLine(), "," );  / /here you use any delimiter of your choice       while (st.hasMoreTokens()) {           s.execute("insert into testdb values(' "+st.nextToken()+" ',' "+st.nextToken()+" ');"); //here my table consistes of two string fields      }  } } //after the program terminates open your table to see the output }

Java program that prints meta data of table

import java.sql.*; public class MetaData { public static void main(String[] args) { try{  Connection con=DriverManager.getConnection("jdbc:mysql://localhost/jdbc","root","root");    PreparedStatement ps=con.prepareStatement("select * from login");  ResultSet rs=ps.executeQuery();  ResultSetMetaData rsmd=rs.getMetaData();    System.out.println("Total columns: "+rsmd.getColumnCount());  for(int i=1;i<=rsmd.getColumnCount();i++){ System.out.println("Column Name : "+rsmd.getColumnName(i));  System.out.println("Column Type Name : "+rsmd.getColumnTypeName(i));  } con.close();  }catch(Exception e){ System.out.println(e);}  }  }

Java program to perform JDBC operations

import java.sql.*; public class Jdbc_operations { public static void main(String[] args) throws Exception { Connection con= DriverManager.getConnection("jdbc:mysql://localhost/jdbc","root","root"); Statement s =con.createStatement(); //my table name is data and has two columns name and salary String sq1="insert into data values (' " +"king"+ " ', ' " +1000+" ') "; int s1 = s.executeUpdate(sq1); String sq2="update data set name = ' "+"ban"+" 'where name=' "+"kiran"+" ';"; int s2 = s.executeUpdate(sq2); String sq3="delete from data where name =' "+"can"+" ';"; int s3 = s.executeUpdate(sq3); ResultSet rs = s.executeQuery("select * from data"); while(rs.next()){ System.out.println(rs.getString(1)+"         "+rs.getInt(2))...

Applet that computes factorial

import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class FactorialApplet extends Applet implements ActionListener{ TextField tf1,tf2; Button b; int fact=1,n; public void init(){ setBackground(Color.LIGHT_GRAY); b= new Button("COMPUTE"); tf1=new TextField(10); tf2=new TextField(10); tf1.setBounds(10, 20, 150, 30); b.setBounds(50, 60, 100, 25); tf2.setBounds(10,110,150,30); b.addActionListener(this); add(tf1); add(tf2); add(b); setLayout(null); } public void paint(Graphics g){ g.drawString("Enter a number :", 10, 10); g.drawString("Factorial is :", 10, 100); } @Override public void actionPerformed(ActionEvent e) { n=Integer.parseInt(tf1.getText()); for(int i=1;i<=n;i++) fact*=i; tf2.setText(fact+""); } }

Inter Thread Communication using producer-consumer

public class InterThreadCommunication { public static void main(String[] args) { Que q = new Que(); Producer p = new Producer(q); Consumer c = new Consumer(q); p.start(); c.start(); } } class Producer extends Thread{ Que q; int data; public Producer(Que q){ this.q=q; } public void run(){ while(true){ try { q.setData(++data); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class Consumer extends Thread{ Que q; int data; public Consumer(Que q){ this.q=q; } public void run(){ while(true){ try { q.getData(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class Que{ int data; boolean flag=false; public synchronized int getData() throws InterruptedException{ while(!flag) wait(); Thread.sleep(1000); System.out.println("GET :"+data); fl...