Wednesday, February 11, 2015

Applet Java code Database Connection display one table

DatabaseApplet.java



package abc;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;


import javax.swing.*;


public class Ab extends JApplet implements ActionListener
{
 static final long serialVersionUID = 1L;
 private JButton bttnForLogIn, bttnForTime,bttnForViewingTables;
 private String server_connect,user_name,user_pwd,oracle_driver;
 private Connection conn;
 private PreparedStatement pstmt;
 private ResultSet rs;
 
 

 private boolean getConnected=true;
 private JLabel lblTitle, lblDisplayTime; 
 private JTextArea textArea;
 public void init() {
  // TODO Auto-generated method stub

  setSize(800, 700);
  if(getConnected){
   setLayout(new BorderLayout());}

  add(new Ab(),BorderLayout.CENTER);



  System.out.println("Init");

 }


 public Ab()
 {
  if(getConnected){

   lblTitle = new JLabel("Oracle Database Table Data");
   lblTitle.setFont(new Font("Slab Serif", Font.BOLD, 18));
   lblTitle.setForeground(Color.red);
   lblTitle.setBounds(100, 11, 400, 60);

   oracle_driver = "oracle.jdbc.driver.OracleDriver";
   getConnected = true;
   JScrollPane scrollPane;
   getContentPane().add(scrollPane = new JScrollPane(this.textArea = new JTextArea()));
   textArea.setEditable(false);
   textArea.setBackground(Color.WHITE);
   scrollPane.setBounds(40,200, 200, 100);

   getContentPane().setBackground(Color.WHITE);
   bttnForLogIn = new JButton("Log In");
   bttnForLogIn.setBounds(21, 70, 150, 20);
   bttnForLogIn.addActionListener(this);





   bttnForTime = new JButton("Table:");
   bttnForTime.setBounds(51, 110, 150, 20);

   bttnForTime.addActionListener(this);


   lblDisplayTime = new JLabel("");
   lblDisplayTime.setFont(new Font("Times New Roman",30,12));
   lblDisplayTime.setForeground(Color.RED);
   lblDisplayTime.setBounds(180, 345, 400, 50);



   getContentPane().add(lblTitle);

   getContentPane().add(bttnForLogIn);  

   getContentPane().add(bttnForTime);
   //getContentPane().add(bttnForViewingTables);

   getContentPane().add(lblDisplayTime);


  }
 }

 public void setConnection(boolean getConnection)
 {
  getConnection=false;
 }

 public void buttonChanged(){

  bttnForLogIn.setText("Log Out");
 }
//main connection
 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource() == bttnForLogIn)
  {
   user_name = "system";
   user_pwd = "manager";
   server_connect = "jdbc:oracle:thin:@localhost:1521:XE";
   if(getConnected)
   {
    if(testConnection() == 0)
    {

     JOptionPane.showMessageDialog(null, "Successful Completion");

     bttnForTime.setEnabled(true);

     buttonChanged();
     getConnected = false;
    } else
    {
     JOptionPane.showMessageDialog(null, "Oracle DB connection fail");
     bttnForTime.setEnabled(false);


    }
   } else 
   {
    try
    {
     conn.close();
    }
    catch(SQLException e1)
    {
     e1.printStackTrace();
    }
    bttnForLogIn.setText("Login");
    setConnection(getConnected);

    JOptionPane.showMessageDialog(null, "You have logged out! Have a good day!");
    
    
   }
  }else
   if(e.getSource() == bttnForTime)
    Table1();


   else {
    if(e.getSource() == bttnForViewingTables){

     Table1();
          
    }}




 }
 


 

 public int testConnection()
 {
  int flag = 0;
  try
  {
   Class.forName(oracle_driver);
  }
  catch(Exception e)
  {
   JOptionPane.showMessageDialog(null, e.getMessage());
  }
  try
  {
   conn = DriverManager.getConnection(server_connect, user_name, user_pwd);
  }
  catch(SQLException e)
  {
   JOptionPane.showMessageDialog(null, e.getMessage());
   flag = -1;
  }
  return flag;
 }
 

 private void Table1()  
 {
  try  
  {
   String stmtQuery = "select * from user_details";
   pstmt = conn.prepareStatement(stmtQuery);
   rs = pstmt.executeQuery();
   while(rs.next())
   {
    
    id=rs.getString(1);
     name = rs.getString(2);
     
     textArea.append("|"+id+" | "+name+"|"+"\n");
    // textArea.append("");
    
    System.out.println(name);
    
   }
   
   rs.close();
   pstmt.close();
  } catch(SQLException e2)
  {
   e2.printStackTrace();
  }
  
  
 }
 static String id;
 static String name;


 @Override
 public void destroy() {
  // TODO Auto-generated method stub
  System.out.println("destroy");
 }

 @Override
 public void start() {
  // TODO Auto-generated method stub
  System.out.println("Start");
 }

 @Override
 public void stop() {
  // TODO Auto-generated method stub
  if(!getConnected)
   System.out.println("Stop");

 }

} 

Run and execute ; change ur Oracle URL,Username, Password, Tablename

No comments :

Post a Comment