razvandragos29 Posted December 9, 2014 Report Share Posted December 9, 2014 (edited) Salut ma chinui de cateva ore sa adaug datele dintr-o bd intr-un Jtable si nu reusesc ,datele din bd sunt aduse deoarece am incercat cu Syste.out.println si in consola imi apar toate datele . Am incercat sa pun Jtable intr-un Panel nu a mers nici asta , am incercat sa nu folosesc nici un LayoutManager si sa ii setez dimensiunile cu setBounds() insa apare doar un chenar mare si alb atat .Acesta este codul sper sa ma lamureasca si pe mine cineva . package proiect;import java.sql.Date;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import javax.swing.table.DefaultTableModel;import com.mysql.jdbc.PreparedStatement;public class Conexiune { private final static String jdbcUrl= "jdbc:mysql://localhost:3306/EvidentaPopulatiei"; private final static String user="root" ; private final static String pass=""; private final static String dbClass= "com.mysql.jdbc.Driver"; private java.sql.Connection conn; public Conexiune() { try { Class.forName(dbClass); java.sql.Connection conn=DriverManager.getConnection(jdbcUrl,user,pass); if(conn != null) { System.out.println("Am reusiit conexiunea la baza de date!!"); } } catch (ClassNotFoundException | SQLException e) { System.out.println("Eroare"); } } public void insertData(long cnp,String nume, String prenume, String sex, int idStagiuMilitar, int idStareCivila, String idDomiciliu, int idCetatenie,long parinti,int idLocalitati,Date dataNasterii) { PreparedStatement statement = null ; try { java.sql.Connection conn=DriverManager.getConnection(jdbcUrl,user,pass); String query = "insert into Persoana (cnp,nume,prenume,sex,idStagiuMilitar,idStareCivila,idDomiciliu,idCetatenie,parinti,idLocalitati,dataNasterii) " + "Values (?,?,?,?,?,?,?,?,?,?,?)"; statement=(PreparedStatement) conn.prepareStatement(query); statement.setLong(1, cnp); statement.setString(2, nume); statement.setString(3,prenume); statement.setString(4, sex); statement.setInt(5, idStagiuMilitar); statement.setInt(6,idStareCivila); statement.setString(7,idDomiciliu); statement.setInt(8, idCetatenie); statement.setLong(9, parinti); statement.setInt(10, idLocalitati); statement.setDate(11, dataNasterii); statement.execute(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public DefaultTableModel extragereTabel() { DefaultTableModel model = new DefaultTableModel(); try { java.sql.Connection conn = DriverManager.getConnection(jdbcUrl,user,pass); java.sql.Statement statement = conn.createStatement(); String query = "select * from Persoana"; ResultSet rset = statement.executeQuery(query); while(rset.next()) { long cnp = rset.getLong("cnp"); String nume = rset.getString("nume"); String prenume = rset.getString("prenume"); String sex = rset.getString("sex"); int stagiuMilitar = rset.getInt("idStagiuMilitar"); int stareCivila = rset.getInt("idStareCivila"); String domiciliu = rset.getString("idDomiciliu"); int cetatenie = rset.getInt("idCetatenie"); int localitati = rset.getInt("idLocalitati"); Date dataNasterii =rset.getDate("dataNasterii"); long parinti = rset.getLong("parinti"); model.addRow(new Object[] {cnp,nume,prenume,sex,stagiuMilitar,stareCivila,domiciliu,cetatenie, localitati,dataNasterii,parinti }); System.out.println(cnp); System.out.println(dataNasterii); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return model; }} metoda cu pricina este extrageTabel si aici am si partea de interfata grafica package proiect;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class TableFrame extends JFrame { private JButton buton; private JTable table; private JPanel panel; public TableFrame() { super("TableForm"); table = new JTable(); panel = new JPanel(); buton = new JButton("Tabel"); buton.setBounds(500, 500, 150, 150); buton.setVisible(true); buton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == buton) { Conexiune conn = new Conexiune(); DefaultTableModel model = new DefaultTableModel(); model.addColumn("Cnp"); model.addColumn("Nume"); model.addColumn("Prenume"); model.addColumn("Sex"); model.addColumn("StagiuMilitar"); model.addColumn("StareCivila"); model.addColumn("Domiciliu"); model.addColumn("Cetatenie"); model.addColumn("Localitati"); model.addColumn("DataNasterii"); model.addColumn("Parinti"); model = conn.extragereTabel(); table = new JTable(model); } } }); table.setVisible(true); table.setFillsViewportHeight(true); JScrollPane tableContainer = new JScrollPane(table); panel.add(tableContainer); setLayout(new BorderLayout()); add(buton, BorderLayout.PAGE_START); add(panel, BorderLayout.CENTER); setSize(600, 600); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} PS:Am urmat exact indrumarile date in acest topic https://rstforums.com/forum/86400-adaugare-date-din-db-variabile-java.rst , model.addColumn() a fost pus si in metoda extrageTabel (); Edited December 9, 2014 by razvandragos29 Quote Link to comment Share on other sites More sharing options...
M2G Posted December 9, 2014 Report Share Posted December 9, 2014 Aici faci o instanta de model.public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == buton) { Conexiune conn = new Conexiune(); DefaultTableModel model = new DefaultTableModel();mai jos ai: model = conn.extragereTabel(); table = new JTable(model);aici model o sa fie inlocuit de instanta care ai creat-o in extragereTabel. Iar acea instanta nu are adaugate coloane. public DefaultTableModel extragereTabel() { DefaultTableModel model = new DefaultTableModel(); //... return model; }Incearca sa dai ca parametru obiectul de model deja creat. Incearca asa: public void extragereTabel(DefaultTableModel model) { try { java.sql.Connection conn = DriverManager.getConnection(jdbcUrl,user,pass); java.sql.Statement statement = conn.createStatement(); String query = "select * from Persoana"; ResultSet rset = statement.executeQuery(query); while(rset.next()) { long cnp = rset.getLong("cnp"); String nume = rset.getString("nume"); String prenume = rset.getString("prenume"); String sex = rset.getString("sex"); int stagiuMilitar = rset.getInt("idStagiuMilitar"); int stareCivila = rset.getInt("idStareCivila"); String domiciliu = rset.getString("idDomiciliu"); int cetatenie = rset.getInt("idCetatenie"); int localitati = rset.getInt("idLocalitati"); Date dataNasterii =rset.getDate("dataNasterii"); long parinti = rset.getLong("parinti"); model.addRow(new Object[] {cnp,nume,prenume,sex,stagiuMilitar,stareCivila,do miciliu,cetatenie, localitati,dataNasterii,parinti }); System.out.println(cnp); System.out.println(dataNasterii); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }si cand apelezi:DefaultTableModel model = new DefaultTableModel(); model.addColumn("Cnp"); model.addColumn("Nume"); model.addColumn("Prenume"); model.addColumn("Sex"); model.addColumn("StagiuMilitar"); model.addColumn("StareCivila"); model.addColumn("Domiciliu"); model.addColumn("Cetatenie"); model.addColumn("Localitati"); model.addColumn("DataNasterii"); model.addColumn("Parinti"); conn.extragereTabel(model); // aici se updateaza instanta asta de model cu datele din db table = new JTable(model); Quote Link to comment Share on other sites More sharing options...
razvandragos29 Posted December 9, 2014 Author Report Share Posted December 9, 2014 (edited) @M2G am incercat sa adaug coloanele in metoda din clasa Conexiune insa tot nici un rezultat si nici dupa ce am schimbat cu ce ai dat tu . " aici model o sa fie inlocuit de instanta care ai creat-o in extragereTabel. Iar acea instanta nu are adaugate coloane." public DefaultTableModel extragereTabel() { DefaultTableModel model = new DefaultTableModel(); //...------------------------>>>>>>>>>>>>am incercat sa adaug aici mode.addColumn() --insa nici un rezultat return model; } Edited December 9, 2014 by razvandragos29 Quote Link to comment Share on other sites More sharing options...
M2G Posted December 9, 2014 Report Share Posted December 9, 2014 Ce am zis mai sus + model.addColumn("Cnp"); model.addColumn("Nume"); model.addColumn("Prenume"); model.addColumn("Sex"); model.addColumn("StagiuMilitar"); model.addColumn("StareCivila"); model.addColumn("Domiciliu"); model.addColumn("Cetatenie"); model.addColumn("Localitati"); model.addColumn("DataNasterii"); model.addColumn("Parinti"); conn.extragereTabel(model); // aici se updateaza instanta asta de model cu datele din db table = new JTable(model);Tabelul e deja creat, iar ii dai alta instanta si e normal sa nu se vada.Foloseste-l pe cel care e creat mai sus.table.setModel(model); in loc de table = new JTable(model); 1 Quote Link to comment Share on other sites More sharing options...
razvandragos29 Posted December 9, 2014 Author Report Share Posted December 9, 2014 Da a mers mersi mult am facut acuma aceeasi greseala .. Quote Link to comment Share on other sites More sharing options...