Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql. * ;
- import java.awt. * ;
- import javax.swing. * ;
- import java.awt.event. * ;
- public class NewMeaning extends JFrame {
- JLabel nw = new JLabel("Word : ");
- public JComboBox jcb = new JComboBox();
- JLabel pos = new JLabel("Part of Speech : ");
- public JComboBox jcb1=new JComboBox();
- JLabel lblMeaning = new JLabel("Meaning : ");
- public JTextField tfMeaning = new JTextField(20);
- JLabel lblSample = new JLabel("Sample : ");
- public JTextField tfSample = new JTextField(20);
- JButton btnSave = new JButton("Save"), btnClose = new JButton("Close ");
- public NewMeaning() {
- setTitle("Insert New Word");
- setSize(250, 400);
- setLayout(new FlowLayout());
- add(nw);
- populateWords();
- add(jcb);
- add(pos);
- populatePOS();
- add(jcb1);
- add(lblMeaning);
- add(tfMeaning);
- add(lblSample);
- add(tfSample);
- add(new JLabel(" "));
- add(btnSave);
- add(btnClose);
- setLocationRelativeTo(null);
- setVisible(true);
- setResizable(false);
- eventHandler();
- }
- public int getWordID(String w)
- {
- Connection conn;
- int res=0;
- try {
- String url = "jdbc:mysql://localhost/dictionary";
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, "root", "");
- Statement stmt = conn.createStatement();
- String sql =
- "select * from words where word='"
- + w + "';";
- ResultSet rs = stmt.executeQuery(sql);
- jcb = new JComboBox();
- while (rs.next()) {
- res = rs.getInt("id");
- }
- conn.close();
- conn=null;
- } catch (Exception sqx) {
- System.out.println("Error connecting...");
- }
- return res;
- }
- public int getPOSID(String w)
- {
- Connection conn;
- int res=0;
- try {
- String url = "jdbc:mysql://localhost/dictionary";
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, "root", "");
- Statement stmt = conn.createStatement();
- String sql =
- "select * from pos where plong='"
- + w + "';";
- ResultSet rs = stmt.executeQuery(sql);
- jcb = new JComboBox();
- while (rs.next()) {
- res = rs.getInt("id");
- }
- conn.close();
- conn=null;
- } catch (Exception sqx) {
- System.out.println("Error connecting...");
- }
- return res;
- }
- private void populateWords() {
- Connection conn;
- try {
- String url = "jdbc:mysql://localhost/dictionary";
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, "root", "");
- Statement stmt = conn.createStatement();
- String sql = "select * from words order by word;";
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- jcb.addItem(rs.getString("word"));
- }
- conn.close();
- } catch (Exception sqx) {
- System.out.println("Error connecting...");
- }
- }
- private void populatePOS() {
- Connection conn;
- try {
- String url = "jdbc:mysql://localhost/dictionary";
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, "root", "");
- Statement stmt = conn.createStatement();
- String sql = "select * from pos;";
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- jcb1.addItem(rs.getString("plong"));
- }
- conn.close();
- } catch (Exception sqx) {
- System.out.println("Error connecting...");
- }
- }
- private void eventHandler() {
- btnSave.addActionListener(new ActionListener() {
- public void actionPerformed(
- ActionEvent e) {
- Connection conn;
- try {
- String url = "jdbc:mysql://localhost/dictionary";
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, "root", "");
- Statement stmt = conn.createStatement();
- String m = tfMeaning.getText();
- String s = tfSample.getText();
- String test = jcb.getSelectedItem().toString();
- int wid = getWordID(test);
- int pid = getPOSID(jcb1.getSelectedItem().toString());
- String sql = "insert into meanings values(null,"+wid+","+pid+ ",'" + m + "');";
- stmt.executeUpdate(sql);
- conn.close();
- } catch (Exception sqx) {
- System.out.println("Error connecting...");
- sqx.printStackTrace();
- }
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement