Guest User

Untitled

a guest
Aug 18th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.ComponentOrientation;
  3. import java.awt.Container;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyAdapter;
  9. import java.awt.event.KeyEvent;
  10. import java.sql.*;
  11.  
  12. import javax.swing.ButtonGroup;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16. import javax.swing.JRadioButton;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTextField;
  19. import javax.swing.JTextPane;
  20. import javax.swing.text.StyledDocument;
  21.  
  22.  
  23. public class AClass extends JPanel implements ActionListener {
  24.  
  25.     public class Dbc {
  26.         Connection con;
  27.         Statement stm;
  28.         ResultSet rs;
  29.         ResultSet create;
  30.        
  31.         String[] name = new String[30];
  32.         public String[] query(String q, int k){
  33.             try {
  34.                 Class.forName("oracle.jdbc.driver.OracleDriver");
  35.             } catch (ClassNotFoundException e) {
  36.                 System.out.println(e);
  37.                 System.exit(-1);
  38.             }
  39.            
  40.             try{
  41.                 con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "user", "password");
  42.                    
  43.                 stm = con.createStatement();
  44.                
  45.                 rs = stm.executeQuery(q);
  46.                 int i=0;
  47.                 while(rs.next()){          
  48.                    
  49.                     name[i]=rs.getString(k);
  50.                     i++;
  51.                    
  52.                 }
  53.                    
  54.                 con.close();
  55.             }catch(java.sql.SQLException e){
  56.                 System.err.println(e);
  57.                 System.exit(-1);
  58.             }
  59.             return name;
  60.            
  61.         }
  62.        
  63.     }
  64.    
  65.     private JTextField textField;
  66.     private JButton search = new JButton("C\u0103utare");
  67.     private JTextPane textPane = new JTextPane();
  68.     private JScrollPane scrollPane = new JScrollPane();
  69.     StyledDocument doc = textPane.getStyledDocument();
  70.     Dbc d = new Dbc();
  71.     JRadioButton rdModel = new JRadioButton("Caut\u0103 dup\u0103 model");
  72.     JRadioButton rdBrand = new JRadioButton("Caut\u0103 dup\u0103 marc\u0103");
  73.     private final ButtonGroup buttonGroup = new ButtonGroup();
  74.    
  75.     public AClass(){
  76.        
  77.         textPane.addKeyListener(new KeyAdapter() {
  78.            
  79.             public void keyPressed(KeyEvent e){
  80.                 char c = e.getKeyChar();
  81.                 if (c=='c'){
  82.                     textPane.setText("");
  83.                     textPane.setCaretPosition(0);
  84.                    
  85.                 }
  86.             }
  87.         });
  88.        
  89.         setBackground(Color.green);
  90.         setLayout(null);
  91.         textField = new JTextField();
  92.         textField.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  93.         textField.setToolTipText("Scrie textul c\u0103utat");
  94.         textField.setColumns(10);
  95.         textField.setBounds(182, 5, 86, 20);
  96.         textField.setLocation(750, 50);
  97.         add(textField);
  98.        
  99.         search.setBounds(840, 47, 90, 25);
  100.         search.addActionListener(this);
  101.         add(search);
  102.        
  103.         textPane.setEditable(false);
  104.         textPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  105.         textPane.setBounds(200,100,800,300);
  106.         textPane.setLocation(new Point(200, 300));
  107.        
  108.         scrollPane.setBounds(300, 150, 650, 550);
  109.         scrollPane.setViewportView(textPane);
  110.         add(scrollPane);
  111.        
  112.         rdBrand.setBackground(Color.GREEN);
  113.         buttonGroup.add(rdBrand);
  114.         rdBrand.setBounds(600, 40, 130, 23);
  115.        
  116.         add(rdBrand);
  117.        
  118.        
  119.         rdModel.setBackground(Color.GREEN);
  120.         buttonGroup.add(rdModel);
  121.         rdModel.setBounds(600, 60, 130, 23);
  122.         add(rdModel);
  123.     }
  124.    
  125.     public void paintComponent(Graphics g){
  126.         g.setColor(Color.black);
  127.     }
  128.    
  129.     public void actionPerformed(ActionEvent e) {
  130.        
  131.         Object source = e.getSource();
  132.        
  133.         if(source==search){
  134.            
  135.            
  136.             String[] b = new String[30];
  137.             int id=0;
  138.             String s = "";
  139.             String r = "";
  140.            
  141.             if(rdBrand.isEnabled()){
  142.                
  143.                
  144.                 String a = textField.getText();
  145.            
  146.                 if(a.equals("Something") || a.equals("something"))
  147.                     id=1;
  148.                    
  149.                 if(a.equals("Volkswagen") || a.equals("volkswagen"))
  150.                     id=2;
  151.                 if(a.equals("bmw") || a.equals("BMW"))
  152.                     id=3;
  153.                
  154.                 for(int i=0;i<30;i++)
  155.                     b[i]=d.query("SELECT * FROM Mod WHERE ID ='"+id+"'", 2)[i];
  156.                
  157.                
  158.                 for(int k=0;k<30;k++)
  159.                     if(b[k]!=null){
  160.                         try{
  161.                             doc.insertString(0, b[k]+"\n", null);
  162.                         }catch(Exception e3) { System.out.println(e3); }
  163.                 }
  164.                
  165.             }
  166.            
  167.             if(rdModel.isEnabled()){
  168.                 String a = textField.getText();
  169.                
  170.                
  171.                 if(a.equals("Logan") || a.equals("logan"))
  172.                     s="Logan";
  173.                 if(a.equals("m3") || a.equals("M3") || a.equals("M 3"))
  174.                     s="M3";
  175.                 if(a.equals("3") || a.equals("Seria 3") || a.equals("seria 3") || a.equals("3 Series") || a.equals("3 series"))
  176.                     s="3 Series";
  177.                 if(a.equals("1310"))
  178.                     s="1310";
  179.                 if(a.equals("1300"))
  180.                     s="1300";
  181.                 if(a.equals("polo") || a.equals("Polo"))
  182.                     s="Polo";
  183.                 if(a.equals("golf") || a.equals("Golf"))
  184.                     s="Golf";
  185.                 if(a.equals("passat") || a.equals("Passat"))
  186.                     s="Passat";
  187.                 if(a.equals("X6") || a.equals("x6"))
  188.                     s="X6";
  189.                
  190.                 r=d.query("SELECT Descriere from mod where model = '"+s+"'", 1)[0];
  191.                
  192.                 try{
  193.                     doc.insertString(0, "  "+r+"\n", null);
  194.                 }catch(Exception e2) { System.out.println(e2); }
  195.             }
  196.         }
  197.        
  198.        
  199.     }
  200.    
  201.    
  202.     public class Frame {
  203.        
  204.        
  205.         public void main(String[] args) {
  206.            
  207.             new Frame();
  208.         }
  209.        
  210.         public Frame(){
  211.             JFrame gui = new JFrame();
  212.             gui.setTitle("Title");
  213.             gui.setSize(1000, 900);
  214.            
  215.             gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  216.            
  217.             AClass panel = new AClass();
  218.            
  219.            
  220.             Container pane = gui.getContentPane();
  221.            
  222.            
  223.             pane.add(panel);
  224.            
  225.            
  226.            
  227.            
  228.            
  229.             gui.setVisible(true);
  230.         }
  231.  
  232.     }
  233. }
Add Comment
Please, Sign In to add comment