Results 1 to 4 of 4
  1. #1
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    439
    Rep Power
    6

    Default import database in Jtable?

    hello

    i want to display my database table in Jtable component?

    could u send some sample code for that?
    Mak
    (Living @ Virtual World)

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    you should only iterate over the ResultSet of your query and add rows to the JTable (for the row data).

    you should iterate over the ResultSetMetaData of the ResultSet and add your columns to the JTable (for the column names).

    here is the java docs for using tables
    How to Use Tables (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

    here is an example for using the QueryTableModel in JTable instead of the normal model:
    Java Tips - How to show data in database with a JTable

    i really think you should do a search on google for code examples, you should find some fast :P

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Mak, writing that code in a usable manner should involve at least three classes, one for handling the database records, one for handling the GUI, and one for handling the interactions between the two. That's called MVC (Model View Controller).

    If you want to code in Java, you need to learn to do it "right". Java simply does not provide a whole lot of shortcuts. Microsoft .NET would allow you do what you described with minimal coding...

  4. #4
    premavidya is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default import database into JTable

    Hello,
    im doing a mini project on JTable adn database. i want to know how to fetch data from database adn display into the JTable when a button is clicked.. im sending the code in which when i click on the enter button i should get the data into the JTable from database.. my doubt is im getting the data but its not aligning properly and one more thing is the quantity field should be empty. i need to enter that field by hand at runtime.... can any one please help me...

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Font.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import java.util.Vector;
    import javax.swing.table.AbstractTableModel;
    import java.io.*;
    import java.util.Date;
    import java.sql.*;
    import javax.swing.table.DefaultTableModel;

    public class POSPrinterExample3 extends JFrame
    implements ActionListener
    {

    String portname1="COM1";
    public POSPrinterExample3()
    {



    setTitle("JavaPOS POSPrinter Example");
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOS E);



    Container pane = getContentPane();

    deviceNameField.setEditable(false);
    pane.setLayout(new FlowLayout());

    JPanel headerPanel = new JPanel(new FlowLayout());
    headerPanel.setBorder(new TitledBorder(""));
    headerPanel.add(deviceNameLabel);
    headerPanel.add(deviceNameField);
    pane.add(headerPanel);

    JPanel actionButtonsPanel = new JPanel(new GridLayout(1, 3));
    actionButtonsPanel.setBorder(new TitledBorder(""));
    actionButtonsPanel.add(selectButton);
    actionButtonsPanel.add(openButton);
    actionButtonsPanel.add(closeButton);
    pane.add(actionButtonsPanel);

    JPanel entryPanel = new JPanel();
    entryPanel.setBorder(new TitledBorder(""));
    entryPanel.add(entryFieldPrompt);
    entryPanel.add(entryField);

    pane.add(entryPanel);

    JPanel tablePanel = new JPanel(new GridLayout(1,3));
    tablePanel.setBorder(new TitledBorder(""));
    tablePanel.add(new JScrollPane(table), BorderLayout.CENTER);
    pane.add(tablePanel);

    JPanel exitPanel = new JPanel();
    exitPanel.setBorder(new TitledBorder(""));
    exitPanel.add(exitButton);
    exitPanel.add(printButton);
    exitPanel.add(enter);
    exitPanel.add(update);

    pane.add(exitPanel);

    pack();


    EvtListener elWindow = new EvtListener();
    addWindowListener(elWindow);
    selectButton.addActionListener(this);
    openButton.addActionListener(this);
    closeButton.addActionListener(this);
    exitButton.addActionListener(this);
    printButton.addActionListener(this);
    entryField.addActionListener(this);
    enter.addActionListener(this);

    }

    static public void main(String args[])
    {
    try
    {
    POSPrinterExample3 ra = new POSPrinterExample3();

    ra.setSize(800, 600);
    ra.setVisible(true);
    }
    catch (Exception e)
    {
    System.err.println(e);
    e.printStackTrace();
    System.exit(1);
    }
    }
    public Connection getConnection(){
    Connection connection = null;
    try {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


    connection = DriverManager.getConnection("Jdbc:Odbc:oracle","sc ott","tiger");
    } catch (ClassNotFoundException e) {

    } catch (SQLException e) {

    }
    return connection;
    }
    public void actionPerformed(ActionEvent event)
    {
    ResultSet rs=null;
    Connection con=null;

    Statement st=null;
    String s=entryField.getText();
    if(event.getSource()==enter)
    {
    try {

    con = getConnection();
    st = con.createStatement();
    model = new DefaultTableModel(new String[][]{}, new String[]{"Id",
    "item","rate"});
    rs = st.executeQuery("SELECT * FROM printing ");

    while(rs.next())
    {


    String itemid = rs.getString(1);
    String itemname = rs.getString(2);
    String itemrate = rs.getString(3);
    model.addRow(new String[] {itemid, itemname,itemrate});

    tableData.addText(itemid);
    tableData.addText(itemname);
    tableData.addText(itemrate);


    }

    }
    catch (SQLException e) {

    e.printStackTrace();
    }
    }

    Object object = event.getSource();

    if (object == exitButton)
    exitApplication();
    else if (object == selectButton)
    selectPOSPrinter();
    else if (object == openButton)
    openPOSPrinter();
    else if (object == closeButton)
    closePOSPrinter();
    else if (object == printButton)
    printText();
    }

    void selectPOSPrinter()
    {
    System.out.println("epson device selected");

    }

    void openPOSPrinter()
    {
    System.out.println("epson device opened");
    }

    void closePOSPrinter()
    {
    System.out.println("epson device closed");
    }

    void printText()
    {
    if(portname1=="COM1")
    {
    try{
    System.out.println("hello");
    FileOutputStream os = new FileOutputStream(portname1);


    //wrap stream in "friendly" PrintStream
    PrintStream ps = new PrintStream(os);

    //print text here
    Date today = new Date();
    ;
    ps.println("");
    ps.println("");
    ps.println(today);
    ps.println("");
    ps.println("");
    ps.println("ID Qnty Item Rate\n");



    // ps.println(textData);

    ps.println("\t=======================");
    ps.println("\n\t\tSubtotal:200 ");
    ps.println("\t========================");
    ps.println("\t\ttender: 200");
    ps.println("\t========================");
    ps.println("\t\tChange: 0");
    ps.println("\n\n\t***Thank you***");
    ps.print("\f");

    ps.close();
    }
    catch (Exception e2)
    {
    System.out.println("Exception occurred: " + e2);
    }
    }
    }

    protected void exitApplication()
    {
    // Exit the application
    System.exit(0);
    }

    class EvtListener extends WindowAdapter
    {
    public void windowClosing(WindowEvent event) {
    Object object = event.getSource();
    if (object==POSPrinterExample3.this)
    {
    exitApplication();
    }
    }
    }


    JButton openButton = new JButton("Open Printer");
    JButton closeButton = new JButton("Close Printer");
    JButton selectButton = new JButton("Select Printer");
    JButton printButton = new JButton("Print");
    JButton exitButton = new JButton("Exit");
    JButton enter = new JButton("Enter");
    JButton update = new JButton("Insert");
    JTextField entryField = new JTextField(10);
    JLabel entryFieldPrompt = new JLabel("Enter text to be printed: ");

    JLabel deviceNameLabel = new JLabel("Logical Device Name: ");
    JTextField deviceNameField = new JTextField(10);

    final SimpleModel tableData = new SimpleModel();

    JTable table = new JTable(tableData);
    static DefaultTableModel model;
    }

    class SimpleModel extends AbstractTableModel {

    public Vector textData = new Vector();

    public String[] columnNames = {"ID","QUANTITY",
    "ITEM",
    "RATE"
    };


    public void addText(String text) {
    textData.addElement(text);
    fireTableDataChanged();//Notifies all listeners that all cell values in the table's rows may have changed.


    }

    public int getRowCount() {
    int n=textData.size();
    return n;

    }

    public int getColumnCount() {
    return columnNames.length;
    }

    public String getColumnName(int col) {
    return columnNames[col];
    }
    public Object getValueAt(int row, int column) {
    return textData.elementAt(row);
    }

    }

Similar Threads

  1. JTable and database
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 10:15 PM
  2. editing jTable then writing new values to database!
    By sweet angle in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2009, 05:07 PM
  3. Replies: 0
    Last Post: 12-04-2008, 05:39 PM
  4. Updating database table from JTable
    By yesjava in forum New To Java
    Replies: 1
    Last Post: 08-16-2008, 10:16 PM
  5. Replies: 0
    Last Post: 03-29-2008, 03:36 PM

Posting Permissions

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