Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. import java.sql. * ;
  2. import java.awt. * ;
  3. import javax.swing. * ;
  4. import java.awt.event. * ;
  5.  
  6. public class NewMeaning extends JFrame {
  7.  
  8.     JLabel nw = new JLabel("Word : ");
  9.     public JComboBox jcb = new JComboBox();
  10.     JLabel pos = new JLabel("Part of Speech : ");
  11.     public JComboBox jcb1=new JComboBox();
  12.  
  13.     JLabel lblMeaning = new JLabel("Meaning : ");
  14.     public JTextField tfMeaning = new JTextField(20);
  15.  
  16.     JLabel lblSample = new JLabel("Sample : ");
  17.     public JTextField tfSample = new JTextField(20);
  18.  
  19.     JButton btnSave = new JButton("Save"), btnClose = new JButton("Close ");
  20.  
  21.     public NewMeaning() {
  22.         setTitle("Insert New Word");
  23.         setSize(250, 400);
  24.         setLayout(new FlowLayout());
  25.         add(nw);
  26.         populateWords();
  27.         add(jcb);
  28.         add(pos);
  29.         populatePOS();
  30.         add(jcb1);
  31.        
  32.         add(lblMeaning);
  33.         add(tfMeaning);
  34.  
  35.         add(lblSample);
  36.         add(tfSample);
  37.         add(new JLabel("                       "));
  38.         add(btnSave);
  39.         add(btnClose);
  40.         setLocationRelativeTo(null);
  41.         setVisible(true);
  42.         setResizable(false);
  43.  
  44.         eventHandler();
  45.     }
  46.  
  47.     public int getWordID(String w)
  48.     {
  49.         Connection conn;
  50.         int res=0;
  51.         try {
  52.             String url = "jdbc:mysql://localhost/dictionary";
  53.             Class.forName("com.mysql.jdbc.Driver");
  54.             conn = DriverManager.getConnection(url, "root", "");
  55.             Statement stmt = conn.createStatement();
  56.             String sql =
  57.               "select * from words where word='"
  58.                           + w + "';";
  59.             ResultSet rs = stmt.executeQuery(sql);
  60.             jcb = new JComboBox();
  61.             while (rs.next()) {
  62.                 res = rs.getInt("id");
  63.             }
  64.            conn.close();
  65.            conn=null;
  66.            
  67.         } catch (Exception sqx) {
  68.             System.out.println("Error connecting...");
  69.  
  70.         }
  71.         return res;
  72.        
  73.     }
  74.    
  75.  
  76.     public int getPOSID(String w)
  77.     {
  78.         Connection conn;
  79.         int res=0;
  80.         try {
  81.             String url = "jdbc:mysql://localhost/dictionary";
  82.             Class.forName("com.mysql.jdbc.Driver");
  83.             conn = DriverManager.getConnection(url, "root", "");
  84.             Statement stmt = conn.createStatement();
  85.             String sql =
  86.               "select * from pos where plong='"
  87.                           + w + "';";
  88.             ResultSet rs = stmt.executeQuery(sql);
  89.             jcb = new JComboBox();
  90.             while (rs.next()) {
  91.                 res = rs.getInt("id");
  92.             }
  93.             conn.close();
  94.             conn=null;
  95.            } catch (Exception sqx) {
  96.             System.out.println("Error connecting...");
  97.  
  98.         }
  99.         return res;
  100.        
  101.     }
  102.    
  103.     private void populateWords() {
  104.         Connection conn;
  105.         try {
  106.             String url = "jdbc:mysql://localhost/dictionary";
  107.             Class.forName("com.mysql.jdbc.Driver");
  108.             conn = DriverManager.getConnection(url, "root", "");
  109.  
  110.             Statement stmt = conn.createStatement();
  111.             String sql = "select * from words order by word;";
  112.             ResultSet rs = stmt.executeQuery(sql);
  113.             while (rs.next()) {
  114.                 jcb.addItem(rs.getString("word"));
  115.             }
  116.             conn.close();
  117.         } catch (Exception sqx) {
  118.             System.out.println("Error connecting...");
  119.         }
  120.     }
  121.  
  122. private void populatePOS() {
  123.     Connection conn;
  124.     try {
  125.         String url = "jdbc:mysql://localhost/dictionary";
  126.         Class.forName("com.mysql.jdbc.Driver");
  127.         conn = DriverManager.getConnection(url, "root", "");
  128.  
  129.         Statement stmt = conn.createStatement();
  130.         String sql = "select * from pos;";
  131.         ResultSet rs = stmt.executeQuery(sql);
  132.         while (rs.next()) {
  133.             jcb1.addItem(rs.getString("plong"));
  134.         }
  135.         conn.close();
  136.     } catch (Exception sqx) {
  137.         System.out.println("Error connecting...");
  138.     }  
  139.  
  140. }
  141.  
  142.  
  143. private void eventHandler() {
  144.     btnSave.addActionListener(new ActionListener() {
  145.         public void actionPerformed(
  146.         ActionEvent e) {
  147.             Connection conn;
  148.             try {
  149.                 String url = "jdbc:mysql://localhost/dictionary";
  150.                 Class.forName("com.mysql.jdbc.Driver");
  151.                 conn = DriverManager.getConnection(url, "root", "");
  152.                 Statement stmt = conn.createStatement();
  153.                 String m = tfMeaning.getText();
  154.                 String s = tfSample.getText();
  155.                 String test = jcb.getSelectedItem().toString();
  156.                 int wid = getWordID(test);
  157.                 int pid = getPOSID(jcb1.getSelectedItem().toString());
  158.            
  159.                 String sql = "insert into meanings values(null,"+wid+","+pid+ ",'" + m + "');";
  160.                 stmt.executeUpdate(sql);
  161.                 conn.close();
  162.             } catch (Exception sqx) {
  163.                 System.out.println("Error connecting...");
  164.                 sqx.printStackTrace();
  165.  
  166.             }      
  167.         }
  168.     }
  169.  
  170.     );
  171. }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement