Sunday, June 19, 2016

Java Oracle Database Connectivity

import java.sql.*;
public class JdbcAccessCreate
{
 Connection con;
 Statement st;
 public JdbcAccessCreate()
 {
  try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:abc");
   System.out.println("Database Connected");
   st=con.createStatement();
   String query="create table student(roll number,name text,contactno number)";
    st.executeUpdate(query);
    System.out.println("Table created successfully");
    }catch(Exception e)
      {
      System.out.println("Error in connection"+e);
      }
  }
  public static void main(String arg[])
  {
    new JdbcAccessCreate();
    }
}
       import java.sql.*;
public class JdbcOracleCreate
{
 Connection con;
 Statement st;
 public JdbcOracleCreate()
 {
  try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:abc","scott","tiger");
   System.out.println("Database Connected");
   st=con.createStatement();
   String query="create table student_xx(roll number(4),name varchar2(20),contactno number(10))";
    st.executeUpdate(query);
    System.out.println("Table created successfully");
    }catch(Exception e)
      {
      System.out.println("Error in connection"+e);
      }
  }
  public static void main(String arg[])
  {
    new JdbcOracleCreate();
    }
}
import java.sql.*;
import java.io.*;

public class JdbcOracleInsert
{
 Connection con;
 PreparedStatement pst;
 public JdbcOracleInsert()
 {
  try{
 char ch='y';
 while(ch=='y' || ch=='Y')
 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter roll");
int r=Integer.parseInt(br.readLine());
System.out.println("Enter name");
String nm=br.readLine();
System.out.println("Enter Contactno");
int ct=Integer.parseInt(br.readLine());
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:abc","scott","tiger");
   System.out.println("Database Connected");
   pst=con.prepareStatement("insert into student_xx values(?,?,?)");
   pst.setInt(1,r);
   pst.setString(2,nm);
   pst.setInt(3,ct);
    pst.executeUpdate();
    System.out.println("Values inserted successfully\nDo u wish to insert more records");
    ch=(char)br.read();
}
    }catch(Exception e)
      {
      System.out.println("Error in connection"+e);
      }
  }
  public static void main(String arg[])
  {
    new JdbcOracleInsert();
    }
}
import java.sql.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class JdbcOracleInsertJF extends JFrame implements ActionListener
{
 Connection con;
 int r,ct1;
 String nm;
 PreparedStatement pst;
 JLabel l1,l2,l3;
 JTextField t1,t2,t3;
 JButton b;
 JPanel p,p1,mp;
 public JdbcOracleInsertJF()
 {
  l1=new JLabel("Roll");
  l2=new JLabel("Name");
  l3=new JLabel("DOB");
  t1=new JTextField(20);
  t2=new JTextField(20);
  t3=new JTextField(20);
  b=new JButton("Insert");
  b.addActionListener(this);
  p=new JPanel(new GridLayout(3,2));
  p.add(l1);
  p.add(t1);
  p.add(l2);
  p.add(t2);
  p.add(l3);
  p.add(t3);
  p1=new JPanel();
  p1.add(b);
  mp=new JPanel(new GridLayout(2,1));
 mp.add(p);
 mp.add(p1);
 Container ct=getContentPane();
      ct.setLayout(new FlowLayout());
      ct.add(mp);
        setTitle("Demo");
          setSize(400,400);
         setVisible(true);

  try{

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:abc","scott","tiger");
JOptionPane.showMessageDialog(this,"Database Connected");
   pst=con.prepareStatement("insert into student_xx values(?,?,?)");
  }catch(Exception e)
      {
      System.out.println("Error in connection"+e);
      }
  }
      public void actionPerformed(ActionEvent ae)
      {
 try{
 r=Integer.parseInt(t1.getText());
 nm=t2.getText();
ct1=Integer.parseInt(t3.getText());
pst.setInt(1,r);
pst.setString(2,nm);
pst.setInt(3,ct1);
pst.executeUpdate();
JOptionPane.showMessageDialog(this,"Record inserted ");
}catch(Exception e)
      {
     JOptionPane.showMessageDialog(this,"Error is "+e);
      }

   }
  public static void main(String arg[])
  {
    new JdbcOracleInsertJF();
    }
}
import java.sql.*;
public class JdbcOracleSelect
{
 Connection con;
 Statement st;
 ResultSet rs;
 public JdbcOracleSelect()
 {
  try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:abc","scott","tiger");
   System.out.println("Database Connected");
   st=con.createStatement();
   String query="select * from stud_ash";
    rs=st.executeQuery(query);
    System.out.println("Roll \t Name \t Cont No.");
    while(rs.next())
    {
System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
    }catch(Exception e)
      {
      System.out.println("Error in connection"+e);
      }
  }
  public static void main(String arg[])
  {
    new JdbcOracleSelect();
    }
}
import java.sql.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.util.*;
public class JdbcOracleSelectAll extends JFrame implements ActionListener
{
 Connection con;
  ResultSet rs;
 Statement st;
ResultSetMetaData rsmd;
 JLabel l1,l2;
 JTable jtab;
 JScrollPane jsp;
 JTextField t1;
 JButton b_execute;
 JPanel p1,p2,mp;
 Vector head,row,data;
 public JdbcOracleSelectAll()
 {
  l1=new JLabel("SQL");
  l2=new JLabel("Details are");
   t1=new JTextField(20);
  b_execute=new JButton("Execute");
  b_execute.addActionListener(this);
  p1=new JPanel();
  jtab=new JTable();
  int vsp=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
  int hsp=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
  jsp=new JScrollPane(jtab,vsp,hsp);

  p1.add(l1);
  p1.add(t1);
  p1.add(b_execute);
  p2=new JPanel();
  p2.add(l2);
  p2.add(jsp);
  mp=new JPanel();
 Container ct=getContentPane();

      ct.add(p1,BorderLayout.NORTH);
      ct.add(p2,BorderLayout.CENTER);
        setTitle("Demo");
          setSize(550,400);
         setVisible(true);

  try{

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:abc","scott","tiger");
JOptionPane.showMessageDialog(this,"Database Connected");
  }catch(Exception e)
      {
    JOptionPane.showMessageDialog(this,"Error in connection"+e);
      }
}
  public Vector displayData(String query)
  {
 try{
 st=con.createStatement();
 rs=st.executeQuery(query);
rsmd=rs.getMetaData();
 int cols=rsmd.getColumnCount();
 head=new Vector();
 for(int i=1;i<=cols;i++)
  {
  head.add(rsmd.getColumnName(i));
  }
  data=new Vector();
  data.add(head);
  while(rs.next())
  {
  row=new Vector();
for(int i=1;i<=cols;i++)
  {
  row.add(rs.getString(i));
  }
  data.add(row);
  }

 }catch(Exception e)
 {
 head=new Vector();
 head.add("No data found");
 data=new Vector();
 data.add(head);
      }
      return data;
}
   public void actionPerformed(ActionEvent ae)
      {
 if(ae.getSource()==b_execute)
 {

try
{

String query=t1.getText();
data=displayData(query);
head=(Vector)data.elementAt(0);
data.removeElementAt(0);
DefaultTableModel dtm=new DefaultTableModel(data,head);
jtab.setModel(dtm);

}catch(Exception e)
 {
    JOptionPane.showMessageDialog(this,"Error in execute "+e);
 }
}
}

  public static void main(String arg[])
  {
    new JdbcOracleSelectAll();
    }
}
import java.sql.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class JdbcOracleSelectJF extends JFrame implements ActionListener
{
 Connection con;
  ResultSet rs;
 Statement st;
  PreparedStatement pst;
 JLabel l1,l2,l3;
 JTextField t1,t2,t3;
 JButton b_insert,b_first,b_next,b_pre,b_last,b_clear;
 JPanel p,p1,p2,mp;
 public JdbcOracleSelectJF()
 {
  l1=new JLabel("Roll");
  l2=new JLabel("Name");
  l3=new JLabel("Contactno");
  t1=new JTextField(20);
  t2=new JTextField(20);
  t3=new JTextField(20);
  b_insert=new JButton("Insert");
  b_insert.addActionListener(this);
  b_first=new JButton("  <<  ");
    b_first.addActionListener(this);
  b_pre=new JButton("  <  ");
    b_pre.addActionListener(this);
  b_next=new JButton("  >  ");
    b_next.addActionListener(this);
    b_last=new JButton("  >>  ");
 b_last.addActionListener(this);
 b_clear=new JButton(" Clear");
 b_clear.addActionListener(this);
    p=new JPanel(new GridLayout(3,2));
  p.add(l1);
  p.add(t1);
  p.add(l2);
  p.add(t2);
  p.add(l3);
  p.add(t3);
  p1=new JPanel();
  p1.add(b_insert);
  p1.add(b_clear);
  p2=new JPanel();
  p2.add(b_first);
  p2.add(b_pre);
  p2.add(b_next);
  p2.add(b_last);
  mp=new JPanel(new GridLayout(3,1));
 mp.add(p);
 mp.add(p1);
 mp.add(p2);
 Container ct=getContentPane();
      ct.setLayout(new FlowLayout());
      ct.add(mp);
        setTitle("Demo");
          setSize(550,400);
         setVisible(true);

  try{

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:abc","scott","tiger");
JOptionPane.showMessageDialog(this,"Database Connected");
   st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
  rs=st.executeQuery("select * from student_xx");
  }catch(Exception e)
      {
    JOptionPane.showMessageDialog(this,"Error in connection"+e);
      }
}
  public void display()
  {
 try{
 t1.setText(""+rs.getInt(1));
 t2.setText(""+rs.getString(2));
 t3.setText(""+rs.getInt(3));
 }catch(Exception e)
 {
 JOptionPane.showMessageDialog(this,"Error in display "+e);
      }
}
   public void actionPerformed(ActionEvent ae)
      {
 if(ae.getSource()==b_clear)
 {
 t1.setText("");
 t2.setText("");
 t3.setText("");
 t1.requestFocus();
 }
if(ae.getSource()==b_insert)
{
try
{
int r=Integer.parseInt(t1.getText());
String nm=t2.getText();
int ct=Integer.parseInt(t3.getText());
pst=con.prepareStatement("insert into student_xx values(?,?,?)");
pst.setInt(1,r);
pst.setString(2,nm);
pst.setInt(3,ct);
pst.executeUpdate();
JOptionPane.showMessageDialog(this,"Record inserted successfully");
}catch(Exception e)
 {
    JOptionPane.showMessageDialog(this,"Error in insert "+e);
 }
}
 if(ae.getSource()==b_first)
 {
try{
rs.first();
display();

}catch(Exception e)
     {
    JOptionPane.showMessageDialog(this,"Error in first  "+e);
     }
 }
       if(ae.getSource()==b_pre)
   {
  try{
if(rs.previous()==false)
  {
JOptionPane.showMessageDialog(this,"This is the 1st Record of File ");
}else

  display();

 }catch(Exception e)
       {
      JOptionPane.showMessageDialog(this,"Error in pre "+e);
      }
  }
       if(ae.getSource()==b_next)
   {
  try{
  if(rs.next()==false)
  {
JOptionPane.showMessageDialog(this,"This is the Last Record ");
}else
  display();

 }catch(Exception e)
       {
      JOptionPane.showMessageDialog(this,"Error in next "+e);
      }
  }
  if(ae.getSource()==b_last)
     {
    try{
    rs.last();
    display();

   }catch(Exception e)
         {
        JOptionPane.showMessageDialog(this,"Error in last "+e);
        }
  }
}
  public static void main(String arg[])
  {
    new JdbcOracleSelectJF();
    }
}

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...