Results 1 to 3 of 3
  1. #1
    Enriketa is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default JTable and JTextAreas interaction

    Hi,
    I am trying to do a project for several days now. I have a JTable wich gets info from my MYSQL database table student and below that i have several JTextFields. I have a button "SAVE" wich adds new Students from the JtextFiels. I have already done the JTable display, but now when i want to add a new student, after i fill in the JTextFields, and click the SAVE button , it only adds me an empty row to my database table.. From what i notice that happens becouse i create another connection to the database or smth similar as at the instance i type SAVE it Connects to my DB all over again and the problem is i dont know how to fix my code.. PLEASE GUYS I NEED HELP! what i am doing wrong ??


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import java.sql.Statement;


    public class Lidhja
    {
    private Connection connection;
    private Statement stm;

    public Lidhja()
    {
    try
    {
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    System.out.println("Driver Loaded");
    connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/enriketaenterprise","root","121ireeri121");
    System.out.println("DB connected");

    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }

    }

    public Connection getConnection(){return connection;}

    public Statement getStatement() throws SQLException{
    stm=connection.createStatement();
    return stm;}

    }

    //*************************************************
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.Vector;
    import java.awt.*;

    public class SectionB //extends TableResult
    {
    private JLabel studentId= new JLabel("STUDENT ID");
    private JLabel studentEmer= new JLabel("EMER");
    private JLabel studentMbiemer= new JLabel("MBIEMER");
    private JLabel studentDtl= new JLabel("DATELINDJA");
    private JLabel studentAdr= new JLabel("ADRESA");
    private JLabel studentPrf= new JLabel("PROFILI");
    private JLabel studentStatus= new JLabel("STATUS CIV");
    private JLabel studentQyteti= new JLabel("QYTETI");

    public JTextField studentIdf= new JTextField(5);
    public JTextField studentEmerf= new JTextField(25);
    public JTextField studentMbiemerf= new JTextField(25);
    public JTextField studentDtlf= new JTextField(15);
    public JTextField studentAdrf= new JTextField(50);
    public JTextField studentPrff= new JTextField(40);
    public JTextField studentStatusf= new JTextField(1);
    public JTextField studentQytetif= new JTextField(40);

    private JPanel labels= new JPanel();
    private JPanel textfields= new JPanel();


    public JPanel createMainPanel()

    { createLabels();
    createTextFields();
    JPanel panel=new JPanel();
    panel.setLayout(new GridLayout(1,3));
    panel.add(createLabels());
    panel.add(createTextFields());

    return panel;

    }
    public JPanel createLabels()
    {
    labels.setLayout(new GridLayout(8,1));
    labels.add(studentId);
    labels.add(studentEmer);
    labels.add(studentMbiemer);
    labels.add(studentDtl);
    labels.add(studentAdr);
    labels.add(studentPrf);
    labels.add(studentStatus);
    labels.add(studentQyteti);

    return labels;
    }

    public JPanel createTextFields()
    {
    textfields.setLayout(new GridLayout(8,1));
    textfields.add(studentIdf);
    textfields.add(studentEmerf);
    textfields.add(studentMbiemerf);
    textfields.add(studentDtlf);
    textfields.add(studentAdrf);
    textfields.add(studentPrff);
    textfields.add(studentStatusf);
    textfields.add(studentQytetif);

    return textfields;
    }

    public void setEmri(String emr)
    {
    studentEmerf.setText(emr);
    }
    }
    //***************************************
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.Vector;
    import java.awt.*;

    public class SectionC extends SectionB

    {

    public JPanel Butonat() throws SQLException
    {
    JPanel butonat = new JPanel();
    butonat.add(createSave());
    return butonat;
    }


    public JButton createSave()throws SQLException
    {
    JButton saveButton= new JButton("Save");

    class newListener implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {

    try {

    Lidhja lidhja=new Lidhja();
    Connection conn= lidhja.getConnection();
    String sql="INSERT INTO student(`student_id`,`student_emri`,`student_mbiem ri`,`student_ditelindja`,`student_adresa`,`student _profili`,`student_statusi_civil`,`student_qyteti` )VALUES(?,?,?,?,?,?,?,?)";

    PreparedStatement statement= conn.prepareStatement(sql);

    statement.setString(1,studentIdf.getText());
    statement.setString(2,studentEmerf.getText());
    statement.setString(3,studentMbiemerf.getText());
    statement.setString(4,studentDtlf.getText());
    statement.setString(5,studentAdrf.getText());
    statement.setString(6,studentPrff.getText());
    statement.setString(7,studentStatusf.getText());
    statement.setString(8,studentQytetif.getText());

    int rows=statement.executeUpdate();
    if (rows<0)
    {System.out.println("Shtimi NUK U BE ME SUKSES");
    }

    }
    catch (SQLException ex)
    {
    ex.printStackTrace();
    }

    }
    }

    ActionListener listener= new newListener();
    saveButton.addActionListener(listener);

    return saveButton;
    }

    }
    //*********************
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.Vector;
    import java.awt.*;

    public class TableResult //extends SectionB
    {
    private JTable resultTable;
    private SectionB fusha= new SectionB();
    private Lidhja lidhja=new Lidhja();


    public JTable createTable() throws SQLException
    {
    Connection connection=lidhja.getConnection();
    Statement stm=lidhja.getStatement();

    resultTable=new JTable();

    String sql="Select * from student";

    Vector columnName= new Vector();
    Vector<Vector<String>> data= new Vector<Vector<String>>();

    ResultSet res = stm.executeQuery(sql);

    columnName.add("S_ID");
    columnName.add("S_Emer");
    columnName.add("S_Mbiemer");
    columnName.add("S_Datelindje");
    columnName.add("S_Adrese");
    columnName.add("S_Profil");
    columnName.add("S_Gjendje_Civ");
    columnName.add("S_Qyteti");

    while(res.next())
    {
    Vector rekorde= new Vector();

    String id=res.getString(1);
    String emer=res.getString(2);
    String mbiemer=res.getString(3);
    String dtl=res.getString(4);
    String adresa=res.getString(5);
    String profili=res.getString(6);
    String status=res.getString(7);
    String qyteti=res.getString(8);

    rekorde.add(id);
    rekorde.add(emer);
    rekorde.add(mbiemer);
    rekorde.add(dtl);
    rekorde.add(adresa);
    rekorde.add(profili);
    rekorde.add(status);
    rekorde.add(qyteti);

    data.add(rekorde);


    }//while

    resultTable.setModel(new DefaultTableModel(data,columnName));

    /*class ClickedListener implements MouseListener
    {
    public void mouseClicked(MouseEvent event)
    { JTable target = (JTable)event.getSource();
    int row = target.getSelectedRow();
    int col = resultTable.columnAtPoint(event.getPoint());
    if (row>=0 && col>=0)
    { String str= "AAAAA";
    fusha.setEmri(str);
    }

    } public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}
    public void mousePressed(MouseEvent event) {}
    public void mouseReleased(MouseEvent event) {}

    }
    MouseListener listener = new ClickedListener();
    resultTable.addMouseListener(listener);*/

    connection.close();
    return resultTable;
    }

    public JMenuBar createMenu()
    {
    JMenuBar menu= new JMenuBar();
    menu.add(createFile());
    menu.add(createHelp());
    return menu;
    }

    public JMenu createFile()
    {
    JMenu menu1=new JMenu("File");
    menu1.add(exitItem());
    return menu1;
    }

    public JMenu createHelp()
    {
    JMenu menu2= new JMenu("Help");
    menu2.add(helpItem());
    return menu2;
    }

    public JMenuItem exitItem()
    {
    JMenuItem item= new JMenuItem("Exit");

    class MenuItem1 implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    System.exit(0);
    }
    }

    ActionListener listener= new MenuItem1();
    item.addActionListener(listener);
    return item;
    }

    public JMenuItem helpItem()
    {
    JMenuItem item = new JMenuItem("Exit");

    class MenuItem2 implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    System.exit(0);
    }
    }
    ActionListener listener= new MenuItem2();
    item.addActionListener(listener);
    return item;
    }

    }
    //******************************************
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.sql.SQLException;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;


    public class ShfaqFrame // extends TableResult
    {
    public static void main(String[] args)throws SQLException
    {
    TableResult table= new TableResult();
    SectionB sectionB= new SectionB();
    SectionC sectionC= new SectionC();
    JPanel panel= new JPanel();
    JPanel panelA= new JPanel();
    JFrame frame= new JFrame();
    panel.setLayout(new BorderLayout());
    panelA.setLayout(new GridLayout(2,1));
    panelA.setBorder(new EtchedBorder());
    panel.setBorder(new EtchedBorder());
    panelA.add(table.createMenu());
    panelA.add(table.createTable());
    panel.add(panelA,BorderLayout.NORTH);
    panel.add(sectionB.createMainPanel(),BorderLayout. CENTER);
    panel.add(sectionC.Butonat(),BorderLayout.SOUTH);
    frame.add(panel);
    frame.setSize(500,400);
    frame.setTitle("Eri Project");
    frame.setVisible(true);

    }
    }

    JTable and JTextAreas interaction-jtable-img.png

  2. #2
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

    Default Re: JTable and JTextAreas interaction

    are you sending the information to the correct variable that you are using to display the data? are you using the correct method to display the data?

  3. #3
    Enriketa is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: JTable and JTextAreas interaction

    I think they are ok.. The display of the data of my table in the DB is ok too. the problem is when i insert the rows it sends me "empty" fields . It also loads me the conncetion all over again and i think that makes the "empty data send " ...

Similar Threads

  1. Interaction with javascript
    By rasen58 in forum Java Applets
    Replies: 1
    Last Post: 11-05-2012, 04:07 PM
  2. want help about ArrayList and interaction
    By chmo in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 02:45 PM
  3. GUI interaction
    By Glyph in forum New To Java
    Replies: 6
    Last Post: 10-19-2010, 08:43 PM
  4. Multiple jtextareas in a splitpane?
    By justTina in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2010, 11:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •