Avatar billede SjollerDk Nybegynder
10. oktober 2009 - 01:19 Der er 3 kommentarer

Vis Oracle tabel i jtextfield

Hej. Har siddet og leget lidt med connection fra oracle til java, og dette fungerer egentlig meget godt. Har også fået diverse funktion til at virke, men en den driller mig. Det er garanteret rigtig simpelt, men kan ikke lige lure hvad jeg gør forkert.

Har en tabel med postnummer, og med bynavn, vil gerne liste alle poster i denne ud i en jtextfield (eller er der andet bedre at benytte til dette?).

Class: OraConn5 (selve connection delen er fjernet)

package productconnect;
import java.sql.*;

/**
*
* @author Jones
*/
public class OraConn5 {
   
    private Passwd login;
    private Connection conn;
    private Statement stmt;
    private ResultSet rset;
   
public OraConn5 ()
{


public ResultSet getAllRecords()
{
    try
    {
        String str = new String("select POSTALCODE, CITYNAME from city"); 
        //stringobject with  select statement
        rset = stmt.executeQuery(str);
        System.out.println("have got all records");
        //metoden executeQuery(str) make select-statement
        //rset put = result set
    }
   
    catch (SQLException e)
    {
        System.out.println("there is a sql fault (2)"+e);
    }
    catch (Exception e)
    {
        System.out.println("fault"+e);
    }
      return rset;
   
}

public ResultSet getMaxPostalCode()
{
    try
    {
        String str = new String("select max(POSTALCODE) from city");
        //stringobject with select-statement.
        rset = stmt.executeQuery(str);
        System.out.println("have got max postalcode");
        //metoden executeQuery(str) make select-statement
        //rset put = result set
    }
   
    catch (SQLException e)
    {
        System.out.println("there is a sql fault (3)"+e);
    }
    catch (Exception e)
    {
        System.out.println("fault"+e);
    }
    return rset;
}

public ResultSet getOneRecord(String valg)
{
    try
    {
        String str = new String("select POSTALCODE, CITYNAME from city where POSTALCODE = '"+valg+"'");
        //stringobject with select-statement
        System.out.println("str = "+str);
        rset = stmt.executeQuery(str);
        System.out.println("I have got 1 record");
        //methode executeQuery(str) make select-statement
        //rset put = result set
    }
   
    catch (SQLException e)
    {
        System.out.println("there is a sql fault (4) "+e);
    }
        catch (Exception e)
        {
            System.out.println("fejl "+e);
        }
        return rset; 
}

public void showAll()
{
 
    try
    {
        //while loop move to all records in result set
        while (rset.next())
        {
           
            System.out.println (rset.getString(1)+"\t"+ rset.getString(2));
            //call methode getString, write value for columm nr. 1 and 2
            //tab is use.
        }
    }
    catch (SQLException e)
    {
        System.out.println("There is a sql fault (5) "+e);
    }
    catch (Exception e)
    {
        System.out.println("fault "+e);
    }
}

public void showNextRecord()
{
    try
    {
        if(rset.next())
           
        {
            System.out.println (rset.getString(1)+"\t"+ rset.getString(2));
            //call methode getString, write value for columm nr.1 and 2
            //tab is use.
        }
    }
    catch (SQLException e)
    {
        System.out.println("there is a sql fault (6) "+e);
    }
    catch (Exception e)
    {
        System.out.println("fault "+e);
    }
}

public void showPreviousRecord()
{
    try
    {
        if (rset.previous())
        {
            System.out.println (rset.getString(1)+"\t"+ rset.getString(2));
            //call methode getString, write value for columm nr.1 and 2 
        }
    }
    catch (SQLException e)
    {
        System.out.println("there is a sql fault (7) "+e);
    }
    catch (Exception e)
    {
        System.out.println("fault "+e);
    }
}
   
public void createInput(int postalcode, String cityname) throws SQLException
{
    Statement stmt = createStatement();
    //call methode make 'Statement' objekt, as stmt look on.
    stmt.executeUpdate("insert into city(POSTALCODE, CITYNAME) values ("+postalcode+" , '"+cityname+"')");
    //put a record in table drinks
}

public void createUpdate(int postalcode, String cityname) throws SQLException
{
    Statement stmt = createStatement();
    //call methode make 'Statement' objekt, as stmt look on.
    String test = "update city set postalcode ="+postalcode+", cityname ='"+cityname+"' where postalcode =" +postalcode;
    System.out.println(test);
    stmt.executeUpdate("update city set postalcode ="+postalcode+", cityname ='"+cityname+"' where postalcode =" +postalcode);
    //change record in table city.
}

public void createDelete(int postalcode) throws SQLException
{
    Statement stmt = createStatement();
    //call methode make 'Statement' objekt, as stmt look on.
    stmt.executeUpdate("delete city where postalcode ="+postalcode);
    //delete a record in table city
}
}

Class: CustomerGUI

/*
* CustomerGUI.java
*
* Created on 8. oktober 2009, 09:37
*/

package productconnect;
import java.sql.*;

/**
*
* @author  Jones
*/
public class CustomerGUI extends javax.swing.JFrame {
   
private OraConn5 oc5;
private String input="", output="";
private int postalcode;
private String cityname;
private ResultSet rset;

    /** Creates new form CustomerGUI */
    public CustomerGUI() {
     
        oc5 = new OraConn5();
        rset = oc5.getAllRecords();
        System.out.println("You have got the set of data");
        initComponents();
    }
   
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        postalcodeTextField = new javax.swing.JTextField();
        citynameTextField = new javax.swing.JTextField();
        createButton = new javax.swing.JButton();
        fremButton = new javax.swing.JButton();
        tilbageButton = new javax.swing.JButton();
        nulstilButton = new javax.swing.JButton();
        findlButton = new javax.swing.JButton();
        redigerlButton = new javax.swing.JButton();
        sletButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        visButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        visTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        postalcodeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                postalcodeTextFieldActionPerformed(evt);
            }
        });

        citynameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                citynameTextFieldActionPerformed(evt);
            }
        });

        createButton.setText("Opret");
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        fremButton.setText("Frem");
        fremButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fremButtonActionPerformed(evt);
            }
        });

        tilbageButton.setText("Tilbage");
        tilbageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tilbageButtonActionPerformed(evt);
            }
        });

        nulstilButton.setText("Nulstil");
        nulstilButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nulstilButtonActionPerformed(evt);
            }
        });

        findlButton.setText("Find");
        findlButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findlButtonActionPerformed(evt);
            }
        });

        redigerlButton.setText("Rediger");
        redigerlButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redigerlButtonActionPerformed(evt);
            }
        });

        sletButton.setText("Slet");
        sletButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sletButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Postnr:");

        jLabel2.setText("By:");

        visButton.setText("Vis Alle");
        visButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                visButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(37, 37, 37)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(postalcodeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                            .addComponent(citynameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(113, 113, 113)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tilbageButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(fremButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(visButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(redigerlButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(findlButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nulstilButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(createButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(sletButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(24, 24, 24))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createButton)
                    .addComponent(jLabel1)
                    .addComponent(postalcodeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(citynameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(nulstilButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(findlButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tilbageButton)
                    .addComponent(redigerlButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sletButton)
                    .addComponent(fremButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(visButton)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        visTextArea.setColumns(20);
        visTextArea.setRows(5);
        jScrollPane1.setViewportView(visTextArea);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(79, 79, 79))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(229, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void postalcodeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
}                                                 

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
input = postalcodeTextField.getText();
postalcode = Integer.parseInt(input);

cityname = citynameTextField.getText();

try
{
    oc5.createInput(postalcode, cityname);
    //insert a record in table drinks
    System.out.println("Record inserted");
}

catch (SQLException f)
{
    System.out.println("there is a sql faul "+f);
}

catch (Exception f)
{
    System.out.println("faul "+f);
}

rset = oc5.getAllRecords();

// TODO add your handling code here:
    }                                           

    private void citynameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                 

// TODO add your handling code here:
    }                                               

    private void fremButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try
    {
        if (rset.next())
        {
            postalcode = rset.getInt(1);
            //getting int number.
            cityname = rset.getString(2);
            //calling getString, write value for columm 2.
           
            postalcodeTextField.setText(""+postalcode);
            citynameTextField.setText(cityname);
        }
   
    }
      catch (SQLException f)
    {
        System.out.println("there is a sqlfault "+f);
    }
    catch (Exception f)
    {
        System.out.println("faul "+f);
    }       
        // TODO add your handling code here:
}                                         

    private void tilbageButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try
    {
        if (rset.previous())
        {
            postalcode = rset.getInt(1);
            //getting int number.
            cityname = rset.getString(2);
            //calling getString, write value for columm 2.
           
            postalcodeTextField.setText(""+postalcode);
            citynameTextField.setText(cityname);
        }
   
    }
      catch (SQLException f)
    {
        System.out.println("there is a sqlfault "+f);
    }
    catch (Exception f)
    {
        System.out.println("faul "+f);
    }
        // TODO add your handling code here:
}                                           

    private void nulstilButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
postalcodeTextField.setText("");
citynameTextField.setText("");

rset = oc5.getAllRecords();
// TODO add your handling code here:
}                                           

    private void findlButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
input = postalcodeTextField.getText();

rset = oc5.getOneRecord(input);

    try
        {
            if (rset.next())
            {
                postalcode = rset.getInt(1);
                //get number
                cityname = rset.getString(2);
                //call getString for columm nr.2
               
                postalcodeTextField.setText(""+postalcode);
                citynameTextField.setText(cityname);
               
                rset = oc5.getAllRecords();
            }
           
        }
   
    catch (SQLException f)
    {
        System.out.println("there is a sqlfault "+f);
    }
    catch (Exception f)
    {
        System.out.println("there is a faul "+f);
    }
        // TODO add your handling code here:
}                                         

    private void redigerlButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
input = postalcodeTextField.getText();
postalcode = Integer.parseInt(input);

cityname = citynameTextField.getText();

try
{
    oc5.createUpdate(postalcode, cityname);
    //update a record in table city
    System.out.println("Record updated");
}

catch (SQLException f)
{
    System.out.println("there is a sql faul "+f);
}

catch (Exception f)
{
    System.out.println("faul "+f);
}

rset = oc5.getAllRecords();       
// TODO add your handling code here:
}                                             

    private void sletButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
input = postalcodeTextField.getText();
postalcode = Integer.parseInt(input);

try
{
    oc5.createDelete(postalcode);
    //insert a record in table drinks
    System.out.println("Record deleted");
}

catch (SQLException f)
{
    System.out.println("there is a sql faul "+f);
}

catch (Exception f)
{
    System.out.println("faul "+f);
}

rset = oc5.getAllRecords();
        // TODO add your handling code here:
}                                         

    private void visButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
}                                       
   
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CustomerGUI().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify
    private javax.swing.JTextField citynameTextField;
    private javax.swing.JButton createButton;
    private javax.swing.JButton findlButton;
    private javax.swing.JButton fremButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nulstilButton;
    private javax.swing.JTextField postalcodeTextField;
    private javax.swing.JButton redigerlButton;
    private javax.swing.JButton sletButton;
    private javax.swing.JButton tilbageButton;
    private javax.swing.JButton visButton;
    private javax.swing.JTextArea visTextArea;
    // End of variables declaration
   
}

----


Som det kan ses har jeg rodet lidt med det i OraConn5 klassen

public void showAll()


        while (rset.next())
        {
           
            System.out.println (rset.getString(1)+"\t"+ rset.getString(2));

        }

Det printer det jo også fint nok ud i prompten, men kan ikke rigtig få det omsat til at blive sat ind i et jtextfield(eller andet?). Skriv endelig hvis jeg ikke har givet nok info med.

På forhånd tak.
Avatar billede arne_v Ekspert
10. oktober 2009 - 01:48 #1
Jeg gætter på at det er dette her som du leder efter:
  http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Det er ikke helt nemt, men der nok måden at gøre det på !
Avatar billede arne_v Ekspert
02. november 2009 - 02:50 #2
Kommet videre ?
Avatar billede SjollerDk Nybegynder
03. november 2009 - 21:25 #3
hej. undskyld jeg ikke har fået fulgt op på den her tråd.

er gået i krig med mit 1. års projekt, så har ikke rigtig haft tid til at kigge på det siden sidst :-|
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester