I new with database and java.I want to show result of the table WHERE USER = USER LOGIN NAME

private void query() {
 try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@andy-lcoyx7gfw:1521:orcl", "andy", "andy");
            String sql = "select * from schedules where (userName = @User) ";
        //'{$_SESSION['username']}'
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(md.getColumnName(i));
            }
            while (rs.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(rs.getObject(i));
                }
                data.addElement(row);
            }
            rs.close();
            stmt.close();

        } catch (Exception e) {
            System.out.println(e);
        }

}

and you don't want to put your dabase access code in your query. you might want to create DAO objects as a bridge between your objects and the database

I am ot sure I am doing it righ but do you mean something like this.

package javaapplication5;

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javaapplication5.Insertschedules;
import javax.swing.table.*;

public class JTableDatabase2 extends javax.swing.JFrame {

    Vector columnNames = new Vector();
    Vector data = new Vector();

    public JTableDatabase2() {

        query();
        initComponents();

    }

    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Insertschedules.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JTableDatabase2().setVisible(true);
            }
        });
    }

    public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@andy-lcoyx7gfw:1521:orcl";
    String username = "andy";
    String password = "andy";
    Class.forName(driver);
    Connection con = DriverManager.getConnection(url, username, password);
    return con;
  }
    private void query() {

    ResultSet rs = null;
    Connection con = null;
    PreparedStatement pstmt = null;
 try {
            con = getConnection();
            String sql = "select * from schedules username = ?";
            pstmt = con.prepareStatement(sql); 
            pstmt.setInt(1, 1001); 
            rs = pstmt.executeQuery();
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(md.getColumnName(i));
            }
            while (rs.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(rs.getObject(i));
                }
                data.addElement(row);
            }
            rs.close();
            pstmt.close();
            con.close();

        } catch (Exception e) {
            System.out.println(e);
        }

}



    private void initComponents() {
        JPanel p = new JPanel();
        JTable table = new JTable(data, columnNames);
        TableColumn col;
        for (int i = 0; i < table.getColumnCount(); i++) {
            col = table.getColumnModel().getColumn(i);
            col.setMaxWidth(500);

        }
        JScrollPane scrollPane = new JScrollPane(table);
        p.add(scrollPane);
        JFrame f = new JFrame();
        f.add(p);
        f.setSize(900, 800);
        f.setVisible(true);

    }
}

Read the thread at the top of the java forum Starting "Java" [Java tutorials / resources / faq]

This article has been dead for over six months. Start a new discussion instead.