Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. /*vvv WinStudent List vvv*/
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.util.*;
  7.  
  8. public class WinStudentList implements ActionListener{
  9.     private JLabel lNo;
  10.     private JLabel lFName;
  11.     private JLabel lLName;
  12.     private JTextField tNo;
  13.     private JTextField tFName;
  14.     private JTextField tLName;
  15.     private JButton next;
  16.     private JButton prev;
  17.     private JFrame win;
  18.     private ArrayList<Student> list;
  19.     private DBAccess db;
  20.     private int counter=0;
  21.  
  22.     public void actionPerformed(ActionEvent e){
  23.         if(e.getActionCommand().equals("next")){
  24.             if(list.size()>counter)
  25.             counter++;
  26.         }else
  27.         if(e.getActionCommand().equals("prev")){
  28.             counter--;
  29.         }
  30.         tFName.setText(list.get(counter).getF());
  31.         tLName.setText(list.get(counter).getL());
  32.         tNo.setText(""+list.get(counter).getN());
  33.     }
  34.  
  35.     public WinStudentList(){
  36.         list = new ArrayList<Student>();
  37.         db = new DBAccess();
  38.         list = db.getStudent();
  39.         lNo=new JLabel("No: ");
  40.         lFName=new JLabel("First: ");
  41.         lLName=new JLabel("Last: ");
  42.         tNo=new JTextField();
  43.         tFName=new JTextField();
  44.         tLName=new JTextField();
  45.         next=new JButton("->");
  46.         prev=new JButton("<-");
  47.         next.addActionListener(this);
  48.         prev.addActionListener(this);
  49.         next.setActionCommand("next");
  50.         prev.setActionCommand("prev");
  51.         win=new JFrame();
  52.         win.setLayout(new GridLayout(4,2));
  53.         win.add(lNo);
  54.         win.add(tNo);
  55.         win.add(lFName);
  56.         win.add(tFName);
  57.         win.add(lLName);
  58.         win.add(tLName);
  59.         win.add(prev);
  60.         win.add(next);
  61.         tNo.setEditable(false);
  62.         tFName.setEditable(false);
  63.         tLName.setEditable(false);
  64.         tFName.setText(list.get(0).getF());
  65.         tLName.setText(list.get(0).getL());
  66.         tNo.setText(""+list.get(0).getN());
  67.         win.pack();
  68.         win.setVisible(true);
  69.         win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.     }
  71.     public static void main(String args[]){
  72.         new WinStudentList();
  73.     }
  74. }
  75.  
  76. /*vvv DBAccess vvv*/
  77. import java.sql.*;
  78. import oracle.jdbc.pool.*;
  79. import java.io.*;
  80. import java.util.*;
  81.  
  82. public class DBAccess{
  83.    
  84.     //The Connect String
  85.     static final String connect_string=
  86.                     "jdbc:oracle:thin:system/db_147@//localhost:1521/XE";
  87.     //The query we will execute
  88.      String query = "select 'Hello JDBC: ' || sysdate from dual";
  89.     //The Connection to the database
  90.     private Connection conn;
  91. /* 
  92.     public static void main(String args[]){
  93.         new DBAccess();
  94.     }
  95. */ 
  96.     public DBAccess(){
  97.         try{
  98.             //Seeif we need to open the connection to the database
  99.             if(conn==null){
  100.                 //Create a OracleDataSOurce instance and set URL
  101.                 OracleDataSource ods = new OracleDataSource();
  102.                 ods.setURL(connect_string);
  103.                 //Connect to the database
  104.                 System.out.println("Connecting to " + connect_string + "\n");
  105.                 conn=ods.getConnection();
  106.                 System.out.println("Connected\n");
  107.             }
  108.             //Create a statement
  109.             Statement stmt = conn.createStatement();
  110.             //Execute the query
  111.             System.out.println("Executing query" + query + "\n");
  112.             ResultSet rset = stmt.executeQuery(query);
  113.             //Dump the result
  114.             while(rset.next()){
  115.                 System.out.println(rset.getString (1) + "\n");
  116.                 //System.out.println(rset.getString("FName"));
  117.                 //System.out.println(rset.getString (" LName") + "\n");
  118.             }
  119.             //We're done
  120.             System.out.println("done.\n");
  121.         }
  122.         catch(Exception e){
  123.             System.out.println(e.getMessage() + "\n");
  124.         }
  125.     }//DB constructor
  126.    
  127.     public void addStudent(String No, String Fname, String Lname){
  128.         //Create a statement
  129.         try{
  130.             Statement stmt = conn.createStatement();
  131.             query="insert into students(SNO, FNAME, LNAME) values("+No+",'"+Fname+"','"+Lname+"')";
  132.             //Execute the query
  133.             System.out.println("Executing query" + query + "\n");
  134.             stmt.executeQuery(query);
  135.         }catch(Exception e){
  136.             System.out.println(e);
  137.         }
  138.     }
  139.    
  140.     public ArrayList getStudent(){
  141.         ArrayList<Student> list= new ArrayList<Student>();
  142.         try{
  143.             Statement stmt = conn.createStatement();
  144.             query="select sno, fname, lname from students";
  145.             ResultSet rset=stmt.executeQuery(query);
  146.             Student s;
  147.             while(rset.next()){
  148.                 s = new Student(
  149.                         rset.getInt("SNO"),
  150.                         rset.getString("FNAME"),
  151.                         rset.getString("LNAME")
  152.                         );
  153.                 list.add(s);
  154.             }
  155.  
  156.         }catch(Exception e){
  157.             System.out.println(e);
  158.         }
  159.         return list;
  160.     }
  161.    
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement