Results 1 to 3 of 3
  1. #1
    dream88_angel is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Unhappy help Sql and JTable

    Hello,

    I try to display fields from my SQL database into a JTable,but something goes wrong.The code i have written ony displays the column names.Please help me to find the problem.

    I have posted the code below.



    public class ResultSetTable implements TableModel{

    ResultSet results;
    ResultSetMetaData metadata;
    int numcols, numrows;


    ResultSetTable(ResultSet results) throws SQLException {

    this.results = results;
    metadata = results.getMetaData();
    numcols = metadata.getColumnCount();
    results.last();
    numrows = results.getRow();
    }

    public void close() {
    try { results.getStatement().close(); }
    catch(SQLException e) {};
    }

    protected void finalize() { close(); }


    public int getColumnCount() { return numcols; }
    public int getRowCount() { return numrows; }

    public String getColumnName(int column) {
    try {
    return metadata.getColumnLabel(column+1);
    } catch (SQLException e) { return e.toString(); }
    }


    public Class getColumnClass(int column) { return String.class; }

    public Object getValueAt(int row, int column) {
    try {
    results.absolute(row+1);
    Object o = results.getObject(column+1);
    if (o == null) return null;
    else return o.toString();
    } catch (SQLException e) { return e.toString(); }
    }


    public boolean isCellEditable(int row, int column) { return false; }


    public void setValueAt(Object value, int row, int column) {}
    public void addTableModelListener(TableModelListener l) {}
    public void removeTableModelListener(TableModelListener l) {}


    }

    __________________________________________________ ____________

    public class FereastraAfisarePacienti extends JFrame {

    Operatii factory;
    JTextField query;
    JTable table;
    JLabel msgline;

    public FereastraAfisarePacienti(Operatii f) {
    super("Afisare pacienti");

    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) { System.exit(0); }
    });

    this.factory = f;

    query = new JTextField();
    table = new JTable();
    msgline = new JLabel();

    Container contentPane = getContentPane();
    contentPane.add(query, BorderLayout.NORTH);
    contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
    contentPane.add(msgline, BorderLayout.SOUTH);

    query.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

    displayQueryResults(query.getText());
    }
    });
    }

    public void displayQueryResults(final String q) {

    msgline.setText("Realizare conexiune...");

    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {

    table.setModel(factory.afisareListaPacienti(q));
    msgline.setText(" ");
    }
    catch (SQLException ex) {

    msgline.setText(" ");
    JOptionPane.showMessageDialog(FereastraAfisarePaci enti.this,
    new String[] { // Display a 2-line message
    ex.getClass().getName() + ": ",
    ex.getMessage()
    });
    }
    }
    });

    }



    public static void main(String args[]) throws Exception {

    Operatii factory = new Operatii();

    FereastraAfisarePacienti qf = new FereastraAfisarePacienti(factory);

    qf.setSize(500, 600);
    qf.setVisible(true);


    }
    }
    __________________________________________________ _______________
    public class Operatii {

    private Connection con;


    public Operatii()
    {
    }

    public ResultSetTable afisareListaPacienti(String nume_medic ) throws SQLException
    {


    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","password");
    Statement st = con.createStatement();
    ResultSet result = st.executeQuery("SELECT * FROM pacienti WHERE medic = '?'");

    return new ResultSetTable(result);

    }

    Thank you!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I think this question is misplaced. I don't see any reference to the Applet class.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JTable
    By riddhishah28 in forum AWT / Swing
    Replies: 2
    Last Post: 02-19-2011, 07:24 AM
  2. Need help with JTable
    By murali18 in forum AWT / Swing
    Replies: 2
    Last Post: 03-23-2010, 05:07 PM
  3. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  4. Regarding JTable
    By adeeb in forum AWT / Swing
    Replies: 12
    Last Post: 06-19-2008, 07:39 AM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 AM

Posting Permissions

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