razvandragos29 Posted July 1, 2014 Report Posted July 1, 2014 (edited) Incerc sa adaug un nou rand intr-un tabel si acesta sa fie cu informatii din baza de date am public String [] return1() throws SQLException { Connection con=(Connection) DriverManager.getConnection(conbd, user, parola); java.sql.Statement stmt=con.createStatement(); String query="select * from elev"; ResultSet rset=stmt.executeQuery(query); int i=0; while(rset.next()) { String nume=rset.getString("nume"); String ceva1=rset.getString("adresa"); rand[i]={nume,ceva1} ; i++; } return null; } nu pot pune acele stringuri intr-un array de stringuri ca pe urma sa folosesc metoda addrow() care primeste in paranteza ca parametru un array separat de virgule Edited July 1, 2014 by razvandragos29 Quote
M2G Posted July 1, 2014 Report Posted July 1, 2014 Folosesti un table model? Din cate imi dau seama, nu folosesti. As vrea totusi sa stiu.Nu mai da nume la metode in genul "return1()" ca nu iti zice nimic acel nume. Nici macar pt un exemplu nu da numele aiurea. Invata sa scrii cod care zice ceva. Aceasi greseala si aici:String ceva1=rset.getString("adresa"); Puteai sa ii zici frumos String adresa = rset.getString("adresa"); Poate acum nu iti dai seama dar chestiile astea conteaza mai mult decat crezi.Iti lipsesc conceptele de baza in lucrul cu metodele. De ce declari o metoda de tip String [] si pe urma returnezi null?E normal sa nu iti mearga asa. while(rset.next()) { String nume=rset.getString("nume"); String ceva1=rset.getString("adresa"); rand={nume,ceva1} ; i++; }La fiecare iteratie din acel while tu declari alte stringuri si cred ca incerci sa le salvezi undeva dar nu imi dau seama unde. Ce este acea variabila rand?Da-mi un exemplu sa vad cum folosesti acea functie, unde o apelezi si ce te astepti sa faca. Quote
razvandragos29 Posted July 1, 2014 Author Report Posted July 1, 2014 deci mai sus am variabila string declarata String rand[]; eu vreau sa ma pot folosi de stringu acela intr-o alta clasa Gui unde am tabelul pus o sa folosesc DefaultTableModel pentru a putea folosi metoda addrow(String []), pt a adauga randuri in tabel depinzand de fiecare metoda pe care o apelez acolo am lasat null asa stiu ca trebuia sa intoarca un String Quote
M2G Posted July 1, 2014 Report Posted July 1, 2014 Poti sa postezi codul clasei GUI si codul clasei din care face parte metoda de mai sus?Foloseste tagul [Java] [/Java] Quote
razvandragos29 Posted July 1, 2014 Author Report Posted July 1, 2014 package proiect;import java.awt.ScrollPane;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class Gui extends JFrame { JTable table ; BD conex; JScrollPane pane; JButton buton; public Gui() throws SQLException { super("Fereastra"); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tabel(); setLayout(null); add(table); add(buton); setVisible(true); } public void tabel() throws SQLException { conex=new BD(); conex.return1(); String lista=conex.rand; model=new DefaulTableModel(); model.addrow(lista); table =new JTable(model); JScrollPane pane=new JScrollPane(table); table.setFillsViewportHeight(true); table.setBounds(10, 10, 400,200); buton =new JButton("Buton aici"); buton.setBounds(200, 10, 20, 20); } } sper sa intelegi ce vreau sa fac multumesc anticipat stiu ca inca nu stapanesc unele chestii bine si am anumite goluri dar sunt presat de facultate sa termin proiectu pana vineri si nu vreau sa ii fac ceva complicat ceva simplu si la obiect Quote
M2G Posted July 1, 2014 Report Posted July 1, 2014 Ok, o sa te ajut dar daca vrei sa avansezi o sa trebuiasca sa iei lucrurile de la inceput ca asa nu o sa intelegi mare lucru si o sa inveti greu daca ai multe goluri in cunostinte.Nu am testat ca am scris codul direct in notepad asa ca poate sunt greseli.Incearca asa:package proiect;import java.awt.ScrollPane;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class Gui extends JFrame { JTable table ; BD conex; JScrollPane pane; JButton buton; public Gui() throws SQLException { super("Fereastra"); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tabel(); setLayout(null); add(table); add(buton); setVisible(true); } public void tabel() throws SQLException { conex=new BD(); //conex.return1(); //String lista=conex.rand; model = conex.extractTableModel() ; // model.addrow(lista); model.addColumn("Nume"); model.addColumn("Adresa");// etc... aici poti sa pui ce coloane te astepti sa ai in tabel. Retine ordinea lor! table =new JTable(model); JScrollPane pane=new JScrollPane(table); table.setFillsViewportHeight(true); table.setBounds(10, 10, 400,200); buton =new JButton("Buton aici"); buton.setBounds(200, 10, 20, 20); } } public DefaulTableModel extractTableModel() throws SQLException { DefaulTableModel model = new DefaulTableModel(); Connection con=(Connection) DriverManager.getConnection(conbd, user, parola); java.sql.Statement stmt=con.createStatement(); String query="select * from elev"; ResultSet rset=stmt.executeQuery(query); int i=0; while(rset.next()) { String nume=rset.getString("nume"); String adresa=rset.getString("adresa"); model.addRow(new Object[] {nume, adresa}); } return model; }Daca mai ai intrebari, ma gasesti aici. Quote
razvandragos29 Posted July 1, 2014 Author Report Posted July 1, 2014 (edited) Am pus codul dat de tine insa nu imi returneaza nimica in tabel codul merge am mai adaugat eu in clasa Gui, am declarat variabila model ca fiind defaulttablemodel :Dv + unde ai pus tu cu mode.addColumn ar trebui cred ca sus sa imi scrie numele coloanei nu merge nici asta Edited July 1, 2014 by razvandragos29 Quote
M2G Posted July 1, 2014 Report Posted July 1, 2014 Trebuie sa adaugi coloanele inainte de randuri. Pentru simplitate si pentru ca nu prea mai am mult timp, poti muta adaugarea coloanelor in metoda aia din conexiune.A doua problema e ca tu pui tabelul intr-u JScrollPane pe care il instantiezi si local si la nivel de clasa si nu il adaugi nicaieri pe fereastra.Deci:public DefaulTableModel extractTableModel() throws SQLException { DefaulTableModel model = new DefaulTableModel(); model.addColumn("Nume"); model.addColumn("Adresa");// etc... aici poti sa pui ce coloane te astepti sa ai in tabel. Retine ordinea lor! Connection con=(Connection) DriverManager.getConnection(conbd, user, parola); java.sql.Statement stmt=con.createStatement(); String query="select * from elev"; ResultSet rset=stmt.executeQuery(query); int i=0; while(rset.next()) { String nume=rset.getString("nume"); String adresa=rset.getString("adresa"); model.addRow(new Object[] {nume, adresa}); } return model; } package proiect;import java.awt.ScrollPane;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class Gui extends JFrame { JTable table ; BD conex; JScrollPane pane; JButton buton; public Gui() throws SQLException { super("Fereastra"); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tabel(); setLayout(null); add(pane); // trebuie adaugat panoul. Panoul deja contine tabelul asa ca nu trebuie adaugat direct tabelul. add(buton); setVisible(true); } public void tabel() throws SQLException { conex=new BD(); //conex.return1(); //String lista=conex.rand; model = conex.extractTableModel() ; // model.addrow(lista); table =new JTable(model); pane=new JScrollPane(table); // aici am sters declararea unei noi variable. era declarata la nivel de clasa. table.setFillsViewportHeight(true); table.setBounds(10, 10, 400,200); buton =new JButton("Buton aici"); buton.setBounds(200, 10, 20, 20); } } Quote
razvandragos29 Posted July 1, 2014 Author Report Posted July 1, 2014 Da multumesc mult totul a mers de minune Quote
razvandragos29 Posted July 2, 2014 Author Report Posted July 2, 2014 Salut incerc sa aduc acuma informatiile in tabel bazat pe un sistem de login am urmatoarele clasa package proiect;import java.awt.ScrollPane;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPasswordField;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.table.DefaultTableModel;public class Gui extends JFrame { static JTextField user; JPasswordField pass; JButton login; JLabel username; JLabel password; public Gui() throws SQLException { super("Log in Window"); setSize(300,200); setLayout(null); Componente(); add(user); add(pass); add(login); add(username); add(password); setResizable(false); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void Componente() { user=new JTextField(15); user.setBounds(70, 30, 150, 20); pass=new JPasswordField(15); pass.setBounds(70, 65, 150, 20); login=new JButton("Log in"); login.setBounds(110,100,80,20); login.addActionListener(actiune); username=new JLabel("Username"); username.setBounds(1,30,70,20); password=new JLabel("Password"); password.setBounds(1, 65, 70, 20); } Actiune actiune=new Actiune(); public class Actiune implements ActionListener { @Override public void actionPerformed(ActionEvent argument) { String pname=user.getText(); String ppass=new String(pass.getPassword()); if (argument.getSource()==login){ BD dbHandler=new BD(); int tip =dbHandler.queryUserType(pname, ppass); if(tip==1) { System.out.println("Userul este admin") ; dispose(); new GuiProfesor(); } else { System.out.println("Userul este normalll"); dispose(); try { new GuiElev(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } } package proiect;import java.sql.SQLException;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class GuiElev extends JFrame { JTable table ; BD conex; JScrollPane pane; JButton buton; public GuiElev() throws SQLException { super("Gui Elev"); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); setVisible(true); tabel(); add(pane); add(buton); } public void tabel() throws SQLException { conex=new BD(); String nume=Gui.user.getText(); System.out.println(nume); DefaultTableModel model=new DefaultTableModel(); model = conex.extractTableModel(nume) ; // model.addrow(lista); table =new JTable(model); pane=new JScrollPane(table); // aici am sters declararea unei noi variable. era declarata la nivel de clasa. table.setFillsViewportHeight(true); table.setBounds(10, 10, 400,200); buton =new JButton("Buton aici"); buton.setBounds(200, 10, 20, 20); } } public DefaultTableModel extractTableModel(String nume) throws SQLException { DefaultTableModel model = new DefaultTableModel(); model.addColumn("Nume"); model.addColumn("Adresa");// etc... aici poti sa pui ce coloane te astepti sa ai in tabel. Retine ordinea lor! model.addColumn("ide"); Connection con=(Connection) DriverManager.getConnection(conbd, user, parola); java.sql.Statement stmt=con.createStatement(); String queryy="select nume,adresa,id from elev where" + "nume='" +nume+ "'"; ResultSet rsett=stmt.executeQuery(queryy); while(rsett.next()) { String numee=rsett.getString("nume"); String adresa=rsett.getString("adresa"); int ide=rsett.getInt("ide"); model.addRow(new Object[] {numee, adresa,ide}); } return model; } imi spune ca nu am sintaxa corecta la sql insa am verificat in mysql exact acelasi select si merge imi returneaza ceea ce vreau Quote
tjt Posted July 2, 2014 Report Posted July 2, 2014 String queryy="select nume,adresa,id from elev where" + "nume='" +nume+ "'";La "... where" + "nume="... rezulta wherenume in SQL.Incearca "select nume,adresa,id from elev where nume='" +nume+ "'"; sau baga un spatiu inainte de nume sau dupa where. Quote
razvandragos29 Posted July 2, 2014 Author Report Posted July 2, 2014 da asta era lipsea un plus neatentia asta si sunt si racit . Quote